简体   繁体   中英

How to find data through foreign key in cakephp?

 Product        Plan            ProductPlan
 id |name       id |name        id  | product_id | plan_id     
 1    aplha      1   a          1        1          2             
 2    bravo      2   b
 3    charlie 

I want to find Product name and Plan name against ProductPlan, if product id and plan id for product exist in ProductPlan then the name of plan and product will show, I tried a lot, the relation b/w tables are correct but i didn't get the exact data, query which i used that is

        $p_plan = $this->ProductPlan->find('all',array(
                                    'conditions'=>array(
                                                'ProductPlan.product_id'=>'Product.product_id'
                                                )                       
                                        )
                                    );  
    $this->set('p_plan', $p_plan);  

if some body help me , i'll very thaksful to him. Thanks in advance.

Relation For Plan

class Plan extends AppModel{ 

public $primaryKey='plan_id';

public $hasMany = array(
    'ProductPlan'=>array(
        'className'    => 'ProductPlan',
        'foreignKey'   => 'plan_id' 
    )
);

Product

class Product extends AppModel{ 

public $primaryKey='product_id';

public $hasMany = array(
    'ProductsUser'=>array(
        'className'    => 'ProductsUser',
        'foreignKey'   => 'product_id'  
    ),
    'ProductsUserNode'=>array(
        'className'    => 'ProductsUserNode',
        'foreignKey'   => 'product_id'  
    ),
    'ProductPlan'=>array(
        'className'    => 'ProductPlan',
        'foreignKey'   => 'product_id'  
    )
);

for Product Plan

class ProductPlan extends AppModel{
var $primaryKey='product_plan_id';
 public $belongsTo = array(
    'Product' => array(
        'className'    => 'Product',
        'foreignKey'   => 'product_id'
    ),
    'Plan' => array(
        'className'    => 'Plan',
        'foreignKey'   => 'plan_id'
    )       

     );
public $hasMany = array(
    'ProductPlansUserNode'=>array(
        'className'    => 'ProductPlansUserNode',
        'foreignKey'   => 'product_plan_id' 
    ),
);

}

You should simply be able to use 'contain':-

$p_plan = $this->Product->find('all', array(
    'contain' => array('Plan')
));

This will return all your products, with their associated plans. Your Product and Plan models need a hasAndBelongToMany relationship. There is no need to define a model for your joins table.

class Product AppModel {
    ...
    public $hasAndBelongsToMany = array('Plan');
    ...
}

class Plan AppModel {
    ...
    public $hasAndBelongsToMany = array('Product');
    ...
}

As a side note I'd personally avoid overriding CakePHP's default way of handling primary keys. It is better to stick with convention and use id .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM