title: Yii Framework One to many relationship
body: <p>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.. </p>
  
  <p>this is what I have got so far...</p>
  
  <p>Movie Model (Movie)</p>
  
  <pre><code>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' =&gt; array(self::HAS_MANY, 'Competition', 'movie_id'),
          'studio' =&gt; array(self::BELONGS_TO, 'Studio', 'studio_id'),
          'country' =&gt; array(self::BELONGS_TO, 'Country', 'country_id'),
          'movieRating' =&gt; array(self::BELONGS_TO, 'MovieRating', 'movie_rating_id'),
          'mapPin' =&gt; array(self::BELONGS_TO, 'MapPin', 'map_pin_id'),
          'twitterFeeds' =&gt; array(self::HAS_MANY, 'TwitterFeed', 'movie_id'),
          'YoutubeVideo' =&gt; array(self::HAS_MANY, 'YoutubeVideo', 'movie_id'),
      );
  }
  </code></pre>
  
  <p>Movie Form Part of the Form -</p>
  
  <pre><code>    &lt;div class="row"&gt;
      &lt;?php echo $form-&gt;labelEx($model,'description'); ?&gt;
      &lt;?php echo $form-&gt;textArea($model,'description',array('rows'=&gt;6, 'cols'=&gt;50)); ?&gt;
      &lt;?php echo $form-&gt;error($model,'description'); ?&gt;
  &lt;/div&gt;
  
  &lt;div class="row"&gt;
  
      &lt;?php echo $form-&gt;labelEx($model,'title_trailer_youtube_code'); ?&gt;
      &lt;?php echo $form-&gt;textField($model,'title_trailer_youtube_code',array('size'=&gt;50,'maxlength'=&gt;50)); ?&gt;
      &lt;?php echo $form-&gt;error($model,'title_trailer_youtube_code'); ?&gt;
  
  &lt;/div&gt;
  
  &lt;div class="row"&gt;
      &lt;?php echo $form-&gt;labelEx($model,'title_image'); ?&gt;
          &lt;?php echo CHtml::activeFileField($model, 'title_image'); ?&gt;
          &lt;?php echo $form-&gt;error($model,'title_image'); ?&gt;
  &lt;/div&gt;
  
  
  &lt;div class="row clone"&gt;
      &lt;?php echo $form-&gt;labelEx($modelYoutubeVideo,'embed_code'); ?&gt;
      &lt;?php echo $form-&gt;textField($modelYoutubeVideo,'embed_code',array('size'=&gt;50,'maxlength'=&gt;50)); ?&gt;
      &lt;?php echo $form-&gt;error($modelYoutubeVideo,'embed_code'); ?&gt;
  
      &lt;?php echo $form-&gt;labelEx($modelYoutubeVideo,'description'); ?&gt;
      &lt;?php echo $form-&gt;textField($modelYoutubeVideo,'description',array('size'=&gt;50,'maxlength'=&gt;250)); ?&gt;
      &lt;?php echo $form-&gt;error($modelYoutubeVideo,'description'); ?&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Movie Controller - </p>
  
  <pre><code>    public function actionCreate()
  {
      $model=new Movie;  // this is my model related to table
  
      $modelYoutubeVideo=new YoutubeVideo;
  
  
  
      if(isset($_POST['Movie']))
      {
  
          $model-&gt;attributes=$_POST['Movie'];
  
          // Save Title Image and store file name in database
  
          $_POST['Movie']['title_image'] = $model-&gt;title_image;
          $uploadedFile=CUploadedFile::getInstance($model,'title_image');
          $name = $uploadedFile-&gt;getName();
          $model-&gt;title_image = $name;
  
          if($model-&gt;save())
          {
  
              if(!empty($uploadedFile))  // check if uploaded file is set or not
              {
                  $uploadedFile-&gt;saveAs(Yii::getPathOfAlias('webroot')."/title_image/".$name);
              }
  
  
              $modelYoutubeVideo = new YoutubeVideo();
  
              $modelYoutubeVideo-&gt;attributes=$_POST['youtube_video'];
  
  
  
              // Redirect to admin
  
              $this-&gt;redirect(array('admin'));
  
          }
      }
  
      $this-&gt;render('create',array(
          'model'=&gt;$model,
          'modelYoutubeVideo'=&gt;$modelYoutubeVideo,
  
      ));
  }
  </code></pre>