如何将此MySQL语句转换为symfony Propel? [英] How to convert this MySQL statement to symfony Propel?

查看:75
本文介绍了如何将此MySQL语句转换为symfony Propel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是MySQL:

SELECT *
FROM tbl1
WHERE name > (
  SELECT name
  FROM tbl1
  WHERE id = 3
)
ORDER BY name

这是我对Propel代码的最佳猜测,但它不起作用:

Here's my best guess at the Propel code but it's not working:

$c = new Criteria();
$c->addAscendingOrderByColumn(CartPeer::ITEM_NAME);
$c->add(CartPeer::CATEGORY, $c->add(CartPeer::ITEM_ID, $item->getItemId()), Criteria::GREATER_THAN);

在回复这个问题

推荐答案

Propel没有 standard 方法作为子条件的一部分来进行子查询.

Propel doesn't have a standard way of doing subqueries as part of a criteria.

您可以将查询分开(先获取要与之进行比较的值,然后将其用于原始查询),或者在推进式查询中对子查询使用条件.

You can either separate your query (obtain the value you want to compare against first and then use it into the original query) or use a CUSTOM criteria with your subquery in your propel query.

这是第二个选项的示例:

Here's an example of the second option:

$c = new Criteria();

$subSelect = "cart.category > (
  SELECT cart.category
  FROM carts
  WHERE carts.id = 3)";

$c->add(CartPeer::CATEGORY, $subSelect, Criteria::CUSTOM);

编辑:这是第一个选项的示例

Here's an example of the first option

// find the object we want to compare against
$c = new Criteria();
$c->add(CartPeer::ID, 3); 
$cart = CartPeer::doSelectOne($c)

// then make the actual criteria
$c = new Criteria();
$c->add(CartPeer::CATEGORY, $cart->getCategory(), Criteria::GREATER_THAN)

此选项的唯一问题是您要进行两个查询而不是一个查询,这可能会影响您的性能,但这当然取决于您的应用程序.

The only problem with this option is that you are making two queries instead of one, which may hit on your performance, but it depends on your application of course.

这篇关于如何将此MySQL语句转换为symfony Propel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