【发布时间】:2012-01-14 18:35:47
【问题描述】:
我用谷歌搜索了很多,但似乎没有什么与我正在寻找的相似..
我要做的是使用多个 HTML 列表一次将一条记录“用户”与其他多条记录“卡片”链接起来。
表格是: 用户(ID、姓名、用户名) 卡片(id,user_id)
型号:
class User extends AppModel {
public $name = 'User';
public $hasMany = array('Card');}
class Card extends AppModel {
public $name = 'Card';
public $belongsTo = array('User');}
用户edit.ctp视图
echo $this->Form->input('id');
echo $this->Form->input('Card', array('multiple'=>true));
我的控制器会是什么样子?目前它看起来像这样,但只保存“没有相关卡”的用户记录
if (!empty($this->data)) {
foreach($this->data['User']['Card'] as $key => $item){
$this->data['User']['Card'][$key] = array('id'=>$item, 'user_id'=>$this->data['User']['id']);
}
if ($this->User->saveAll($this->data)) {
//$this->User->Card->saveAll($this->data);
$this->Session->setFlash(__('The user has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
}
}
if (empty($this->data)) {
$this->data = $this->User->read(null, $id);
$this->set('cards', $this->User->Card->find('list'));
}
$this->数据包含:
Array
(
[User] => Array
(
[id] => 1
[name] => Superman
[status] => 1
[Card] => Array
(
[0] => Array
(
[id] => 11402130001
[user_id] => 1
)
[1] => Array
(
[id] => 11402130002
[user_id] => 1
)
[2] => Array
(
[id] => 11402130003
[user_id] => 1
)
[3] => Array
(
[id] => 11402130004
[user_id] => 1
)
)
)
)
【问题讨论】:
标签: cakephp cakephp-1.3