Yii框架更新页面添加更多模型进行查看

时间:2014-03-05 14:58:49

标签: php yii one-to-many

我有一个无法加载的更新表单,因为它无法找到模型..我有三个表..电影,YoutubeVideos,Twitter。电影与其他表有一对多的关系。我希望能够使用当前电影ID创建更新表单,并与YouTube视频和推特中的数据匹配。并显示结果..

这就是我所拥有的

public function actionUpdate($id)
{



    $model=$this->loadModel($id);

    // $modelYoutubeVideo = YoutubeVideo::model()->findByAttributes(array('movie_id'=>$id));
    // $modelTwitterFeed = TwitterFeed::model()->findByAttributes(array('movie_id'=>$id));

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['Movie']))
    {
        $model->attributes=$_POST['Movie'];
        if($model->save())
            $this->redirect(array('view','id'=>$model->id));
    }

    $this->render('update',array(
        'model'=>$model,
        'modelYoutubeVideo'=> YoutubeVideo::model()->findByAttributes(array('movie_id'=>$model->id)),
        'modelTwitterFeed'=> TwitterFeed::model()->findByAttributes(array('movie_id'=>$id))

    ));
}

电影模型 -

<?php

/ **  *这是表“电影”的模型类。  *  *以下是“电影”表格中的可用栏目:  * @property整数$ id  * @property string $ movie_name  * @property整数$ studio_id  * @property integer $ country_id  * @property integer $ movie_rating_id  * @property integer $ map_pin_id  * @property string $ description  * @property string $ title_image  * @property string $ title_trailer_youtube_code  * @property string $ release_date  * @property string $ bg_c​​olour_code  * @property string $ text_colour_code  * @property integer $ is_released  * @property integer $ is_advanced_booking_allowed  * @property string $ advanced_booking_start_date  * @property string $ advanced_booking_end_date  * @property string $ domain_prefix  * @property string $ facebook_app_id  * @property string $ facebook_app_url  * @property string $ facebook_icon_image_url  * @property string $ facebook_text  * @property string $ twitter_text  * @property string $ booking_share_text  * @property string $ westworld_movie_id  * @property string $ home_tracking_url  * @property string $ shows_tracking_url  *  *以下是可用的模型关系:  * @property比赛[] $比赛  * @property Studio $ studio  * @property国家$ country  * @property MovieRating $ movieRating  * @property MapPin $ mapPin  * @property TwitterFeed [] $ twitterFeeds  * @property YoutubeVideo [] $ youtubeVideos   / class Movie扩展了CActiveRecord {     / *      * @return字符串关联的数据库表名称      * /     public function tableName()     {         回归'电影';     }

/**
 * @return array validation rules for model attributes.
 */
public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('studio_id, country_id, movie_rating_id, map_pin_id, is_released, is_advanced_booking_allowed', 'numerical', 'integerOnly'=>true),
        array('movie_name domain_prefix, facebook_app_url, facebook_icon_image_url, home_tracking_url, shows_tracking_url', 'length', 'max'=>255),
        array('title_image', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'),
        array('title_trailer_youtube_code', 'length', 'max'=>50),
        array('bg_colour_code, text_colour_code', 'length', 'max'=>8),
        array('facebook_app_id', 'length', 'max'=>20),
        array('westworld_movie_id', 'length', 'max'=>10),
        array('title_image', 'file', 'allowEmpty'=>true),
        array('description, release_date, advanced_booking_start_date, advanced_booking_end_date, facebook_text, twitter_text, booking_share_text', 'safe'),
        // The following rule is used by search().
        // @todo Please remove those attributes that should not be searched.
        array('id, movie_name, studio_id, country_id, movie_rating_id, map_pin_id, description, title_trailer_youtube_code, release_date, bg_colour_code, text_colour_code, is_released, is_advanced_booking_allowed, advanced_booking_start_date, advanced_booking_end_date, domain_prefix, facebook_app_id, facebook_app_url, facebook_icon_image_url, facebook_text, twitter_text, booking_share_text, westworld_movie_id, home_tracking_url, shows_tracking_url', 'safe', 'on'=>'search'),
    );
}

/**
 * @return array relational rules.
 */
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(
        'competitions' => array(self::HAS_MANY, 'Competition', 'movie_id'),
        'studio' => array(self::BELONGS_TO, 'Studio', 'studio_id'),
        'country' => array(self::BELONGS_TO, 'Country', 'country_id'),
        'movieRating' => array(self::BELONGS_TO, 'MovieRating', 'movie_rating_id'),
        'mapPin' => array(self::BELONGS_TO, 'MapPin', 'map_pin_id'),
        'twitterFeeds' => array(self::HAS_MANY, 'TwitterFeed', 'movie_id'),
        'YoutubeVideo' => array(self::HAS_MANY, 'YoutubeVideo', 'movie_id'),
    );
}

/**
 * @return array customized attribute labels (name=>label)
 */
public function attributeLabels()
{
    return array(
        'id' => 'ID',
        'movie_name' => 'Movie Name',
        'studio_id' => 'Studio',
        'country_id' => 'Country',
        'movie_rating_id' => 'Movie Rating',
        'map_pin_id' => 'Map Pin',
        'description' => 'Description',
        'title_image' => 'Title Image',
        'title_trailer_youtube_code' => 'Title Trailer Youtube Code',
        'release_date' => 'Release Date',
        'bg_colour_code' => 'Bg Colour Code',
        'text_colour_code' => 'Text Colour Code',
        'is_released' => 'Is Released',
        'is_advanced_booking_allowed' => 'Is Advanced Booking Allowed',
        'advanced_booking_start_date' => 'Advanced Booking Start Date',
        'advanced_booking_end_date' => 'Advanced Booking End Date',
        'domain_prefix' => 'Domain Prefix',
        'facebook_app_id' => 'Facebook App',
        'facebook_app_url' => 'Facebook App Url',
        'facebook_icon_image_url' => 'Facebook Icon Image Url',
        'facebook_text' => 'Facebook Text',
        'twitter_text' => 'Twitter Text',
        'booking_share_text' => 'Booking Share Text',
        'westworld_movie_id' => 'Westworld Movie',
        'home_tracking_url' => 'Home Tracking Url',
        'shows_tracking_url' => 'Shows Tracking Url',
    );
}

