skip to Main Content
Code that causes problem
foreach ($items as $itemData) {
     $item = new PurchaseItem();
     $item->internal_product_id = (int)$itemData['internal_product_id'];
     $item->quantity = (float)$itemData['quantity'];
     $item->total = (float)$itemData['total'];
     $item->purchase_id = $purchase->id;
     if (!$item->save()) {
        throw new Exception('Error saving the purchase item.');
      }
}
Table Structure
CREATE TABLE `purchase_items` (
  `id` int NOT NULL,
  `purchase_id` int DEFAULT NULL,
  `internal_product_id` int DEFAULT NULL,
  `quantity` decimal(10,2) NOT NULL,
  `total` double(10,3) DEFAULT NULL
)

I’ve tried to change the variable name for unique. also ensure the field name . But Results are same & not saving data in Purchase_items table

2

Answers


  1. Chosen as BEST ANSWER

    Here is the problem i got on the code. In modal all the field was declare in public property that's why getting the error. after remove them it works fine.

    class CompraItem extends yiidbActiveRecord
    {
        public $internal_product_id;
        public $purchase_id;
        public $quantity;
        public $total;
    }
    

  2. Is exampled structure of table final/full?

    One of possible problem is absent of AUTO_INCREMENT for PRIMARY KEY (id).

    Please, change structure of table to

    CREATE TABLE `purchase_items` (
      id int NOT NULL AUTO_INCREMENT,
    
      `purchase_id` int DEFAULT NULL,
      `internal_product_id` int DEFAULT NULL,
      `quantity` decimal(10,2) NOT NULL,
      `total` double(10,3) DEFAULT NULL,
      
      PRIMARY KEY (id),
      KEY (purchase_id),
      KEY (internal_product_id)
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search