Hi Guys…
New to here, just having abit of a issue. I have a form which collects data and stores the records in to a main movie table. I want to now be able to store a youtube vides which has a one to many relationship with the movie table using movie id as the relationship key… so one movie can have many youtube videos. I have tried lots of different options and searched on the web… This is what I have so far…
Form (Part Show)
The Youtube Video embed section, user can add more text fields dynamically so they can input more than one video…
<div class="row">
<?php echo $form->labelEx($model,'map_pin_id'); ?>
<?php echo $form->dropDownList($model,'map_pin_id', $model->getMapPinOptions()); ?>
<?php echo $form->error($model,'map_pin_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'description'); ?>
<?php echo $form->textArea($model,'description',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'description'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'title_trailer_youtube_code'); ?>
<?php echo $form->textField($model,'title_trailer_youtube_code',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'title_trailer_youtube_code'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'title_image'); ?>
<?php echo CHtml::activeFileField($model, 'title_image'); ?>
<?php echo $form->error($model,'title_image'); ?>
</div>
<div class="row clone">
<?php echo $form->labelEx($modelYoutubeVideo,'embed_code'); ?>
<?php echo $form->textField($modelYoutubeVideo,'embed_code',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($modelYoutubeVideo,'embed_code'); ?>
<?php echo $form->labelEx($modelYoutubeVideo,'description'); ?>
<?php echo $form->textField($modelYoutubeVideo,'description',array('size'=>50,'maxlength'=>250)); ?>
<?php echo $form->error($modelYoutubeVideo,'description'); ?>
</div>
<?php
$this->widget('ext.widgets.reCopy.ReCopyWidget', array(
'targetClass'=>'clone',
));
?>
Movie controller –
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
));
}
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model=new Movie; // this is my model related to table
if(isset($_POST['Movie']))
{
$model->attributes=$_POST['Movie'];
// Save Title Image and store file name in database
$_POST['Movie']['title_image'] = $model->title_image;
$uploadedFile=CUploadedFile::getInstance($model,'title_image');
$name = $uploadedFile->getName();
$model->title_image = $name;
if($model->save())
{
if(!empty($uploadedFile)) // check if uploaded file is set or not
{
$uploadedFile->saveAs(Yii::getPathOfAlias('webroot')."/title_image/".$name);
}
if(isset($_POST['youtube_video'])) {
$modelYoutubeVideo = new YoutubeVideo();
$modelYoutubeVideo->attributes=$_POST['youtube_video'];
}
// Redirect to admin
$this->redirect(array('admin'));
}
}
$this->render('create',array(
'model'=>$model,
'modelYoutubeVideo'=>$modelYoutubeVideo,
));
}
Movie Model —
*/
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'),
);
}
YoutubeVideo Model –
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, 'Movie', 'movie_id'),
);
}
Hope you guys can point me the right direction
Thanks