-1

我使用 cakephp 控制台生成了一些 CRUD 操作。生成的代码有问题,因为默认情况下未在控制器中加载关联模型的模型。

例如:

$programs = $this->Chantier->Program->find('list');

行不通,但是:

$this->loadModel('Program');
$programs = $this->Program->find('list');

将。这是关联的代码:

/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
    'Programs' => array(
        'className' => 'Programs',
        'foreignKey' => 'Programs_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Etats' => array(
        'className' => 'Etats',
        'foreignKey' => 'Etats_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Types' => array(
        'className' => 'Types',
        'foreignKey' => 'Types_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'ChampsLibres' => array(
        'className' => 'ChampsLibres',
        'foreignKey' => 'Champs_libres_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),


);
4

1 回答 1

1

仔细查看您的代码:

public $belongsTo = array(
'Programs' => array(
    'className' => 'Programs',
    'foreignKey' => 'Programs_id',
    'conditions' => '',
    'fields' => '',
    'order' => ''
),

关联的别名和类名都是复数形式,虽然这是可能的并且很好,但它违反了 CakePHP 的约定。你正在尝试这个:

$programs = $this->Chantier->Program->find('list');

但请注意,您以单数形式编写 Program,而不是在您的关联中声明的复数形式。我建议重新烘焙您的代码以遵循约定,以避免将来的头痛和混乱。

于 2012-08-03T08:02:11.343 回答