Hi everyone,
I’ve got an issue in defining relational condition.
The schema is quite simple. Parent HAS_MANY Child.
parent
id
name
...
child
id
parent_id
name
birthday
...
I wanted to define a HAS_MANY relation of "siblings" for Child model. At first it seems quite simple to me. "You just have to find the children sharing the same parent, with the IDs different from that of the child itself."
// Child.php
public function relations()
{
return array(
'parent' => array(self::BELONGS_TO, 'Parent', 'parent_id'),
'siblings' => array(self::HAS_MANY, 'Child',
array('parent_id' => 'parent_id'),
'condition' => 't.id != siblings.id',
'order' => 'siblings.birthday',
),
);
}
But I’ve got an error saying
SQLSTATE[42S22]: Column not found: 1054 Unknown column 't.id' in 'where clause'.
The error occured in a lazy loading scenario in which tables are not joined.
(I haven’t tested it in eager loading yet.)
Now I’m stacked.
Any suggestions will be appreciated.
[EDIT]
I have defined a named scope with parameters and using it for the moment.
public function siblings($parent, $self)
{
$this->getDbCriteria()->mergeWith(array(
'condition' => 'parent_id = :parent_id and id != :self',
'params' => array(':parent_id' => $parent, ':self' => $self),
'order' => 't.birthday',
));
return $this;
}
But can I define the "siblings" relation?