This is the table for the model:
CREATE TABLE IF NOT EXISTS `SomeModel` (
`id` int NOT NULL AUTO_INCREMENT,
`parent_id` int NOT NULL
)
My goal is to be able to query a model with its siblings using:
SomeModel::model()->with('siblings')->findByPk($id);
Here is my current attempt at the relation:
public function relations()
{
return array(
'siblings' => array(self::HAS_MANY, 'SomeModel', array('parent_id'=>'parent_id')),
);
}
The problem is that I can’t find a way to create a condition so that the model itself isn’t returned along with it’s siblings in the $model->siblings array.
Any thoughts would be great.
Thanks!
Change your relation to this:
Edit: Some explanation, in the documentation for
relation(), we can specify extra options for the join that takes place and these additional options:Plus the default alias for the table is
thence uset.id.Edit: from the comments:
Implementing lazy loading, the way you want it, will be tough to accomplish(I don’t know how, not sure if possible either), however i can suggest making the current code better, by
using named scopes, use a scope when you are doing eager loading, and add the condition
siblings.id!=t.idin the scope:Do eager loading with scope:
This will remove the error with lazy loading
$model->siblingsAlthough the error of lazy loading will be removed you will still be getting the current record, to counter that you can add and use a function of the model which will load the related records without the current one, but ofcourse you won’t be using
$model->siblings, and instead have something like:$model->getLazySibs();