簡體   English   中英

如何在cakephp中查看數據的相關ID?

[英]How to view the data against it's associated id in cakephp?

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

我怎樣才能做到這一點? 提前致謝。

我是否了解“產品有很多計划,而計划有很多產品”(很多)( 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'
        )
    );
}

並且您需要在“計划”表中添加一個名為“ product_id”的外鍵。

您必須從控制器傳遞三個變量。 該變量包含三個不同的表數據。

比需要時使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM