2014-03-05 61 views
0

我有一個不能加載,因爲它無法找到模型的更新表格..我有三個表格..電影,YoutubeVideos,Twitter。電影與其他表格有一對多的關係..我希望能夠使用當前電影ID創建更新表單並與YouTube視頻和Twitter中的數據相匹配。並顯示結果..Yii框架更新頁面添加更多的模型來查看

這是我有這麼

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 * @屬性字符串$ MOVIE_NAME * @property整數$ studio_id * @property整數$ COUNTRY_ID * @property整數$ movie_rating_id * @property整數$ map_pin_id * @屬性字符串$描述 * @屬性字符串$ title_image * @屬性字符串$ title_trailer_youtube_code * @屬性字符串$ RELEASE_DATE * @屬性字符串$ bg_c​​olour_code * @屬性字符串$ text_colour_code * @property整數$ is_released * @property整數$ 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 * *以下是日E,可模型關係: * @property大賽[] $比賽 * @property工作室$工作室 * @property國家$國家 * @property MovieRating $ movieRating * @property MapPin $ mapPin * @property通過twitterfeed [] $ twitterFeeds * @property YoutubeVideo [] $ youtubeVideos / 類電影擴展的CActiveRecord { / * * @返回字符串相關的數據庫表名 */ 公共功能表名(){ 回報 '電影' ; }

/** 
* @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; 
} 
+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文件)你DATAS。

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

感謝回覆,我已經在loadmodel函數中有這個,我的問題是使用該ID從其他模型獲取其他模型 – user3355603

+0

因此,請添加您的電影模型代碼... –

+0

添加Ive嘗試這沒有結果公共職能loadYoutubeVideoModel($ id) \t { \t \t $ criteria = new CDbCriteria; \t \t $ criteria-> condition ='movie_id =:movie_id'; \t \t $ criteria-> params = array(':movie_id'=> 10); \t \t $ model = YoutubeVideo :: model() - > find($ criteria); \t \t如果($模型===空) \t \t \t拋出新CHttpException(404 '所請求的頁面不存在。'); \t \t return $ model; \t} – user3355603