尝试创建一个过滤器来从相关模型中检索数据
我有一个 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: .. }
现在很容易根据价格
过滤帖子(例如最低
和最高
之间的价格)..
但是,如果我想根据权重
过滤帖子怎么办??
例如,我希望所有权重
大于10的帖子!!
我希望最好在一个查询中实现这一点,可能使用联接或子查询..
我不知道如何在 cakePHP 中做到这一点,所以如果有人有想法,请帮助!
即使有人只是有一个想法但没有细节,这也会有所帮助......
提前致谢!
I have a Post
model which hasMany
PostField
every post can have several fields stored in the post_fields
table..
post_fields
has this structure: (id, post_id, name, value)
posts
table has some common fields for all posts, but any additional fields should be stored in post_fields
table..
I created a search form that is used to filter the posts
when specifying filters for the fields in the posts
table, it works fine..
but I want to make the filter to work even on the other fields found in post_fields
..
I can retrieve the posts first then filter them manually, but i want something more efficient !
EXAMPLE: let's suppose that posts are describing some products..
post (id, title, created, price)
post_fields (id, post_id, name, value)
in this case, all posts have title
, created
and price
..
but if a post (id=3) wants to have a weight
field, we should do that by creating a record in post_fields
, the record should be :
{ id: .. , post_id: 3, name: weight, value: .. }
it's easy now to filter posts according to price
(e.g. price between min
& max
)..
but, what if i want to filter posts according to weight
??
e.g. i want all posts that have weight
greater than 10 !!
I would like to achieve this preferably in one query, using joins maybe or subqueries ..
I don't know how to do that in cakePHP, so if any one has an idea, plz HELP !!
even if someone just has an idea but doesn't have details, that could help ...
thanx in advance !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无法搜索 hasMany 关系的子级。您将需要针对 PostFields 模型运行查询。即: $this->PostField->find('all', array('conditions'=>array('PostField.name' => 'weight', 'PostField.value' > 10 )));
如果您想同时对 PostField 和 Post 模型进行查询(即:价格 < $1.00 且重量 > 10,您将需要这样做自定义查询,因为 CakePHP 没有内置解决方案,TMK 应该如下所示:
编辑:
我会这样做。您无法通过一次调用就逃脱惩罚,但这仍然是一个干净的解决方案。
There is no way to search against the children of a hasMany relationship. You will need to run your query against the PostFields model. ie:
$this->PostField->find('all', array('conditions'=>array('PostField.name' => 'weight', 'PostField.value' > 10)));
If you want to do a query against both the PostField and Post models at the same time (ie: price < $1.00 and weight > 10, you will need to do a custom query, as CakePHP has no built-in solution for doing so TMK. Should look something like this:
EDIT:
I would do this. You're not going to get away with doing a single call, but this is still a clean solution.
您应该考虑为您的模型使用 Containable 行为。这样,您就可以根据需要过滤返回的列。 (我认为这就是你想要做的过滤类型)
You should look into using the Containable behavior for your models. This way, you can filter the returned columns as you like. (I think this is the type of filtering you want to do)