Question regarding lazy

25 views
Skip to first unread message

Hari K T

unread,
Dec 20, 2014, 5:05:37 AM12/20/14
to aur...@googlegroups.com
Hi, 

I hope all are doing good. I am having a trouble setting lazy object in di.

This is the code .

$di->set('default_connection', $di->lazyNew('Aura\Sql\ExtendedPdo'));
        $di->set('query_factory', $di->lazyNew('Aura\SqlMapper_Bundle\Query\ConnectedQueryFactory'));
        $di->set('connection_locator', $di->lazyNew('Aura\Sql\ConnectionLocator'));

        $di->params['Aura\Sql\ExtendedPdo']['dsn'] = getenv('CONNECTION_DSN');
        $di->params['Aura\Sql\ExtendedPdo']['username'] = getenv('CONNECTION_USERNAME');
        $di->params['Aura\Sql\ExtendedPdo']['password'] = getenv('CONNECTION_PASSWORD');
        $di->params['Aura\SqlQuery\QueryFactory']['db'] = getenv('CONNECTION_TYPE');

        $di->params['Aura\Sql\ConnectionLocator']['default'] = function () use ($di) {
            return $di->get('default_connection');
        };
        $di->params['Aura\SqlMapper_Bundle\Query\ConnectedQueryFactory']['query'] = $di->lazyNew('Aura\SqlQuery\QueryFactory');


I need $di->params['Aura\Sql\ConnectionLocator']['default']  to be a closure. But I am wondering what is the best way to acheive it.

I have tried lazy but it didn't worked as expected.

Thank you.

Hari K T

You can ring me : +91 9388 75 8821

Skype  : kthari85
Twitter : harikt

Shameer C

unread,
Dec 20, 2014, 6:57:46 AM12/20/14
to aur...@googlegroups.com
Hi Hari,

I'm not sure I understand your question correctly, but here is something that worked for me.

$di->set('database', $di->lazyNew(
    'Aura\Sql\ExtendedPdo',
        array(
        'dsn' => 'mysql:dbname=phptoday;host=127.0.0.1',
        'username' => 'root',
        'password' => 'root'
    )
));
$di->set('connection_locator', $di->lazyNew('Aura\Sql\ConnectionLocator'));

$di->params['Aura\Sql\ConnectionLocator']['default'] = function () use ($di) {
    return $di->get('database');
};
$di->params['MyApp\Domain\Services\UserService']['pdo'] = $di->get('connection_locator')->getDefault();

Are you looking for this?

Cheers
Shameer

--
You received this message because you are subscribed to the Google Groups "The Aura Project for PHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to auraphp+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Shameer C
@shameerc | +91-9747 377 001

Brandon Savage

unread,
Dec 20, 2014, 8:42:29 AM12/20/14
to aur...@googlegroups.com
Did you try lazyGet()? It will return a callable. 

Sent from my iPhone
--

Hari K T

unread,
Dec 20, 2014, 9:38:02 PM12/20/14
to aur...@googlegroups.com
Hi Shameer, 
 
I'm not sure I understand your question correctly, but here is something that worked for me.

$di->set('database', $di->lazyNew(
    'Aura\Sql\ExtendedPdo',
        array(
        'dsn' => 'mysql:dbname=phptoday;host=127.0.0.1',
        'username' => 'root',
        'password' => 'root'
    )
));
$di->set('connection_locator', $di->lazyNew('Aura\Sql\ConnectionLocator'));

$di->params['Aura\Sql\ConnectionLocator']['default'] = function () use ($di) {
    return $di->get('database');
};

Yes this is what I am currently doing. But was wondering if Di itself have some sort of way to make it callable.other than 

$di->params['Aura\Sql\ConnectionLocator']['default'] = function () use ($di) {
            return $di->get('default_connection');
        };
 
$di->params['MyApp\Domain\Services\UserService']['pdo'] = $di->get('connection_locator')->getDefault();

Are you looking for this?

Yes, but I want to get rid of using the closure and passing the di object to it :) .

Hari K T

unread,
Dec 20, 2014, 9:56:44 PM12/20/14
to aur...@googlegroups.com
Hey Brandon Savage, 

When you said lazyGet first thought was it will not return callable.

Then I understood you were mentioning to make use of 
$di->params['Aura\Sql\ConnectionLocator']['default'] = $di->lazyGet('Aura\Sql\ExtendedPdo')

But I was making use of service so that the same connection can be used else where.
$di->set('default_connection', $di->lazyNew('Aura\Sql\ExtendedPdo'));

The idea of same connection is when using Aura.Auth and passing the pdo instance etc.

But in the use of $di->lazyGet('Aura\Sql\ExtendedPdo') I don't think it will be making use of same connection. So the best seems the closure itself.

Thanks!
Reply all
Reply to author
Forward
0 new messages