如何使用 Symfony 在 Propel 查询中使用 OR 语句

发布于 2024-11-29 18:36:45 字数 420 浏览 1 评论 0原文

现在,下面的代码在数据库中查找author_id和target_object_id,并要求两者匹配,以便通过查询检索。我希望它位于 WHEREauthor_id='x' || target_object_id='y' 而不是 &&默认情况下,这就是下面的代码的作用。

我看过 Propel 和 Symfony 手册,但它们对我没有帮助。

$c = new Criteria();
$c->add("type", GVCCommentPeer::TYPE_PRIVATE_MESSAGE);
$c->add("author_id", $this->myprofileid);
$c->add("target_object_id", $this->profile->getId());

Right now the code below looks for the author_id and the target_object_id in the database and requires both to match, to be retreived by the query. I would like it to be WHERE author_id='x' || target_object_id='y' instead of it being && by default, which is what the code below does.

I've looked on Propel and the Symfony manuals, and they don't help me.

$c = new Criteria();
$c->add("type", GVCCommentPeer::TYPE_PRIVATE_MESSAGE);
$c->add("author_id", $this->myprofileid);
$c->add("target_object_id", $this->profile->getId());

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

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

发布评论

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

评论(1

路弥 2024-12-06 18:36:45

您需要从 Criteria 对象创建 Criterion 对象,并根据您需要的条件合并它们:

//create your Criteria
$c = new Criteria();
$c->add("type", GVCCommentPeer::TYPE_PRIVATE_MESSAGE);

//create 2 Criterion objects
$c1 = $c->getNewCriterion("author_id", $this->myprofileid);
$c2 = $c->getNewCriterion("target_object_id", $this->profile->getId());

// merge the two fields on OR
$c1->addOr($c2);

//bind
$c->add($c1);

$result = YOURPEERHEREPeer::doSelect($c);

You need to create Criterion objects from your Criteria object and merge them on the condition you require:

//create your Criteria
$c = new Criteria();
$c->add("type", GVCCommentPeer::TYPE_PRIVATE_MESSAGE);

//create 2 Criterion objects
$c1 = $c->getNewCriterion("author_id", $this->myprofileid);
$c2 = $c->getNewCriterion("target_object_id", $this->profile->getId());

// merge the two fields on OR
$c1->addOr($c2);

//bind
$c->add($c1);

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