dqy0707 2013-02-18 10:20
浏览 25
已采纳

如何在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' 
    ),
);

}

  • 写回答

1条回答 默认 最新

  • dongping1689 2013-02-18 11:09
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器