关系cakephp制造了糟糕的阵列

时间:2014-11-02 14:39:20

标签: cakephp cakephp-2.0 cakephp-2.3

我正在使用cakephp 2.3.4。我有一个数据库表供应商和一个表产品。他们彼此之间都有多对多的关系。我的问题是,一旦为包含某些产品的供应商返回了数组,那个数组格式就不那么时髦了,我很难正确地解决它。这是我得到的

供应商模型:

<?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
    ),
)

你看到数组的格式非常混乱,因为实际的产品子数组不是在属性名称下开始,而是它只是以int开头,然后还有一些其他属性包含在数组的结构中。

我的问题是,蛋糕正在做什么,或者我可以解决这个问题。如果我可以怎么样? 感谢

1 个答案:

答案 0 :(得分:4)

在cakephp中,我们可以使用不同的结构实现多对多(HABTM)。对于HABTM,您需要3个表

  1. 产品
  2. 厂商
  3. products_vendors
  4. 你还需要一个像

    这样的关系定义

    在产品型号中

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

    这会给你正确的结果。