2014-11-02 59 views
0

我使用CakePHP 2.3.4。我有一个数据库表供应商和一个表产品。他们都有很多关系网店。我的问题是,一旦阵列返回给我的供应商,携带一些产品,数组格式是小时髦,我很难妥善解决它。以下是我已经得到关系CakePHP可以可怕阵列

供应商型号:

<?php 
App::uses('AppModel', 'Model'); 

class Vendor extends AppModel { 



/** 
* belongsTo associations 
* 
* @var array 
*/ 
    public $actsAs = array('Search.Searchable'); 

    public $filterArgs = array(
     array('name' => 'vendor_name', 'type' => 'query','method'=>'filterVendor'), 
     array('name' => 'is_finalized', 'type' => 'string'), 

    ); 

    public $belongsTo = array(
     'Product' => array(
      'className' => 'Product', 
      'foreignKey' => 'product_id', 
      'conditions' => '', 
      'fields' => '', 
      'order' => '' 
     ) 
    ); 

/** 
* hasMany associations 
* 
* @var array 
*/ 
    public $hasMany = array(
     'Product' => array(
      'className' => 'Product', 
      'foreignKey' => 'vendor_id', 
      'dependent' => false, 
      'conditions' => '', 
      'fields' => '', 
      'order' => '', 
      'limit' => '', 
      'offset' => '', 
      'exclusive' => '', 
      'finderQuery' => '', 
      'counterQuery' => '' 
     ) 
    ); 

    /* public function __construct($id = false,$table = null,$ids = null) { 
     $this->statuses = array(
      'open' => __('Open',true), 
      'closed' => __('Closed',true), 
     ); 
     $this->categories = array(
      'bug' => __('bug',true), 
      'support' => __('support',true), 
      'technical' => __('technical',true), 
      'other' => __('other',true), 
     ); 
     parent::__construct($id,$table,$ids); 

    } 
*/ 


    public function filterVendor($data = array()) { 
     $filter = $data['vendor_name']; 
     if(empty($filter)) 
     { 
      return array(); 
     } 
     $cond = array(
      'OR' => array(
       $this->alias . '.vendor_name LIKE' => '%' . $filter . '%', 
      )); 
     return $cond; 
    } 

} 

问题是,在我的视图文件,我得到的产品阵列这样

<?php 
     $i = 0; 
     Debugger::dump($vendor['Product']); 
     foreach ($vendor['Product'] as $product): 
      if (!is_null($product['id'])): 
       Debugger::dump($product['id']); 
       ?> 
       <tr> 
        <td><?php echo $product['id']; ?></td> 
        <td><?php echo $product['vendor_id']; ?></td> 
        <td><?php echo $product['sku']; ?></td> 
        <td><?php echo $product['product_name']; ?></td> 
        <td><?php echo $product['product_weight']; ?></td> 
        <td><?php echo $product['product_dimenssions']; ?></td> 
        <td><?php echo $product['product_quantity']; ?></td> 
        <td><?php echo $product['product_quantity_sold']; ?></td> 
        <td><?php echo $product['product_quantity_returned']; ?></td> 
        <td><?php echo $product['product_cost_price']; ?></td> 
        <td><?php echo $product['product_list_price']; ?></td> 
        <td><?php echo $product['product_map_price']; ?></td> 
        <td><?php echo $product['product_special_instructions']; ?></td> 
        <td><?php echo $product['is_fragile']; ?></td> 
        <td><?php echo $product['is_downloadable']; ?></td> 
        <td><?php echo $product['is_always_in_stock']; ?></td> 
        <td class="actions"> 
         <?php echo $this->Html->link(__('View'), array('controller' => 'products', 'action' => 'view', $product['id'])); ?> 
         <?php echo $this->Html->link(__('Edit'), array('controller' => 'products', 'action' => 'edit', $product['id'])); ?> 
         <?php echo $this->Form->postLink(__('Delete'), array('controller' => 'products', 'action' => 'delete', $product['id']), null, __('Are you sure you want to delete # %s?', $product['id'])); ?> 
        </td> 
       </tr> 
      <?php endif; endforeach; ?> 

及以上的调试声明将给我们这

