CakePHP saveAll不保存第二级依赖的密钥

时间:2013-05-28 05:43:20

标签: cakephp

我正在使用蛋糕PHP 2.x.我的数据库设置如下。

评级有一个评论

评论有很多照片

data => array(
    'Rating' => array(
        'rating' => '1',
        'user_id' => '1',
        'listing_id' => '55'
        ),
    'Review' => array(
        'title' => 'Good Service',
        'date_visited' => array(
            'month' => '05',
            'day' => '28',
            'year' => '2013',
            ),
        'service_used' => 'Easy Checkout',
        'description' => 'After a fairly quick check-in, the check out service was also breeze ',
        'Photo' => array(
            (int) 1 => array(
                'title' => 'Test',
                'photo' => array(
                'name' => '2.JPG',
                ),
            'listing_id' => '55',
            'user_id' => '1'
            )
           )
        )
    )

Review.php

public $hasMany = array(
    'Photo' => array(
        'className'  => 'Photo',
        'conditions' =>'',
        'order'      => ''
        )
);

Photo.php

public $belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Review' => array(
        'className' => 'Review',
        'foreignKey' => 'review_uuid',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Listing' => array(
        'className' => 'Listing',
        'foreignKey' => 'listing_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

最后是RatingsController.php

$this->Rating->create();

if ($this->Rating->saveAll($this->request->data, array('deep' => true))) {
    $this->Session->setFlash(__('The rating has been saved'));
    $this->redirect(array('action' => 'index'));
}

问题是所有数据都被保存,除了照片模型中的review_uuid(也是同时创建的))。

mysql> select id,user_id,listing_id,review_uuid,title,photo,photo_dir from photos where ID=26;
    +----+---------+------------+-------------+-------+--------------------------------------+-----------    +
    | id | user_id | listing_id | review_uuid | title | photo                                | photo_dir |
    +----+---------+------------+-------------+-------+--------------------------------------+-----------    +
    | 26 |       1 |         55 | NULL        | Test  | 1a107372ef53ba26d7748a50c25e6b27.jpg | 01/77/74  |
    +----+---------+------------+-------------+-------+--------------------------------------+-----------+

1 个答案:

答案 0 :(得分:2)

hasMany中找到的Review Photo关系中,您看起来不像是在定义外键,您没有完成关系。

hasMany中的Review.php数组应如下所示:

public $hasMany = array(
    'Photo' => array(
        'className'  => 'Photo',
        'foreignKey' => 'review_uuid',
        'conditions' =>'',
        'order'      => ''
        )
);

请注意foreignKey参数。你告诉Cake,Review有很多照片,每张照片记录都有一个名为review_uuid的外键,用于识别它与评论的关系。您在belongsTo模型中的Photo数组中定义了外键,但未在hasMany模型中的Review数组中定义外键,因此关系从未完成。

您还应该在belongsToReview

之间定义Rating关系

Review.php

public $hasMany = array(
    'Photo' => array(
        'className'  => 'Photo',
        'foreignKey' => 'review_uuid',
        'conditions' =>'',
        'order'      => ''
        )
);

public $belongsTo = array(
    'Rating' => array(
        'className' => 'Rating',
        'foreignKey' => 'rating_id',
        'conditions' => '',
        'order' => ''
    )
);