data storing error

Hello Friends. I am newbie to yii. I am using two models one is ‘Invoice’ and other is ‘InvoiceItems’ where InvoiceItem is the child of Invoice. In controller I am using


public function actionCreate()

  {

    $model=new Invoices;


    // Uncomment the following line if AJAX validation is needed

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


    if(isset($_POST['Invoices']))

    {

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

      if($model->save()){

        if(!empty($_POST['Invoice']['invoice_items']))

          $this->saveChildModel('Invoiceitems',$_POST['Invoice']['invoice_items'],$model,'invoice_id');

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

        }

      }


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

      'model'=>$model,

    ));

   }



for creating the invoice and its items. The same thing is done for update and view. I am also getting the view in a single page. But my problem is the data entered in the 2nd view of the model means invoice_items it is not stored in the database. But whenever I am accessing the 2nd model means invoice_items view page and entering the values it is storing all the input values. So can some one tell me where is the problem.I want to store all the value of model A AND MODEL B on a single page.

whether the data sent actually exist.




 if(!empty($_POST['Invoice']['invoice_items']))



check with




print_r($_POST['Invoice']['invoice_items'])



Nope it is not showing any error or anything.Thanks for reply.Please help me to solve this prob

Any one please help me out.

You need to explain a bit more your problem and what results you get here…

for


print_r($_POST['Inovice']['invoice_items']

you wrote that you don’t get any error or anything

but

this is just a PHP statement to display the content of that variable… so you should see your data here… if you don’t see anything than you dont have any data here to be saved in the first place…

if you get here the data to be saved… than the problem is in the saveChildModel() this is not a Yii function and you did not post it’s code here…

Ok so here is my dode for invoice controller


<?php


class InvoicesController extends Controller

{

  /**

   * @var string the default layout for the views. Defaults to '//layouts/column2', meaning

   * using two-column layout. See 'protected/views/layouts/column2.php'.

   */

  public $layout='//layouts/column2';


   private $_model;


   private function saveChildModel($child_model_name, $setter_data, $model, $join_key) {

    $setter_ids = array();

    foreach($setter_data as $setter_data_item) {

      if( !empty($setter_data_item['id']) )

        $setter_ids[] = $setter_data_item['id'];

    }

    

    eval('$old_child_model_data='.$child_model_name.'::model()->findAllByAttributes( array($join_key => $model->id) );');

    

    # Deletes None Posted Data

    

    foreach( $old_child_model_data as $key => $val ) {

      if(!in_array( $val->id, $setter_ids )) {

        eval($child_model_name.'::model()->deleteByPk( $val->id );');

      }

    }

    

    # Updates Posted Data

    foreach($setter_data as $setter_data_item) {

      if( !empty($setter_data_item['id']) )

        eval('$setter_data_object='.$child_model_name.'::model()->findByPk($setter_data_item["id"]);');

      else

        $setter_data_object=new $child_model_name;

      $setter_data_object->$join_key=$model->id;

      foreach($setter_data_item as $field_name => $field_value) {

        if( $field_name != 'id' )

          $setter_data_object->$field_name = $field_value;

      }

      $setter_data_object->save();

    }

 }




  /**

   * @return array action filters

   */

  public function filters()

  {

    return array(

      'accessControl', // perform access control for CRUD operations

    );

  }


  /**

   * Specifies the access control rules.

   * This method is used by the 'accessControl' filter.

   * @return array access control rules

   */

  public function accessRules()

  {

    return array(

      array('allow',  // allow all users to perform 'index' and 'view' actions

        'actions'=>array('index','view'),

        'users'=>array('*'),

      ),

      array('allow', // allow authenticated user to perform 'create' and 'update' actions

        'actions'=>array('create','update'),

        'users'=>array('@'),

      ),

      array('allow', // allow admin user to perform 'admin' and 'delete' actions

        'actions'=>array('admin','delete'),

        'users'=>array('admin'),

      ),

      array('deny',  // deny all users

        'users'=>array('*'),

      ),

    );

  }


  /**

   * Displays a particular model.

   * @param integer $id the ID of the model to be displayed

   */

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


    // Uncomment the following line if AJAX validation is needed

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


    if(isset($_POST['Invoices']))

    {

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

      if($model->save()){

         if(!empty($_POST['Invoices']['invoice_items']))

            $this->saveChildModel('InvoiceItems',$_POST['Invoices']['invoice_items'],$model,'invoice_id');

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

          }

      }

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

      'model'=>$model,

    ));

  }


  /**

   * Updates a particular model.

   * If update is successful, the browser will be redirected to the 'view' page.

   * @param integer $id the ID of the model to be updated

   */

  public function actionUpdate($id)

  {

    $model=$this->loadModel($id);


    // Uncomment the following line if AJAX validation is needed

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


    if(isset($_POST['Invoices']))

    {

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

      if($model->save()){

        if(isset($_POST['Invoices']['invoice_items']))

          $this->saveChildModel('InvoiceItems',$_POST['Invoices']['invoice_items'],$model,'invoice_id');

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

        }

      }


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

      'model'=>$model,

    ));

  }


  /**

   * Deletes a particular model.

   * If deletion is successful, the browser will be redirected to the 'admin' page.

   * @param integer $id the ID of the model to be deleted

   */

  public function actionDelete($id)

  {

    if(Yii::app()->request->isPostRequest)

    {

      // we only allow deletion via POST request

      $this->loadModel($id)->delete();


      // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

      if(!isset($_GET['ajax']))

        $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));

    }

    else

      throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');

  }


  /**

   * Lists all models.

   */

  public function actionIndex()

  {

    $dataProvider=new CActiveDataProvider('Invoices');

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

      'dataProvider'=>$dataProvider,

    ));

  }


  /**

   * Manages all models.

   */

  public function actionAdmin()

  {

    $model=new Invoices('search');

    $model->unsetAttributes();  // clear any default values

    if(isset($_GET['Invoices']))

      $model->attributes=$_GET['Invoices'];


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

      'model'=>$model,

    ));

  }


  /**

   * Returns the data model based on the primary key given in the GET variable.

   * If the data model is not found, an HTTP exception will be raised.

   * @param integer the ID of the model to be loaded

   */

  public function loadModel($id)

  {

    $model=Invoices::model()->findByPk($id);

    if($model===null)

      throw new CHttpException(404,'The requested page does not exist.');

    return $model;

  }


  /**

   * Performs the AJAX validation.

   * @param CModel the model to be validated

   */

  protected function performAjaxValidation($model)

  {

    if(isset($_POST['ajax']) && $_POST['ajax']==='invoices-form')

    {

      echo CActiveForm::validate($model);

      Yii::app()->end();

    }

  }

}

After doing so in controller I am just posting the values in form but it is not storin any thing in the data base.What is the error here??

I just took a look at your code and saw that you call eval() 3 times in one function… this is really bad practice…

Anyway…

you need to debug a bit your code top find what is wrong, I can just give you some ideas to test… but it’s up to you to find the error…

have you checked the print_r values?

do you get any data or the child records?

did you check that the validation is OK for the child model?

Ya I made print_r but nothing is coming…can u tell me what is the problem…How to solve this?

So… As you don’t get any data in $_POST[‘invoices’][‘invoice_items’] posting the source code of your whole controller was not needed at all…

How do you collect these data?