开发者

CakePHP add/edit form is working, except for foreign key references

开发者 https://www.devze.com 2022-12-13 06:44 出处:网络
I have a cakephp object (SecurityPerson) that has a few foreign key relationships to other objects. Here\'s the add/edit view code that\'s generating the pulldowns for the foriegn-key references:

I have a cakephp object (SecurityPerson) that has a few foreign key relationships to other objects.

Here's the add/edit view code that's generating the pulldowns for the foriegn-key references:

echo $form->input('ContractNumber');
echo $form->input('Location');

Here's the references to these objects in the model:

var $belongsTo = array(
    'ContractNumber' => array(
        'className' => 'ContractNumber',
        'foreignKe开发者_如何学Cy' => 'contract_number_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Location' => array(
        'className' => 'Location',
        'foreignKey' => 'location_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

And here's the relevent part of the add() controller:

    if (!empty($this->data)) {
        $this->SecurityPerson->create();
        if ($this->SecurityPerson->save($this->data)) {
            $this->Session->setFlash(__('The SecurityPerson has been saved', true));
            $this->redirect(array('action'=>'index'));
        } else {
            $this->Session->setFlash(__('The SecurityPerson could not be saved. Please, try again.', true));
        }
    }
    $contractNumbers = $this->SecurityPerson->ContractNumber->find('list');
    $locations = $this->SecurityPerson->Location->find('list');
    $this->set(compact('contractNumbers', 'locations'));

It all looks good to me, including the generated HTML for the pulldowns on the add and edit pages. BUT... only nulls get added to the sercurity_person row.

Can anyone easily see what's going on, or give me a good start in troubleshooting?

EDIT: What I've tried so far:

I've tried looking at the SQL query. The field names for the foreign key ID's do not appear in the INSERT or UPDATE statement.

I've tried looking at the $data structure. The foreign key references look like this: [ContractNumber] => 2 [Location] => 1


Ok, turns out the form inputs need to be done like this:

    echo $form->input('contract_number_id', array('type' => 'select'));
    echo $form->input('location_id', array('type' => 'select'));
0

精彩评论

暂无评论...
验证码 换一张
取 消