0

I'm having some problems writing some complex queries using Doctrine methods and i would love to know how to do it, maybe someone can help me.

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 Answer 1

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

Real names of Order and Products relations for Users model can be different (depending on the actual schema).

Sign up to request clarification or add additional context in comments.

6 Comments

Do i need to use Doctrine_Query for the other 2? Can't i use methods?
Also, what does Doctrine_Query returns? An object that i can call fetch, or fetchAll, for example?
You can use methods to create query like UsersTable::getInstance()->createQuery('u') instead of Doctrine_Query::create(), but then you dont need to specify the from clause.
Doctrine_Query returns Doctrine_Query object with initialized query params, and you can call execute(), fetchOne() etc on this object to retrieve data from DB.
GetInstance is equal to Doctrine_Core::getTable('Phonenumber'); ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.