Foreign Key Store In The Database When Selected From Dropdown List

Hi,

I have two tables in my database named as

products and sales

product table is something like this




   ================

      product

   ================

   +   id          +

   + product_name  +

   + cost_price    +

   +selling_price  +

   + created_by    +

   + updated_by    +

   + created_at    +

   + updated_at    +

   +++++++++++++++++

   

   

   =================

          Sales

   =================

   + id            +

   + product_id     +

   + price          +

   + created_by    +

   + updated_by    +

   + created_at    +

   + updated_at    +

   +++++++++++++++++



For all the above two tables I have done the Models and controllers from gii tools(crud)

After that I just made relations in Sales model. The relation is like this




  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(

      'product' => array(self::HAS_MANY,'Products','product_id'),

    );

  }

  

  After that I just rendered the Products model in Sales model.

  Sales controller action create is something like this

    public function actionCreate()

  {

    $model=new Sales;

    $products = new Products;


    // Uncomment the following line if AJAX validation is needed

    // $this->performAjaxValidation($model);


    if(isset($_POST['Sales'], $_POST['Products']))

    {

      $model->attributes=$_POST['Sales'];

      $products->attributes = $_POST['Products'];

      $valid = $model->validate();

      $valid = $products->validate();


      if($valid)

      {

        $products->save(false);

        $model->product_id = $products->getPrimaryKey();

        $model->save(false);


        $this->redirect(array('view','id'=>$model->id));

      }

    }


    $this->render('create',array(

      'model'=>$model,

      'products' =>$products,

    ));

  }



and in view I am rendering the files like this




   <div class="row">

    <?php echo $form->labelEx($products,'product_name'); ?>

    <?php echo $form->dropdownList($products,'product_name', CHtml::listData(Products::model()->findAll(), 'id', 'product_name'), array('empty'=>array('choose'=>'---Select One---'))); ?>

    <?php echo $form->error($products,'product_name'); ?>

  </div>


  <div class="row">

    <?php echo $form->labelEx($model,'price'); ?>

    <?php echo $form->textField($model,'price'); ?>

    <?php echo $form->error($model,'price'); ?>

  </div>



Here I am doing the dropdown list of all the available products from the products table. After that when I am doing save the total form.

The product_id inside sales table showing NULL. I want the foreignkey in that place when the selected product is selected from the dropdown. Kindly tell me where I am doing the wrong. Any help and suggestions will be really appreciable. Thanks

After saving product information use it primary key like





$product->id and assign it to your sales model attribute.

like


$model->product_id = $product->id;



And if you are saving multiple records then save data in loop.

Hi,

Thanks for the reply but after all changed my code like yours it became like this




public function actionCreate()

  {

    $model=new Sales;

    $products = new Products;


    // Uncomment the following line if AJAX validation is needed

    // $this->performAjaxValidation($model);


    if(isset($_POST['Sales'], $_POST['Products']))

    {

      $model->attributes=$_POST['Sales'];

      $products->attributes = $_POST['Products'];

      $valid = $model->validate();

      $valid = $products->validate();


      if($valid)

      {

        $products->save(false);

        $model->product_id = $product->id;

        $model->save(false);


        $this->redirect(array('view','id'=>$model->id));

      }

    }


    $this->render('create',array(

      'model'=>$model,

      'products' =>$products,

    ));

  }




But when I am clicking on create it is not saving anothing, nothing happening here… any help?

Could you please share your code from view file also.

Here is the code from view file(_form.php). I have already shared this


<div class="row">

    <?php echo $form->labelEx($products,'product_name'); ?>

    <?php echo $form->dropdownList($products,'product_name', CHtml::listData(Products::model()->findAll(), 'id', 'product_name'), array('empty'=>array('choose'=>'---Select One---'))); ?>

    <?php echo $form->error($products,'product_name'); ?>

  </div>


  <div class="row">

    <?php echo $form->labelEx($model,'price'); ?>

    <?php echo $form->textField($model,'price'); ?>

    <?php echo $form->error($model,'price'); ?>

  </div>