【问题标题】:How to find data through foreign key in cakephp?cakephp中如何通过外键查找数据?
【发布时间】:2013-02-02 17:54:35
【问题描述】:
 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 

我想根据 ProductPlan 查找产品名称和计划名称,如果 ProductPlan 中存在产品的产品 ID 和计划 ID,那么将显示计划和产品的名称,我尝试了很多,关系 b/w 表是正确的但是我没有得到确切的数据,我使用的查询是

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

如果有人帮助我,我会非常感谢他。提前致谢。

关系 对于计划

class Plan extends AppModel{ 

public $primaryKey='plan_id';

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

产品

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

产品计划

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

}

【问题讨论】:

    标签: php cakephp cakephp-1.3 cakephp-2.0 cakephp-2.1


    【解决方案1】:

    您应该能够简单地使用“包含”:-

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

    这将退回您的所有产品及其相关计划。您的 Product 和 Plan 模型需要 hasAndBelongToMany 关系。无需为您的连接表定义模型。

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

    作为旁注,我个人会避免覆盖 CakePHP 处理主键的默认方式。最好遵守约定,使用id

    【讨论】:

    • 我用过,但是产品在重复,例如如果产品(alpha)有两个计划(a和b),那么aplha自己重复了。
    • 我希望如果产品是一个并且计划与计划相悖,因此它写为第一个产品然后产品针对计划
    • 不确定我是否关注你。您的意思是您希望您的所有产品都返回相关的计划吗?类似于 $this->Product->find('all', array('contain'=>array('Plan')).
    • 但我认为产品和计划来自 productplan 表,因为 product_id 和 Plan_id 在 ProductPlan 表中。
    • 您的 ProductPlan 应该是一个连接表,因此您的 Product 和 Plan 模型应该使用彼此的 hasAndBelongsToMany 关系。您不需要直接使用连接表。
    猜你喜欢
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 2021-05-20
    • 2019-06-18
    • 1970-01-01
    • 2019-09-04
    • 2015-08-16
    相关资源
    最近更新 更多