Question

Im having a bit of a headache with trying to input data into a one to many relationship table (videos) from one form which has access to the main database called movie.. so one movie can have lots of videos attached to it.. I have one form which can add more than one video through the users being able to add more text fields for the amount of videos they want to store..tried various option. but the records dont seem to store inside the youtube_video table..

this is what I have got so far...

Movie Model (Movie)

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'),
    );
}

Movie Form Part of the Form -

    <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>

Movie Controller -

    public function actionCreate()
{
    $model=new Movie;  // this is my model related to table

    $modelYoutubeVideo=new YoutubeVideo;



    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);
            }


            $modelYoutubeVideo = new YoutubeVideo();

            $modelYoutubeVideo->attributes=$_POST['youtube_video'];



            // Redirect to admin

            $this->redirect(array('admin'));

        }
    }

    $this->render('create',array(
        'model'=>$model,
        'modelYoutubeVideo'=>$modelYoutubeVideo,

    ));
}

No correct solution

OTHER TIPS

Well, are you duplicating <div class="row clone"> many times to allow people to link more videos? Because if you are then you are not saving them in the DB.

After you save the the main record "foreach" through the videos and save each of them. Also you never save $modelYoutubeVideo so ....

After

$modelYoutubeVideo->attributes=$_POST['youtube_video'];

you should put

$modelYoutubeVideo->save();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top