我有一个Post
模型hasMany
PostField
每个帖子都可以在post_fields
表中存储多个字段..
post_fields
具有以下结构:(id, post_id, name, value)
posts
表包含所有帖子的一些公共字段,但任何其他字段都应存储在post_fields
表中..
我创建了一个用于过滤帖子的搜索表单
为posts
表格中的字段指定过滤器时,它可以正常工作。
但是我想让过滤器在post_fields
..
我可以先检索帖子然后手动过滤,但我想要更有效率的东西!
示例:我们假设帖子正在描述某些产品..
post (id, title, created, price)
post_fields (id, post_id, name, value)
在这种情况下,所有帖子都有title
,created
和price
..
但是如果帖子(id = 3)想要weight
字段,我们应该通过在post_fields
中创建记录来做到这一点,记录应该是:
{ id: .. , post_id: 3, name: weight, value: .. }
现在很容易根据price
过滤帖子(例如min
和max
之间的价格)..
但是,如果我想根据weight
过滤帖子怎么办?
例如我想要weight
大于10的所有帖子!!
我想在一个查询中实现这一点,使用连接可能或子查询。
我不知道如何在cakePHP中做到这一点,所以如果有人有想法,请帮助!!
即使有人只是有想法但没有细节,这可能会有所帮助......
提前完成!
答案 0 :(得分:2)
无法搜索hasMany关系的子项。您需要针对PostFields模型运行查询。即:$this->PostField->find('all', array('conditions'=>array('PostField.name' => 'weight', 'PostField.value' > 10)));
如果你想同时对PostField和Post模型进行查询(即:price< $ 1.00 and weight> 10,你需要进行自定义查询,因为CakePHP没有内置这样做的解决方案TMK。应该看起来像这样:
$query = "SELECT ... FROM posts as Post, post_fields as PostField WHERE PostField.name = 'weight' AND PostField.value > 10 AND POST.price < 1.0 AND PostField.post_id = Post.id;"
$posts = $this->Post->query($query);
修改强> 我会这样做的。你不会单独打电话,但这仍然是一个干净的解决方案。
$postIds = null;
if(/*we need to run query against PostFields*/) {
$conditions = array(
'OR' => array(
array(
'AND' => array(
'PostField.name' => 'weight',
'PostField.value' > 10
)
),
array(
'AND' => array(
'PostField.name' => 'height',
'PostField.value' < 10
)
)
)
);
$fields = array('PostField.id', 'PostField.post_id');
$postIds = $this->Post->PostField->find('list', array('conditions'=>$conditions, 'fields'=>$fields));
}
$conditions = array('Post.price' < 1.0);
if($postIds) {
$conditions['Post.id'] = $postIds;
}
$posts = $this->Post->find('all', array('conditions'=>$conditions));
答案 1 :(得分:0)
您应该考虑使用模型的Containable行为。这样,您可以根据需要过滤返回的列。 (我认为这是你想要做的过滤类型)