2

私は大きな成功を収めずにsaveAssociatedメソッドを使用しようとしてCakePHPいます。メソッドは部分的に機能しているようです。実際には、保存プロセスに3つのモデルがあります: 、これCharacterはモデルです:$hasManyPropertyLabelCharacter

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

0

saveAssociated の代わりに saveAll メソッドを試してください。

于 2012-04-19T21:24:01.657 に答える