我正在尝试使用saveAssociated
方法,CakePHP
但没有取得很大成功,该方法似乎部分有效,事实上我在保存过程中有三个模型:
Character
,哪个$hasMany
> Property
,Label
所以这是Character
模型:
class Character extends AppModel {
public $name = 'Character';
public $belongsTo = 'User';
public $hasMany = array (
'Property' => array (
'dependent' => true
)
);
public $hasOne = array (
'Label' => array (
'dependent' => true
)
);
public $validate = array (
'name' => array (
'required' => true,
'rule' => array('between', 0, 100),
'message' => 'This is the error message for "name" field'
),
'description' => array (
'allowEmpty' => true,
'rule' => array('between', 0, 100),
'message' => 'This is the error message for "description" field'
)
);
}
这是我从表格中获得的数据:
array(
'Character' => array(
'name' => 'Character name',
'description' => 'Character description',
'image' => 'http://url.com/image.jpg'
),
'Label' => array(
'name' => 'Basic attributes',
'value' => 'Value'
),
'Property' => array(
(int) 0 => array(
'name' => 'Strenght',
'value' => '15'
)
)
)
然后在我CharactersController
对数据执行此操作saveAssociated
:
class CharactersController extends AppController {
public $uses = array ('Character', 'Property', 'Label');
public function add () {
if (!empty($this->request->data)) {
$this->Character->saveAssociated($this->request->data);
}
debug ($this->request->data);
}
}
Character
数据保存成功但没有Label
,我Property
错在哪里?