我有电影表和TblMovieVideos表,并且与该表有关系,因此我需要从TblMovieVideos中获取“ youtube_id”,其中“ trailer = 1”

我想我需要通过此代码获得结果,但这是错误的
错误:$model->tblMovieVideos[$model->id]->trailer = 1 ->youtube_id ??????

(附有数据库图片)

电影模型:

public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'tblCinemaMovies' => array(self::HAS_MANY, 'TblCinemaMovies', 'movie_id'),
            'tblDays' => array(self::HAS_MANY, 'TblDay', 'movie_id'),
            'tblGenres' => array(self::HAS_MANY, 'TblGenre', 'movie_id'),
            'tblMoviePhotos' => array(self::HAS_MANY, 'TblMoviePhoto', 'movie_id'),
            'tblMovieVideos' => array(self::HAS_MANY, 'TblMovieVideo', 'movie_id'),
            'tblShowtimes' => array(self::HAS_MANY, 'TblShowtime', 'movie_id'),
        );
    }


TblMovies视频模型:

public function relations()
{
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
        'movie' => array(self::BELONGS_TO, 'TblMovie', 'movie_id'),
        'tblSpotlights' => array(self::HAS_MANY, 'TblSpotlight', 'video_id'),
    );
}






索引:

最佳答案

您可以使用以下方法获取所有带有预告片的电影:

$movies=Movie::model()->with(array('tblMovieVideos'=>array('condition'=>'trailer = 1')))->findAll();


但是,由于您想要的是youtube_id,请尝试以下操作:

$id = 1;//Id of the movie for which you are trying to find the video
$videos = TblMoviesVideos::model()->findAllByPk($id, 'trailer = 1');
foreach ($videos as $video)
{
    echo $video->youtube_id;
}


干杯。

10-04 16:01