0

I have 'seller_businessestable andseller_business_categories` table. I am using a single form to save values to sellers and seller_businesses.

The form is like

<?= $this->Form->create() ?>
<?= $this->Form->input('company_name') ?>
<?= $this->Form->input('logo', ['type' => 'file']) ?>
<?= $this->Form->input('seller_business_categories.category_id') ?>
<?= $this->Form->button('Submit', ['type' => 'submit']) ?>
<?= $this->Form->end() ?>

The seller_business_categories has a column seller_id and I have to save the logged in user id in it.

I know to save value to table seller_businesses in controller like

$seller_business = $this->SellerBusinesses->newEntity();
$seller_business->seller_id = $this->Auth->user('id');
$this->SellerBusinesses->patchEntity($seller_business, $this->request->data, [
  'associated' => ['SellerBusinessCategories']
]);

But how to save default value to associated model SellerBusinessCategories within controller ?

Gaurav
  • 131
  • 12

1 Answers1

0

You need make relationship

SellerBusinessCategorie.php MODEL

public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('seller_business_categories');

        $this->hasMany('seller_businesses', [
            'foreignKey' => 'seller_id'
        ]);
    }

SellerBusinesses.php MODEL

public function initialize(array $config)
        {
            parent::initialize($config);
    $this->belongsTo('seller_business_categories', [
                'foreignKey' => 'category_id',
                'joinType' => 'INNER'
            ]);

}

Controller

 $seller_business = $this->SellerBusinesses->newEntity();
    $seller_business->seller_id = $this->Auth->user('id');
    $this->SellerBusinesses->patchEntity($seller_business, $this->request->data]);

NOW TRY..it works

Kamlesh Gupta
  • 505
  • 3
  • 17
  • I think I'm not clear to you. I want to set value to associated table. This could be anything (not only associated foreign key). `Ex : I have to set **title** to associated table from controller` – Gaurav Aug 24 '16 at 17:24