Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...