【问题标题】:How to view the data against it's associated id in cakephp?如何在 cakephp 中根据关联的 id 查看数据?
【发布时间】:2013-02-19 05:59:51
【问题描述】:

表格

    Tables

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

我想根据它的相关 ID 查看数据,例如,如果产品有两个计划,它将在该产品的列表中。像这样

      alpha  |  delta   |  
       a          c
       b

查看代码是

  <table>
<thead>
    <tr>
        <?php foreach ($p as $ps){?>
        <th>
            <?php echo __l($ps['Product']['name']);?>
        </th>
        <?php }?>
    </tr>   
</thead>    
    <tbody>
        <?php foreach ($p as $p1){
                foreach($p1['Plan'] as $plan){
                        debug($plan);
        ?>
        <tr>
            <td>
                <?php echo __l($plan['name']);?>    
            </td>
        </tr>           
        <?php }
                    }?>

    </tbody>

当我调试 $p1 数组时,

 array(
'Product' => array(
    'product_id' => '1',
    'name' => 'Event Manager',
    'slug' => 'event-manager',
    'is_visible' => true
),
'Plan' => array(
    (int) 0 => array(
        'plan_id' => '1',
        'name' => 'FREE',
        'description' => '30 Days Trial',
        'amount' => '0.00',
        'expiry_days' => '30',
        'created' => '2012-01-12 16:51:21',
        'modified' => '2012-01-12 16:51:22',
        'PlansProduct' => array(
            'plan_product_id' => '1',
            'product_id' => '1',
            'plan_id' => '1'
        )
    ),
    (int) 1 => array(
        'plan_id' => '2',
        'name' => 'BRONZE',
        'description' => '60 Days Trial',
        'amount' => '10.00',
        'expiry_days' => '60',
        'created' => '2012-01-12 16:52:24',
        'modified' => '2012-01-12 16:52:25',
        'PlansProduct' => array(
            'plan_product_id' => '2',
            'product_id' => '1',
            'plan_id' => '2'
        )
    )
)

)

我该怎么做?提前致谢。

【问题讨论】:

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


    【解决方案1】:

    我是否理解“产品有很多计划,计划有很多产品”(多对多)(http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm)。在这种情况下,您必须在模型中声明这种关系。

    //models/product.php
    class Product extends AppModel{
        public $hasAndBelongsToMany = array(
            'Plan' =>
                array(
                    'className'              => 'Plan',
                    'joinTable'              => 'ProductPlan',
                    'foreignKey'             => 'id',
                    'associationForeignKey'  => 'id',
                    'unique'                 => true,
                    'conditions'             => '',
                    'fields'                 => '',
                    'order'                  => '',
                    'limit'                  => '',
                    'offset'                 => '',
                    'finderQuery'            => '',
                    'deleteQuery'            => '',
                    'insertQuery'            => ''
                )
        );
    }
    

    如果关系是“一对多”,则声明如下:

    class Product extends AppModel {
        public $hasMany = array(
            'Plan' => array(
                'className'     => 'Plan',
                'foreignKey'    => 'product_id'
            )
        );
    }
    

    您需要在“Plan”表中添加一个名为“product_id”的外键。

    【讨论】:

    • 但问题是如何设置我想要的视图
    • @usii 在控制器中选择它,使用 $this->render("nameofview");
    【解决方案2】:

    您必须从控制器传递三个变量。 该变量包含三个不同的 Table 数据。

    比需要时使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多