Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find conditions with hasMany model

I have 4 model:

Item------hasMany---->Detail

Item------hasMany---->Favourite

Item------hasMany---->Category

How can I find all Item that has Favourite.member_id = '8' and Category.item_category_id = '1'

Here is my Item model relation:

public $hasMany = array(                
    'Detail' => array(
        'className' => 'Detail',
        'foreignKey' => 'item_id',
        'dependent' => true,
        'conditions' => '',
        'order' => 'created DESC',          
    ),      
    'Category' => array(
        'className' => 'Category',
        'foreignKey' => 'item_id',
        'dependent' => true,
        'conditions' => '',
        'order' => 'created DESC',          
    ),
    'Favorite' => array(
        'className' => 'Favorite',
        'foreignKey' => 'item_id',
        'dependent' => true,
        'conditions' => '',
        'order' => 'created DESC',          
    )

);

I used this query() and it's ok:

$this->set('test',$this->Item->query("SELECT items.*
                                        FROM items, categories, favourites
                                        WHERE items.id = items_item_categories.item_id
                                        AND items.id = favorites.item_id
                                        AND categories.item_category_id = 1 AND favourites.member_id = 8"));

But I don't like using query(), and I change it into find(), and it seems not good:

$this->set('test',$this->Item->find('all', array(
                                                'contain'=>array(
                                                    'ItemDetail',
                                                    'Category'=>array('conditions'=>array('Category.item_category_id'=>1)),
                                                    'Favorite'=>array('conditions'=>array('Favorite.member_id'=>8)));
like image 733
Tam Huynh Avatar asked Apr 27 '12 03:04

Tam Huynh


2 Answers

You need call the containableBehaivor on your model

<?php
class Item extends AppModel {
    public $actsAs = array('Containable');
}

in your controller you can make the query

$items = $this->Item->find('all', array(
           'contain'=>array(
                 'ItemDetail',                                                                
                 'Category'=>array(
                       'conditions'=>array('Category.item_category_id'=>1)
                  ),
                 'Favorite'=>array(
                       'conditions'=>array('Favorite.member_id'=>8)
                  )
              )
          );
$this->set('test', $items);

try using Joins

$items = $this->Item->find('all', array(
            'joins' => array( 
                        array( 
                            'table' => 'categories', 
                            'alias' => 'Category', 
                            'type' => 'inner',  
                            'conditions'=> array('Category.item_category_id' => 1) 
                        ), 
                        array( 
                            'table' => 'favorites', 
                            'alias' => 'Favorite', 
                            'type' => 'inner',  
                            'conditions'=> array('Favorite.member_id' => 8, 'Item.id = Favorite.item_id') 
                            )
                        ),
           'contain'=>array('ItemDetail','Category','Favorite')
          );
$this->set('test', $items);
like image 59
CROWD Avatar answered Nov 15 '22 02:11

CROWD


You could also try something like this:

$this->Item->hasMany['Favorite']['conditions']['member_id'] = 8;

Which has the same effect as rebinding the model with the condition.

Just a possible issue. The above will add the condition for the rest of the request, if you want to rollback to the previous behavior, you need to unset the condition:

unset($this->Item->hasMany['Favorite']['conditions']['member_id']);

Remember to set the $this->Item->recursive property to the desired value (recursive attribute). The default it's 1, which it's the minimum you need for this to work.

like image 3
davidmh Avatar answered Nov 15 '22 01:11

davidmh