1

我正在使用蛋糕 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'
            )
           )
        )
    )

审查.php

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

照片.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  |
    +----+---------+------------+-------------+-------+--------------------------------------+-----------+
4

1 回答 1

2

它看起来不像您在 find inhasMany关系中Review定义外键Photo,您没有完成关系。

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

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

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

您还应该定义和之间的belongsTo关系ReviewRating

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' => ''
    )
);
于 2013-05-28T06:38:01.023 回答