phpcakephpcakephp-model

CakePHP - saving to more models but main fails


I am trying something more complicated. I have an Item which stores all general items, I have a Product which is an item and I have a Good which is a product and a item. So I have a form for entering value for the good and it shall save to all model related tables (items, products, goods). The reason for so many tables is because all tables shall have an id which is used later, example: product shall have its id which is used later for selling. Here is the controller:

class GoodsController extends AppController {

public function add(){
$this->Item->create();
            $this->request->data['Item']['code'] = $finalCode;
            $this->request->data['Item']['is_deleted'] = false;
            $item = $this->Item->save($this->request->data);

            if(!empty($item)){
                $this->request->data['Product']['item_id'] = $this->Item->id;
                $this->request->data['Good']['item_id'] = $this->Item->id;

                $this->Item->Product->save($this->request->data);
                $this->request->data['Good']['pid'] = $this->Product->id;
                $this->Item->Good->save($this->request->data);
            }
}

The Item is never saved but Product and Good are saved and they are all mapped well, ids are ok but Item is not even in the db, althrough Product and Good have item_id set to a value which should be next in the db. Even requested data looks good:

array(
    'Item' => array(
        'name' => 'Microcontrollers',
        'modified' => '2019-10-22 12:37:53',
        'created' => '2019-10-22 12:37:53',
        'id' => '120'
    ),
    'Good' => array(
        'status' => 'development',
        'is_for_distributors' => '1'
    ),
    'Product' => array(
        'project' => 'neqwww'
    )
)

Relations looks good:

class Good extends AppModel {

    public $belongsTo = array(
        'Item' => array(
            'className' => 'Item',
            'foreignKey' => 'item_id',
        ),
        'Product' => array(
            'className' => 'Product',
            'foreignKey' => 'pid',
        )
    );
}

class Product extends AppModel {

    public $hasOne = array(
        'Good' => array(
            'className' => 'Good',
            'foreignKey' => 'pid',
            'dependent' => false,
        ),
    );
}

class Item extends AppModel{

    public $hasOne= array(
         'Good' => array(
        'className' => 'Good',
        'foreignKey' => 'item_id',
        'dependent' => false,
    ),
    'Product' => array(
        'className' => 'Product',
        'foreignKey' => 'item_id',
        'dependent' => false,
    ),
    );

}

Solution

  • In the meantime i understood that this is just a needless complicating and I shall redesign my db.