나는 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
),
)
실제 포맷으로 보면 배열의 형식이 엉망입니다. su b 배열은 속성 이름으로 시작하지 않고 그냥 int로 시작하고 배열의 구조에 포함 된 다른 속성이 있습니다.
제 질문은 케이크가하고있는 것이거나 해결할 수있는 것이지요. 내가 어떻게 할 수 있니? 감사합니다
_ (1) _ 데이터베이스 디자인이 제품과 공급 업체 간의 다 대다 관계를 나타내지 않습니다. @ deepak-bansal 대답은 Cakephp 솔루션을 보여줍니다. 데이터베이스 디자인에서, many-to-many는 [접합 테이블] (http://en.wikipedia.org/wiki/Junction_table)을 필요로합니다. _ (2) _ 데이터베이스 설계가 올바른 것으로 가정하면 관계 정의에서 동일한 별명을 사용할 수 없습니다. [각 모델의 별칭은 앱에서 고유해야합니다.] (http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#relationship-types) 두 관계의 별칭을 지정했습니다. 제품으로 제품 모델에. – AgRizzo