关系cakephp使数组变得糟糕

关系cakephp使数组变得糟糕,cakephp,cakephp-2.0,cakephp-2.3,Cakephp,Cakephp 2.0,Cakephp 2.3,我使用的是CakePHP2.3.4。我有一个数据库表供应商和一个表产品。他们彼此之间都有多对多的关系。我的问题是,一旦我的供应商提供了一些产品的阵列返回,阵列格式就有点古怪,我很难正确地解决它。这是我得到的 供应商模型: <?php App::uses('AppModel', 'Model'); class Vendor extends AppModel { /** * belongsTo associations * * @var array */ public

我使用的是CakePHP2.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;
    }

}
您可以看到数组的格式非常混乱,因为实际的products子数组不是以属性名称开头的,而是以int开头的,然后还有一些其他属性包含在数组的结构中

我的问题是,这是蛋糕正在做的事情,还是我可以修复它。如果我能怎么做?
感谢

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

  • 产品
  • 卖主
  • 产品供应商
  • 你还需要一种关系,比如

    在产品模型中

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

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

  • 产品
  • 卖主
  • 产品供应商
  • 你还需要一种关系,比如

    在产品模型中

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

    这将为您提供正确的结果。

    hmm..我有点吃力,因为我需要创建产品供应商表,我有很多数据。hmm..我有点吃力,因为我需要创建产品供应商表,我有很多数据(1)您的数据库设计不代表产品和供应商之间的多对多关系@deepak bansal的答案显示了Cakephp解决方案。在数据库设计中,多对多需要一个。(2) 假设数据库设计正确,则不能在关系定义中使用相同的别名。请注意,您已将与产品模型的这两种关系别名为Product。(1)您的数据库设计没有表示产品和供应商之间的多对多关系@deepak bansal的答案显示了Cakephp解决方案。在数据库设计中,多对多需要一个。(2) 假设数据库设计正确,则不能在关系定义中使用相同的别名。请注意,您已将与产品模型的这两个关系都别名为Product。