一个模型属于另一个模型。我需要在子模型中应用“查找后”过滤器,所以我尝试这样做:
class Parent extends \lithium\data\Model
{
public $hasMany = array(
'Childs' => array(
'to' => 'app\models\Child',
'key' => array('parent_id' => 'parent_id'),
),
);
}
// ...
class Child extends \lithium\data\Model
{
protected $_meta = array(
'source' => 'child',
'key' => 'child_id',
);
public $belongsTo = array(
'Parent' => array(
'to' => 'app\models\Parent',
'key' => 'parent_id',
)
);
}
Child::applyFilter('find', function($self, $params, $chain)
{
$entity = $chain->next($self, $params, $chain);
if ( is_object($entity) )
{
$entity->notes = empty($entity->notes) ? array() : unserialize($entity->notes);
}
return $entity;
});
然后我尝试找到所有父母,Parent::all(array('with' => 'Child', 'conditions' => $conditions));
并且过滤器不适用:(可以做什么?