我尝试过并尝试过,但我不知道自己做错了什么。我有一个简单的场景,我有Project hasMany 标签和标签 belongsTo 项目。
当我显示所有当前项目的列表时,我想显示每个项目的标签列表。使用hasMany关系可以很好地获取标记,但我希望在用户单击任何标记时调整查找条件,只返回包含所单击标记的项目。
据我所知,我无法直接在find的conditions选项中操作hasMany绑定,而且我也明白我需要使用 unbindModel()和 bindModel() 设置临时 hasOne 关系的方法,其中 Project hasOne Tag 。我以为我需要遵循以下过程:
这是我在Project.php模型文件中的关系:
public $hasMany = array(
'Link' => array(
'className' => 'Link',
'foreignKey' => 'project_id',
'dependent' => false,
),
'Pledge' => array(
'className' => 'Pledge',
'foreignKey' => 'project_id',
'dependent' => false,
),
'ProjectPost' => array(
'className' => 'ProjectPost',
'foreignKey' => 'project_id',
'dependent' => false,
),
'Tag' => array(
'className' => 'Tag',
'foreignKey' => 'project_id',
'dependent' => false,
)
);
然后在我的Tag.php模型文件中:
public $belongsTo = array(
'Project' => array(
'className' => 'Project',
'foreignKey' => 'project_id',
)
);
所有模型都使用可包含的行为。现在,在我的ProjectsController.php文件中,我正在修改绑定以实现hasOne关系,以便我可以根据Tag.name值操作结果:
//我正在取消绑定hasMany标记关系 $ this-> Project-> unbindModel(array('hasMany'=> array('Tag')));
// I am setting up a hasOne relationship where Project hasOne Tag
$this->Project->bindModel(array(
'hasOne' => array(
'MyTag' => array(
'className' => 'Tag',
'foreignKey' => false,
'conditions' => array('MyTag.project_id = Project.id')
)
)
));
然后我发出一个简单的查找命令(标签上没有过滤器):
$projects = $this->Project->find(
'all',
array(
'contain' => array(
'MyTag',
'User.id'
),
//'conditions' => $filters ? $filters : NULL,
'fields' => array(
'Project.id', 'Project.name', 'Project.description'
),
)
);
现在我在数据库中只有一个项目记录,但它有五个标记。发生的事情是它返回ONE项目,但是5次,这是结果的pr():
Array
(
[0] => Array
(
[Project] => Array
(
[id] => 1
[name] => Test Project 01
[description] => Donec enim lacus
)
[User] => Array
(
[id] => 1
)
)
[1] => Array
(
[Project] => Array
(
[id] => 1
[name] => Test Project 01
[description] => Donec enim lacus
)
[User] => Array
(
[id] => 1
)
)
[2] => Array
(
[Project] => Array
(
[id] => 1
[name] => Test Project 01
[description] => Donec enim lacus
)
[User] => Array
(
[id] => 1
)
)
[3] => Array
(
[Project] => Array
(
[id] => 1
[name] => Test Project 01
[description] => Donec enim lacus
)
[User] => Array
(
[id] => 1
)
)
[4] => Array
(
[Project] => Array
(
[id] => 1
[name] => Test Project 01
[description] => Donec enim lacus
)
[User] => Array
(
[id] => 1
)
)
)
最奇怪的是,如果我在 MyTag.name 字段上启用过滤器(注意我使用 MyTag ,因为我在绑定期间更改了类名) ,它按预期工作:
$projects = $this->Project->find(
'all',
array(
'contain' => array(
'MyTag',
'User.id'
),
'conditions' => array('MyTag.name' => 'NGO'),
'fields' => array(
'Project.id', 'Project.name', 'Project.description'
),
)
);
以上只返回一个结果。 发生了什么?如何在初始(非过滤)加载时防止这种重复?
提前致谢。
答案 0 :(得分:0)
我认为你应该看看hasAndBelongsToMany关系。 这就是我设置标签行为的方式。 祝你好运!
修改强>
你可以做到
$this->Project->find('first', array(
'conditions' => array('Project.id' => $projectId),
'recursive' => 1
));
// returns
Array [
Project => Array [...],
Tag => Array [all associated tags]
]
和
$this->Project->Tag->find('first', array(
'conditions' => array('Tag.id' => $tagId),
'recursive' => 1
));
// returns
Array [
Tag => Array [...],
Project => Array [all associated projects]
]
这不是你想要的吗?