Lazy Loading Is Evil

Hey guys,

don’t you think that AR lazy loading approach is evil?

Isn’t it great that you don’t have to load it lazily, then? :)

I’d prefer to get an exception.

Here’s the example.

Controller:


$list = MyModel::model()->with(array('rel1', 'rel2', 'rel3', 'rel5'))->findAll();

View:


<? foreach ($list as $item): ?>

<tr>

    <td><?= $item->rel1->name ?></td>

    <td><?= $item->rel2->name ?></td>

    <td><?= $item->rel3->name ?></td>

    <td><?= $item->rel4->name ?></td>

    <td><?= $item->rel5->name ?></td>

</tr>

<? endforeach ?>

As you have probably noticed, I forgot to add ‘rel4’ to ‘with’.

And, because of my forgetfulness, bad things will happen.

And that brings me back to my original point: lazy loading is evil :)

eager loading and lazy loading both have their usage scenarios . if you always use eager loading that may cause memory and time waste . some time you don’t need the “join” (you see the underlying story about the “with” :lol: , “with” actually action in mysql(or some other DBMS) is JOIN table ,the JOIN action itself is need memory and time ! )

Yeah, but lazy loading is less controllable.

If I want to use extra query , I prefer to do It manually :)




$project = Project::model()->findByPk($pid);

$company = Company::model()->find('projectId = :pid', array(':pid' => $company->projectId));



There’s no need to use lazy loading for this.

hmm. lazy loading. yeah it is a bit evil. i also prefer getting the exception.

Yeah, it could be evil, but surely is convenient for a lazy programmer like me.

Usually I would start with lazy loading and will switch to eager loading if I think it’s necessary. And, what I have to do then is just adding ‘with’.

If referencing unloaded relation would have resulted in exception, it would be cumbersome to change loading manner between lazy and eager.

A graph of related objects might be needed by a complex piece of business logic which performs some calculation based on related data. Business logic is encapsulated in model (as it should be) and the controller must not know its details. Now, how would the controller know what relations have to be loaded?

Performance isn’t always an issue. There are scenarios where some specific functionality of application (admin backend) or the whole application (intranet app) is available only for a few authorized users so database server is not likely to be overloaded.

In a perfect world - yes.

In a real world we will have to move all the eager queries, like with(‘rel1’, …, ‘relN’), to the model, right? Otherwise the controller will know the details about relations.

Speaking about business logic, we should create some methods like MyModel::getAllNeededData() and use only these methods.

And once we have done it, there’s no difference on how the relations are loaded - using eager MyModel::model()->with(‘rel’)->find or lazy MyModel::model()->find.

You’re making me sad :)

Yes, I opinion it’s a real evil too B)

Greeting Eager Loading ;)

(Here good article)