array(
    'id' => '3', 
    'vendor_id' => '6', 
    'sku' => '5327', 
    'product_name' => '4' Bamboo Palm Silk Tree', 
    'product_weight' => null, 
    'product_dimenssions' => '4'x36"x36"', 
    'product_quantity' => null, 
    'product_quantity_sold' => null, 
    'product_quantity_returned' => null, 
    'product_cost_price' => null, 
    'product_list_price' => null, 
    'product_map_price' => null, 
    'product_special_instructions' => '', 
    'is_fragile' => true, 
    'is_downloadable' => false, 
    'is_always_in_stock' => false, 
    'category' => null, 
    'product_category' => null, 
    (int) 0 => array(
     'id' => '3', 
     'vendor_id' => '6', 
     'sku' => '5327', 
     'product_name' => '4' Bamboo Palm Silk Tree', 
     'product_weight' => null, 
     'product_dimenssions' => '4'x36"x36"', 
     'product_quantity' => null, 
     'product_quantity_sold' => null, 
     'product_quantity_returned' => null, 
     'product_cost_price' => null, 
     'product_list_price' => null, 
     'product_map_price' => null, 
     'product_special_instructions' => '', 
     'is_fragile' => true, 
     'is_downloadable' => false, 
     'is_always_in_stock' => false, 
     'category' => null, 
     'product_category' => null 
    ), 
    (int) 1 => array(
     'id' => '4', 
     'vendor_id' => '6', 
     'sku' => '5254', 
     'product_name' => '75" Bamboo Silk Tree', 
     'product_weight' => null, 
     'product_dimenssions' => 'Height: 75 in Width: 33 in Depth: 33 in Pot size: W: 7 in, H: 6 in Trunk type: Natural Number of leaves: 1440', 
     'product_quantity' => null, 
     'product_quantity_sold' => null, 
     'product_quantity_returned' => null, 
     'product_cost_price' => null, 
     'product_list_price' => null, 
     'product_map_price' => null, 
     'product_special_instructions' => '', 
     'is_fragile' => true, 
     'is_downloadable' => false, 
     'is_always_in_stock' => false, 
     'category' => null, 
     'product_category' => null 
    ), 
    (int) 2 => array(
     'id' => '5', 
     'vendor_id' => '6', 
     'sku' => '5250', 
     'product_name' => '7' Bamboo Palm Silk Tree', 
     'product_weight' => null, 
     'product_dimenssions' => 'Height: 7 ft Width: 43 in Depth: 43 in Pot size: W: 7 in, H: 6 in Trunk type: Natural Number of leaves: 1542', 
     'product_quantity' => null, 
     'product_quantity_sold' => null, 
     'product_quantity_returned' => null, 
     'product_cost_price' => null, 
     'product_list_price' => null, 
     'product_map_price' => null, 
     'product_special_instructions' => '', 
     'is_fragile' => true, 
     'is_downloadable' => false, 
     'is_always_in_stock' => false, 
     'category' => null, 
     'product_category' => null 
    ), 
    (int) 3 => array(
     'id' => '6', 
     'vendor_id' => '6', 
     'sku' => '0514', 
     'product_name' => 'Bamboo Square Decorative Planters (Set of 4)', 
     'product_weight' => null, 
     'product_dimenssions' => 'Small Size Dimensions H: 8 in, W: 8 in, D: 8 in Medium Size Dimensions H: 10 in, W: 10 in, D: 10 in Large Size Dimensions H: 12 in, W: 12 in, D: 12 in Extra Large Size Dimensions H: 14 in, W: 13.75 in, D: 13.75', 
     'product_quantity' => null, 
     'product_quantity_sold' => null, 
     'product_quantity_returned' => null, 
     'product_cost_price' => null, 
     'product_list_price' => null, 
     'product_map_price' => null, 
     'product_special_instructions' => '', 
     'is_fragile' => true, 
     'is_downloadable' => false, 
     'is_always_in_stock' => false, 
     'category' => null, 
     'product_category' => null 
    ), 
) 

你看到数组的格式是相当混乱作为实际产品苏b数组不是在一个属性名称下启动的,而是它只是从一个int开始,然后还有一些包含在数组结构中的其他属性。

我的问题是,是这样的东西,蛋糕是做或者我可以解决这个问题。如果我可以如何?使用不同的结构 感谢

+0

_(1)_数据库设计并不代表产品和厂商之间的许多一对多的关系。 @ deepak-bansal答案显示了Cakephp解决方案。在数据库设计中,多对多需要[联结表](http://en.wikipedia.org/wiki/Junction_table)。 _(2)_假设你的数据库设计是正确的,你不能在关系定义中使用同一个别名。参见[每个模型的别名必须是整个应用程序的唯一。(http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#relationship-types)你既别名关系作为产品的产品模型。 – AgRizzo 2014-11-02 15:15:13

回答

4

在CakePHP我们可以实现多对多(HABTM)。对于HABTM需要3个表

  1. 产品
  2. 厂商
  3. products_vendors

而且你需要一个关系定义像

在产品型号

public $hasAndBelongsToMany = array(
    'Vendor' => array(
     'className' => 'Vendor', 
     'joinTable' => 'products_vendors', 
     'foreignKey' => 'product_id', 
     'associationForeignKey' => 'vendor_id', 
     'unique' => 'keepExisting' 
    ) 
); 

在供应商型号

public $hasAndBelongsToMany = array(
    'Product' => array(
     'className' => 'Product', 
     'joinTable' => 'products_vendors', 
     'foreignKey' => 'vendor_id', 
     'associationForeignKey' => 'product_id', 
     'unique' => 'keepExisting' 
    ) 
); 

这会给你适当的结果。

+0

正确! :) +1 – 2014-11-02 15:02:31

+0

hmm..I感到有点咸菜,然后,我就需要创建product_vendors表和我有很多数据 – Autolycus 2014-11-02 15:10:49

+0

但感谢为指向正确的方向,这将工作! – Autolycus 2014-11-02 15:52:42