php - CakePHP to save relationship in other model -
i have these models:
class prefix extends appmodel { public $displayfield = 'prefix'; public $hasmany = array( 'state' => array( 'classname' => 'state', 'foreignkey' => 'prefix_id', 'dependent' => false, ), ); } class state extends appmodel { public $displayfield = 'name'; public $belongsto = array( 'prefix' => array( 'classname' => 'prefix', 'foreignkey' => 'prefix_id', ), ); } then have admin_add method, automatic scaffolder:
public function admin_add() { if ($this->request->is('post')) { $this->peefix->create(); if ($this->prefix->save($this->request->data)) { $this->redirect(array('action' => 'index')); } else { // error message } } $states = $this->prefix->state->find('list'); $this->set(compact('states')); } i have list of them in form:
<?php echo $this->form->input('state', array('multiple' => 'checkbox', 'type' => 'select',)); ?> now can set states prefix. however, when submit form, selection disappears. not saved in database.
what did wrong?
you linked models if there 1 state per prefix, , many prefixes "assigned" 1 state. means cannot use 'multiple' => 'checkbox'. either remove or change model associations habtm.
Comments
Post a Comment