I have 3 db tables: books(id, title, author,…) and orders(id, book_id, user_id,…) and users(id, name, username,…) and I would like to get the titles of the books ordered by given user.
I prepared query using query() method:
$this->set('user_orders', $this->Order->query("SELECT orders.id, orders.status,
(SELECT books.title FROM books WHERE books.id = orders.book_id) as `titles`
FROM orders WHERE orders.user_id = ".$this->Auth->user('id').""));
Now, I would like to obtain the same result but using find() method:
$this->set('user_orders', $this->Order->find('all', array(
'fields' => array(
'Order.id',
'status',
'Order.book_id',
'Book.title' => $this->Book->find('first',array(
'fields' => 'Book.title',
'conditions'=> array('Book.id = Order.id')
)),
),
'conditions' => array('user_id' => $this->Auth->user('id')))));
However, it does not work. How it should be corrected to obtain the same effect like in code above?
Greetings
You might want to read up on the ContainableBehavior for this: http://book.cakephp.org/1.3/en/view/1323/Containable.
And, as Kaklon suggested: please be more specific about what is/isn’t working…