我使用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開始,然後還有一些包含在數組結構中的其他屬性。
我的問題是,是這樣的東西,蛋糕是做或者我可以解決這個問題。如果我可以如何?使用不同的結構 感謝
_(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