尝试创建一个过滤器来从相关模型中检索数据

发布于 2024-11-01 16:27:00 字数 1155 浏览 0 评论 0原文

我有一个 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)

在这种情况下,所有帖子都有 titlecreatedprice..
但是如果一个帖子 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

在梵高的星空下 2024-11-08 16:27:00

无法搜索 hasMany 关系的子级。您将需要针对 PostFields 模型运行查询。即: $this->PostField->find('all', array('conditions'=>array('PostField.name' => 'weight', 'PostField.value' > 10 )));

如果您想同时对 PostField 和 Post 模型进行查询(即:价格 < $1.00 且重量 > 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));

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:

$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);

EDIT:
I would do this. You're not going to get away with doing a single call, but this is still a clean solution.

$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));
2024-11-08 16:27:00

您应该考虑为您的模型使用 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文