我的清单中没有任何内容

时间:2014-09-04 16:15:21

标签: php cakephp

我有一些列表,但没有一个列表被填充。我使用与我在其他控制器中完成的填充列表完全相同的方法,没有任何问题。

这是我的控制器的添加功能:

public function add() {
    if ($this->request->is('post')) {
        $this->ConsumerProduct->create();
        if ($this->ConsumerProduct->save($this->request->data)) {
            $this->Session->setFlash(__('The consumer product has been saved.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The consumer product could not be saved. Please, try again.'));
        }
    }
    $products = $this->ConsumerProduct->Product->find('list');
    $legacyProducts = $this->ConsumerProduct->LegacyProduct->find('list');
    $unitStatuses = $this->ConsumerProduct->UnitStatus->find('list');
    $addresses = $this->ConsumerProduct->find('list', 'Address.id');
    $this->set(compact('products', 'legacyProducts', 'unitStatuses', 'addresses'));
}

有没有人知道可能导致问题的原因?

此前我将$addresses = $this->ConsumerProduct->find('list', 'Address.id');设置为$addresses = $this->ConsumerProduct->Address->find('list');,但这给了我以下错误:

Error: Call to a member function find() on a non-object

这有关系吗?

编辑:

关系如下:

ConsumerProduct belongsTo Product, LegacyProduct, UnitStatus, Address

Address hasMany ConsumerProduct

LegacyProduct hasMany ConsumerProduct

UnitStatus hasMany ConsumerProduct

Address hasMany ConsumerProduct

Product与其中的ConsumerProduct模型

无关

编辑2:

Address.php

<?php
App::uses('AppModel', 'Model');
/**
 * Address Model
 *
 * @property User $User
 * @property Company $Company
 * @property ConsumerProduct $ConsumerProduct
 */
class Address extends AppModel {

/**
 * Primary key field
 *
 * @var string
 */
    public $primaryKey = 'id';

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Company' => array(
            'className' => 'Company',
            'foreignKey' => 'id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
    );

/**
 * hasMany associations
 *
 * @var array
 */
    public $hasMany = array(

        'ConsumerProduct' => array(
            'className' => 'ConsumerProduct',
            'foreignKey' => 'id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

}

ConsumerProduct.php

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


/**
 * ConsumerProduct Model
 *
 * @property Product $Product
 * @property LegacyProduct $LegacyProduct
 * @property UnitStatus $UnitStatus
 * @property addresses $Addresses
 * @property Review $Review
 */
class ConsumerProduct extends AppModel {

/**
 * Primary key field
 *
 * @var string
 */
    public $primaryKey = 'id';

    //The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        /*'Product' => array(
            'className' => 'Product',
            'foreignKey' => 'id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),*/
        'LegacyProduct' => array(
            'className' => 'LegacyProduct',
            'foreignKey' => 'id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'UnitStatus' => array(
            'className' => 'UnitStatus',
            'foreignKey' => 'id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

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

}

Product.php

<?php
App::uses('AppModel', 'Model');
/**
 * Product Model
 *
 * @property serials $Serials
 * @property Category $Category
 * @property ProductStatus $ProductStatus
 * @property ProductReward $ProductReward
 */
class Product extends AppModel {

/**
 * Primary key field
 *
 * @var string
 */
    public $primaryKey = 'id';

/**
 * Display field
 *
 * @var string
 */
    public $displayField = 'name';


/**
 * hasOne associations
 *
 * @var array
 */
    public $hasOne = array(
        'Serials' => array(
            'className' => 'serials',
            'foreignKey' => 'id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'Category' => array(
            'className' => 'Category',
            'foreignKey' => 'id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'ProductStatus' => array(
            'className' => 'ProductStatus',
            'foreignKey' => 'id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

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

}

2 个答案:

答案 0 :(得分:0)

&#34;产品&#34;,&#34; LegacyProduct&#34;和&#34; UnitStatus&#34;模特?如果是,则在使用loadModel

的查找之前声明

使用$ this-&gt; Product-&gt; find(...)...

答案 1 :(得分:0)

在您的ConsumerProduct.php $hasMany数组'className' => 'addresses'中,'className' => 'Address'代替Address(模型也应为className)。

一般情况下,如果您的类名符合约定,并且实际上并未对模型进行别名,请避免指定{{1}}键。