How to convert this MySQL statement to symfony Propel? [closed]

亡梦爱人 提交于 2019-12-11 04:42:12

问题


Here's the MySQL:

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

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

In respond to this question


回答1:


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

EDIT: 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.



来源:https://stackoverflow.com/questions/15148913/how-to-convert-this-mysql-statement-to-symfony-propel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!