views:

40

answers:

2

Hi there,

I'm trying to use on the fly associations to trim down the data I retrieve, but the model I'm using is associated to other models with a re-named field because I have 2 of the same models associated with it.

So, here's the model, say 'test', that has two 'user' fields, both related to the User model.

In the model:

  var $belongsTo = array( 
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id' 
    ),
    'User_Watched' => array(
        'className' => 'User',
        'foreignKey' => 'user_id_watched' 
    )
);

When I retrieve data related to 'test', I want to only retrieve particular data linked to the 'User' and 'User_Watched' fields without any other nested information.

But when I do:

 $this->User->unbindModel(array('hasMany' => array('something1', 'something2')), false);

then something1 and something2 data does not show up for the 'User' field of model 'test', but is still retrieved for the 'User_watched' field.

Can I not retrieve unwanted data for the 'User_watched' field?

Hope this makes sense... :)

+2  A: 

Hello KcYxA,

to use on the fly associations to trim down the data I retrieve

Good idea.

'foreignKey' => 'user_id_watched' 

should possibly be:

'foreignKey' => 'user_watched_id'.

Kind regards, Benjamin.

Edit 1: At least this would make sense according to my current understanding. If user_id is a correct foreign key(FK), which cakephp uses to unbind the relations, but user_id_watched isn't, than your described behavior is explained.

Edit 2: The Containable behavior gives you another tool for controlling associated models.

benjamin
Thank you, benjamin. Why would it have to be ending with 'id'? the actual field in the 'Test' model is 'user_id_watched.I did solve this using 'joins'.
KcYxA
Hello KcYxA,Cakephp uses the `Convention over Configuration` approach, meaning that if a developer sticks to some conventions, cakephp in exchange reduces the amount of configuration needed. In the case at hand, `user_id_watched` functions as a FK. FK's in cakephp should follow convention, so the fieldname in the table should be `user_watched_id`. Then of course all occurrences of the old name have to be changed accordingly.If this works out for you, please let me know so that I can melt this comment in the answer above.Kind regards, Benjamin.
benjamin
+1  A: 

KcYxA,

Containable behavior might help a lot in this case, as benjamin mentioned, your "find" queries would look like:

$this->User->find('first', array(
      'conditions' => array('User.id' => $id),
      'contain'    => array('UserWatched')
    ));

In this case, you won't have to use unbindModel method. In this example, you'll get User and UserWatched data. If you need only User data from "find", then tell Cake to "$this->User->contain();" so it won't go further then User model.

jujav4ik
Thank you. I ended up using a join.
KcYxA
KcYxA,using "hand-written" SQL should be last resort, as it bypasses the abstraction of the underlying database layer. In other words, a developer, who introduced SQL statements directly, has to rewrite these once he switches to another database/datasource.Kind regards, Benjamin.
benjamin
jujav4ik, it is still written using CakePHP's convention, where one adds a 'join' array to the parameters of the 'find' method. But I understand and agree with your point.
KcYxA