/**
 * Retrieves a list of models based on the current search/filter conditions.
 *
 * Typical usecase:
 * - Initialize the model fields with values from filter form.
 * - Execute this method to get CActiveDataProvider instance which will filter
 * models according to data in model fields.
 * - Pass data provider to CGridView, CListView or any similar widget.
 *
 * @return CActiveDataProvider the data provider that can return the models
 * based on the search/filter conditions.
 */
public function search()
{
    // @todo Please modify the following code to remove attributes that should not be searched.

    $criteria=new CDbCriteria;

    $criteria->compare('country_id', Yii::app()->user->getState('country_id'));


    $criteria->compare('id',$this->id);
    $criteria->compare('movie_name',$this->movie_name,true);
    $criteria->compare('studio_id',$this->studio_id);
    $criteria->compare('country_id',$this->country_id);
    $criteria->compare('movie_rating_id',$this->movie_rating_id);
    $criteria->compare('map_pin_id',$this->map_pin_id);
    $criteria->compare('description',$this->description,true);
    $criteria->compare('title_image',$this->title_image,true);
    $criteria->compare('title_trailer_youtube_code',$this->title_trailer_youtube_code,true);
    $criteria->compare('release_date',$this->release_date,true);
    $criteria->compare('bg_colour_code',$this->bg_colour_code,true);
    $criteria->compare('text_colour_code',$this->text_colour_code,true);
    $criteria->compare('is_released',$this->is_released);
    $criteria->compare('is_advanced_booking_allowed',$this->is_advanced_booking_allowed);
    $criteria->compare('advanced_booking_start_date',$this->advanced_booking_start_date,true);
    $criteria->compare('advanced_booking_end_date',$this->advanced_booking_end_date,true);
    $criteria->compare('domain_prefix',$this->domain_prefix,true);
    $criteria->compare('facebook_app_id',$this->facebook_app_id,true);
    $criteria->compare('facebook_app_url',$this->facebook_app_url,true);
    $criteria->compare('facebook_icon_image_url',$this->facebook_icon_image_url,true);
    $criteria->compare('facebook_text',$this->facebook_text,true);
    $criteria->compare('twitter_text',$this->twitter_text,true);
    $criteria->compare('booking_share_text',$this->booking_share_text,true);
    $criteria->compare('westworld_movie_id',$this->westworld_movie_id,true);
    $criteria->compare('home_tracking_url',$this->home_tracking_url,true);
    $criteria->compare('shows_tracking_url',$this->shows_tracking_url,true);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

/**
 * Returns the static model of the specified AR class.
 * Please note that you should have this exact method in all your CActiveRecord descendants!
 * @param string $className active record class name.
 * @return Movie the static model class
 */
public static function model($className=__CLASS__)
{
    return parent::model($className);
}

public function getYesNoOptions(){
    $optionList = array();
    $optionList[0] = "No";
    $optionList[1] = "Yes";
    return $optionList;
}

public function getRatingOptions(){
    $ratingList = MovieRating::model()->findAll();
    $optionList = CHtml::listData($ratingList, 'id', 'rating_name');
    return $optionList;
}

public function getStudioOptions(){
    $studioList = Studio::model()->findAll();
    $optionList = CHtml::listData($studioList, 'id', 'name');
    return $optionList;
}

public function getMapPinOptions(){
    $mapPinList = MapPin::model()->findAll();
    $optionList = CHtml::listData($mapPinList, 'id', 'pin_colour_code');
    return $optionList;
}

}

重试 - 认为它需要沿着这些方向

public function loadYoutubeVideoModel($id)
{

    $criteria=new CDbCriteria;
    $criteria->addSearchCondition('movie_id', $id);

    $model=YoutubeVideo::model()->findAll($criteria);

    if($model===null)
        throw new CHttpException(404,'The requested page does not exist.');
    return $model;
}

public function loadTwitterModel($id)
{
    $criteria=new CDbCriteria;
    $criteria->addSearchCondition('movie_id', $id);

    $model=TwitterFeed::model()->findAll($criteria);

    if($model===null)
        throw new CHttpException(404,'The requested page does not exist.');
    return $model;
}

1 个答案:

答案 0 :(得分:0)

试试这个。

public function actionUpdate($id){



$model=Movie::model()->with('YoutubeVideo','twitterFeeds')->findbypk($id);

// $modelYoutubeVideo = YoutubeVideo::model()->findByAttributes(array('movie_id'=>$id));
// $modelTwitterFeed = TwitterFeed::model()->findByAttributes(array('movie_id'=>$id));

// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);

if(isset($_POST['Movie']))
{
    $model->attributes=$_POST['Movie'];
    if($model->save())
        $this->redirect(array('view','id'=>$model->id));
}

$this->render('update',array(
    'model'=>$model,
)

);

}

您可以使用此代码在更新视图文件(不在_form文件中)中显示数据。

foreach($modelYoutubeVideo as $youtube){
   echo $youtube["blabla"];
}
foreach($modelTwitterFeed as $twitter){
   echo $twitter["blabla"];
}