我可以在“可容纳”中使用子查询吗?健康)状况?

发布于 2024-12-07 08:41:13 字数 1690 浏览 0 评论 0原文

在我的 CakePHP 中,我有 ModelA,其中有许多 ModelB。 ModelB 有一个 int 值 Q。

我可以查询 ModelA 并使用 containsable 来确保只有那些具有 Q 最大值的 ModelB 记录吗?

我已经尝试过这个:

$this->ModelA->contain(array(
    'ModelB.Q =(SELECT MAX(ModelB.Q) FROM modelb ModelB WHERE ModelA_id = ' . $id . ')'
));

但是它会抛出一个 MySQL 错误,因为 CakePHP 将该等式运算符的右侧解释为一个字段(至少我认为这就是原因),因此用点表示它。

... WHERE `Draw`.`round` =.(SELECT MAX.(`Draw`.`round`) ...

有办法做到这一点吗?如果可能的话,我宁愿不必进入 $query() 模式。


编辑好的,在尝试遵循 api55 建议的页面上的建议后,我有以下代码:

$dbo = $this->Tournament->getDataSource();
$conditionsSubQuery['"Draw"."tournament_id"'] = $id;
$maxRounds = $dbo->buildStatement(array(
    'fields' => array('MAX(Draw.round) AS prevRound'),
    'table' => $dbo->fullTableName($this->Tournament->Draw),
    'alias' => 'Draw',
    'limit' => null,
    'offset' => null,
    'joins' => array(), 
    'conditions' => $conditionsSubQuery,
    'order' => null,
    'group' => null
    ),
    $this->Tournament
);
$maxSubQuery = ' "Draw"."round" = (' . $maxRounds . ') ';
$maxSubQueryExpression = $dbo->expression($maxSubQuery);
$this->Tournament->contain(array(
    'Entrant.selected = 1',
    $maxSubQueryExpression
));
$tournament = $this->Tournament->read(null, $id);

但是当它运行时,它给了我 7 个通知/警告。前 6 个与传递的对象而不是字符串有关:

preg_match() 期望参数 2 为字符串,给定对象

并且有 6 个变体:

类 stdClass 的对象到字符串转换

最后一个不太清楚:

模型“锦标赛”与模型“”没有关联

我怀疑我太愚蠢了,但我们就这样吧。

In my CakePHP I have ModelA which hasMany ModelB. ModelB has an int value Q.

Can I query ModelA and use containable to ensure that only those ModelB records with the maximum value for Q?

I've tried this:

$this->ModelA->contain(array(
    'ModelB.Q =(SELECT MAX(ModelB.Q) FROM modelb ModelB WHERE ModelA_id = ' . $id . ')'
));

But it throws a MySQL error because CakePHP interprets the right hand side of that equality operator as a field (at least I think that's why) and so dots it.

... WHERE `Draw`.`round` =.(SELECT MAX.(`Draw`.`round`) ...

Is there a way to do this? I'd prefer not to have to drop down into $query() mode, if at all possible.


EDIT OK, after trying to follow the advice on the page that api55 suggested, I have this code:

$dbo = $this->Tournament->getDataSource();
$conditionsSubQuery['"Draw"."tournament_id"'] = $id;
$maxRounds = $dbo->buildStatement(array(
    'fields' => array('MAX(Draw.round) AS prevRound'),
    'table' => $dbo->fullTableName($this->Tournament->Draw),
    'alias' => 'Draw',
    'limit' => null,
    'offset' => null,
    'joins' => array(), 
    'conditions' => $conditionsSubQuery,
    'order' => null,
    'group' => null
    ),
    $this->Tournament
);
$maxSubQuery = ' "Draw"."round" = (' . $maxRounds . ') ';
$maxSubQueryExpression = $dbo->expression($maxSubQuery);
$this->Tournament->contain(array(
    'Entrant.selected = 1',
    $maxSubQueryExpression
));
$tournament = $this->Tournament->read(null, $id);

But when it runs, it gives me 7 notice/warnings. The first 6 are to do with an object being passed instead of a string:

preg_match() expects parameter 2 to be string, object given

And 6 variations on this:

Object of class stdClass to string conversion

The last is less clear:

Model "Tournament" is not associated with model ""

I suspect I'm being colossally stupid, but there we go.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

菊凝晚露 2024-12-14 08:41:13

包含使用条件作为普通查找,可以生成子查询并将其放入条件中。所以你也应该能够做到这一点。尝试此处中的子查询部分,并告诉我它是如何进行的;)

这种为条件生成子查询的方法不应失败:D,因为这是 cakephp 的方法。
如果您遇到错误或有问题,请评论答案,看看我是否可以提供帮助。

The contain uses conditions as a normal find, a subquery can be generated and put in conditions. So you should be able to do this as well. Try the subquery part in here and tell me how did it go ;)

This way of generating subqueries for conditions shouldn't fail :D since is the cakephp way.
If you got an error or something comment the answer to see if i can help.

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