使用Doctrine方法编写查询?

时间:2011-05-26 12:07:39

标签: php orm doctrine

我在使用Doctrine方法编写一些复杂查询时遇到了一些问题,我很想知道怎么做,也许有人可以帮助我。

1) What's the equivalent of SELECT * FROM users WHERE user = 'admin' and password = 123456
2) What's the equivalent of SELECT name, title FROM products WHERE category_id = 4 AND title LIKE %something%
3) What's the equivalent of SELECT products.id, users.id FROM users INNER JOIN orders ON users.id = orders.buyer INNER JOIN products ON orders.product = products.id WHERE users.id = 2

1 个答案:

答案 0 :(得分:1)

1)

UsersTable::getInstance()->findByUserAndPassword('admin', 123456);

2)

Doctrine_Query::create()
     ->select('p.name, p.title')
     ->from('Products p')
     ->where('category_id = ? AND title LIKE ?', array(4, '%something%'));

3)

Doctrine_Query::create()
     ->select('p.id, u.id')
     ->from('Users u')
     ->innerJoin('u.Order o')
     ->innerJoin('o.Products p')
     ->where('u.id = ?', 2);

用户模型的订单和产品关系的实名可以不同(取决于实际架构)。