Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

cakephp - 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)));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...