Reputation: 55
I'm using CakePHP for my project... To speed up my code I only want to get some types of models which are related to my actual object:
$result = $this->Category->find(
'first',
array(
'conditions' => array(
'Category.folder' => $path[0]
),
'contain' => array('Rubric',
'conditions' => array('Rubric.category_id' => 'Category.id'),
'contain' => array('Subrubric',
'conditions' => array('Subrubric.rubric_id' => 'Rubric.id')
)
)
)
);
path[0] is the param from the url...
The category and rubrics are found but the subrubrics aren't. There are also entries related to my object, but I want them in my rubric-view not in the category-view.
The model relations:
Category:
public $hasMany = array(
'Rubric' => array(
'className' => 'Rubric',
'foreignKey' => 'category_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => 'title',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
Rubric:
public $hasMany = array(
'Entrieslocation' => array(
'className' => 'Entrieslocation',
'foreignKey' => 'rubric_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'Subrubric' => array(
'className' => 'Subrubric',
'foreignKey' => 'rubric_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
Here I don't want the Entrylocations...
Upvotes: 3
Views: 4656
Reputation: 27382
you can also use one more alternative.
$this->ModelName->unbindModel(array
(
'hasMany' => array
(
'OtherModel'
)
));
$this->ModelName->bindModel(array
(
'belongsTo' => array
(
'SomeOtherModel' => array
(
'foreignKey' => false,
'conditions' => array
(
'ModelName.id = SomeOtherModel.foreignKey'
)
)
)
));
Upvotes: 0
Reputation: 6047
See this: Containing deeper associations
You shouldn't use node 'contain' in the deeper relations. Your code should look like this.
$result = $this->Category->find(
'first',
array(
'conditions' => array(
'Category.folder' => $path[0]
),
'contain' => array('Rubric',
'conditions' => array('Rubric.category_id' => 'Category.id'),
'Subrubric' => array(
'conditions' => array('Subrubric.rubric_id' => 'Rubric.id')
)
)
)
);
Upvotes: 4