2

我正在尝试使用saveAssociated方法,CakePHP但没有取得很大成功,该方法似乎部分有效,事实上我在保存过程中有三个模型: Character,哪个$hasMany> PropertyLabel所以这是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错在哪里?

4

2 回答 2

1

在另外两个 Model 中声明了 Model Association 的另一端了吗?好好看看这个页面。如果您使用$hasAndBelongsToMany关联,可能会使生活更轻松。

于 2013-04-26T19:14:21.590 回答
0

尝试使用 saveAll 方法而不是 saveAssociated。

于 2012-04-19T21:24:01.657 回答