2

I want to sava the record but in the for loop the next id is null, how can i save the records with this condition, the loop will be stop when it find null and save the previous records.

 foreach($record['ProductPlan'] as $value=>$key)

{
 $ProductsUser = array('product_plan_id'=>$key,'user_node_id'=>$userNodeId, 'is_active'=>1);
                            $this->$model->UserNode->ProductPlansUserNode->create();
                            $this->$model->UserNode->ProductPlansUserNode->save($ProductsUser);

                        }   

and the error is Integrity constraint violation: 1048 Column 'product_plan_id' cannot be null
and in the array

   '2'
array(
'product_plan_id' => '2',
'user_node_id' => '26',
'is_active' => (int) 1
)
'3'
array(
'product_plan_id' => '3',
'user_node_id' => '26',
'is_active' => (int) 1
)
'' // This is null how can i save previous records before null?

3 Answers 3

3

I think you are confused between $value and $key, it should be used vice versa

You need to add a check for empty or null array before inserting in to table Try this

 foreach($record['ProductPlan'] as $value=>$key){
   If($key){
   $ProductsUser = array('product_plan_id'=>$key,'user_node_id'=>$userNodeId, 'is_active'=>1);
   $this->$model->UserNode->ProductPlansUserNode->create();
   $this->$model->UserNode->ProductPlansUserNode->save($ProductsUser);  
  }

}   
3

As your error says ,

Integrity constraint violation: 1048 Column 'product_plan_id' cannot be null

You have to alter this field to allow null in db to save null records. And if you want db to descide it self a unique key for that column make it AUTO_INCREMENT. So it will take the next key if you pass null for that column.

3

Just use normal if . Here is the sample code:

 foreach ($record['ProductPlan'] as $value => $key) {
    if (isset($key) && $key) {
       $ProductsUser = array('product_plan_id' => $key, 'user_node_id' => $userNodeId, 'is_active' => 1);
       $this->$model->UserNode->ProductPlansUserNode->create();
       $this->$model->UserNode->ProductPlansUserNode->save($ProductsUser);
    }
 }    
4
  • i dont want to insert null value in table, how can i do this?
    – Usman Ali
    Feb 13, 2013 at 8:08
  • if the product_plan_id column is AUTO_INCREMENT , it will get incremented automatically after you passing value null, NULL will not be inserted in the database
    – sandip
    Feb 13, 2013 at 8:11
  • 1
    primary key is product_plan_user_node_id and the foreign keys are product_plan_id and user_node_id, the foreign key and primary key in my table is not null. I want to add data without null value.
    – Usman Ali
    Feb 13, 2013 at 9:12
  • when i change product_plan_id is null in table it insert the record with null value, i dont' want to do insert record with null value, only those records whose product_plan_id is not null. hope you understand.
    – Usman Ali
    Feb 13, 2013 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.