views:

26

answers:

3

I have a rather simple data model, but I have a centric entity called "item" which is associated to 7 other entities via 1-to-1 / 1-to-many relationships. Everything works smooth. But in most actions that work with the item model, I dont need any of the associations from the item Model - so mysql ends up issuing a many unnecessary queries (because of the association defined in the "item" Model class).

I'm trying to think what's the best way to minimize those unnecessary queries- I'm thinking to remove all associations from the Model classes, make all the Models Containable, and create associations in runtime where required.

Another option is to simply use bindModel / unbindModel wherever it makes sense.

I'm not really sure what's which approach is better, and if there's another, more suitable way to minimize those unnecessary mysql queries.

Any feedback would be great :)

+1  A: 

Before issuing the find() set the recursive property to 0:

$this->Model->recursive = 0;
matiasf
But sometimes I want only some of the associations. Not none of them.
azv
A: 

I would use unbindModel() first. Then I would use Containable behavior only in those controllers or models where it is not enough to use only unbindModel(). Sometimes it is not enough to use bind/unbind or containable to minimize the number of queries. Then you can use 'joins' option in find() queries.

bancer
+1  A: 

Don't use recursive. Don't remove your associations.

In app_model.php add (at class level):

var $actsAs = array('Containable');

Then use

$this->MyModel->contain();

or

$this->MyModel->contain(array('AssocThing.field','OtherThing.SubThng.field'));

to control the tables and fields you retrieve.

Containable will give you fine-grained control of the data you retrieve. That's what it's for!

Leo