PHP Yii 关系 : Movies & cinemas & showtimes

标签 php mysql yii

我正在研究电影和电影院放映时间指南。

我有放映时间表(照片#1)和时间表(照片#2)

我需要在电影页面中显示带有放映时间的电影院列表,如下所示

影院名称:时间 1 - 时间 2 - 时间 3

大影院:** 00:18 - 00:20 - 00:22 .... 等

我的代码不起作用..我可以写什么来在 View 文件中显示它

Error: The relation "cinemas" in active record class "movie" is not specified correctly: the join table "{{showtime}}" given in the foreign key cannot be found in the database.

电影模型有关系:

'cinemas' => array(self::MANY_MANY, 'Cinema', '{{showtime}}(movie_id, cinema_id)')

电影模型有关系:

'times' => array(self::MANY_MANY, 'Time', '{{showtime}}(cinema_id, time_id)')

影院模型应具有命名范围:

public function times_by_movie($movie_id) {
    return $this->with(
        array(
            'times'=>array(
                'condition'=>'`movie_id` = :movie_id',
                'params' => array(':movie_id'=>$movie_id),
                'joinType' => 'INNER JOIN'
            )
        )
    );
}

MoviesController > actionView

public function actionView($id) {
    $movie = Movie::model()->with(
        array(
            'cinemas'=>array(
                'scopes'=>array(
                    'times_by_movie'=>$id
                )
            )
        )
    )->findByPk($id);
    if (!$movie)
      throw new CHttpException(404, 'Movie not found.');

}

enter image description here enter image description here

最佳答案

因此,您有模型 MovieShowtimeTime。 您的 Movie 模型具有以下关系:

'cinemas' => array(self::MANY_MANY, 'Cinema', '{{showtime}}(movie_id, cinema_id)')

您的 Cinema 模型有关系:

'times' => array(self::MANY_MANY, 'Time', '{{showtime}}(cinema_id, time_id)')

此外,您的 Cinema 模型应该具有命名范围:

public function times_by_movie($movie_id) {
    return $this->with(
        array(
            'times'=>array(
                'condition'=>'`movie_id` = :movie_id',
                'params' => array(':movie_id'=>$movie_id),
                'joinType' => 'INNER JOIN'
            )
        )
    );
}

MoviesController->actionView 中,您应该具有:

public function actionView($id) {
    $movie = Movie::model()->with(
        array(
            'cinemas'=>array(
                'scopes'=>array(
                    'times_by_movie'=>$id
                )
            )
        )
    )->findByPk($id);
    if (!$movie)
      throw new CHttpException(404, 'Movie not found.');

}

类似这样的事情。不过应该进行测试。

关于PHP Yii 关系 : Movies & cinemas & showtimes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10055974/

相关文章:

php - 我可以使用 SimpleXML 和 Xpath 直接选择元素属性吗?

php - 如何显示自定义字段值数组中的 Wordpress 帖子

php - 使用 Yii 中的 updateAll 方法按某个数字更新字段值

php - 使用php从google maps html调用解析json数据到mysql数据库

php - 如何在 .htaccess 文件中指定范围以重定向 url

mysql - 从表中选择字符串的特定部分

MySQL 重用函数返回的值

php - 如何使用 yii 在 URL 中隐藏动态文件夹?

php - Yii 上的 FindAll,字段名称无效

php - CodeIgniter 中的异常处理