-1

I used the cakephp console to generate some CRUD operations. The generated code was buggy as the model of an associated model was not loaded by default in the controller.

For instance:

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

would not work, but:

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

would. Here is the code of the associations:

/**
* 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' => ''
    ),


);
L. Sanna
  • 6,482
  • 7
  • 33
  • 47

1 Answers1

1

Take a close look to your code:

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

Both the alias for the association and classname are in plural, while this is possible and fine, it goes against CakePHP conventions. You are trying this:

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

But note that you wrote Program in singular and not in plural as declared in your associations. I would suggest to re-bake your code to follow convention to avoid future headaches and confusion.