Issue with relation table belongs to many

Hello there,

I have an issue with the following case :

I have a table users :

id, profile_id, name, created, modified

I have a table profiles :

id, first_name, last_name, gender

I have a businesses table :

id, name, created, modified

I have a relation table many to many to link profiles to businesses : businesses_profiles

id, business_id, profile_id, created, modified

When i try to create a new business, I would like to link directly logged in user profile id to the business I’m creating.

in my profileTable, I’ve added in initialize() :

  $this->belongsToMany('Businesses', [
    'alias' => 'Businesses',
    'foreignKey' => 'profile_id',
    'targetForeignKey' => 'business_id',
    'joinTable' => 'businesses_profiles'
]);

in my businessesTable, I’ve also put in initialize() method:

 $this->belongsToMany('Profiles', [
    'alias' => 'Profiles',
    'foreignKey' => 'business_id',
    'targetForeignKey' => 'profile_id',
    'joinTable' => 'businesses_profiles'
]);

In each entities Business & Profile, I put respectively in right context :

protected $_accessible = [
'*' => true,
'id' => false,
'businesses' => true,
'_joinData' => true

];

and :

 protected $_accessible = [
'name' => true,
'slug' => true,
'active' => true,
'hash' => true,
'data' => true,
'approved' => true,
'created' => true,
'modified' => true,
'profiles' => true,
'_joinData' => true

];

Nothings work about saving in my businesses_profiles table.

Thanks in advance for your help, Best,

Laurent.

you need to provide also how and what(->request->getData()) you are trying to save, what you gave is not enough

Thanks for your help Graziel.

Here is my function add :
public function add()
{
$business = $this->Businesses->newEntity();

    if ($this->request->is('post')) {

        $associated = [
            'associated' => [
                'Profiles'
            ]
        ];

        $business = $this->Businesses->patchEntity($business, $this->request->getData(), $associated);

        $business->set('hash');
        $business->set('active', 0);
        $business->set('slug');

     //   debug($business); die;

        if ($this->Businesses->save($business, $associated)) {
            $this->Flash->success(__('The business has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        else
        {
    debug($business->getErrors());
            $this->Flash->error(__('The business could not be saved. Please, try again.'));
        }
    }
    $this->set(compact('business'));
}

try adding `._joinData’ to associated as in
https://book.cakephp.org/3.0/en/orm/saving-data.html#saving-additional-data-to-the-join-table

$student = $this->Students->newEntity($data, [
    'associated' => ['Courses._joinData']
]);

Thanks Graziel, nothings more happens but I’ll read Cake documentation you point.
Thanks for your help. =)

Ok, I solved my issue this way

After persistence ( save() ), I used link method to make the association.
Now, on each business added, I have record maked in my businesses_profiles relation table. Great !
But : on my businesses_profiles table, I have the column admin to fill, how can I proceed to achieve that ?
By the method link ? Any possibilities ? Thanks !

Here is my add method

public function add()
{
    $business = $this->Businesses->newEntity();

    if ($this->request->is('post')) {

        $business = $this->Businesses->patchEntity($business, $this->request->getData());

        $business->set('hash');
        $business->set('active', 0);
        $business->set('slug');

        $profile = TableRegistry::getTableLocator()->get('Profiles')->get($this->_currentUser->profile_id);

        if ($this->Businesses->save($business)) {

            $this->Businesses->Profiles->link($business, [$profile]);
            $this->Flash->success(__('The business has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        else
        {
            debug($business->getErrors()); // debug
            $this->Flash->error(__('The business could not be saved. Please, try again.'));
        }
    }
    $this->set(compact('business'));
}

I found the answer in Cookbook, I share the info if it can be useful to others users :

https://book.cakephp.org/3.0/fr/orm/saving-data.html#enregistrements-avec-associations

Thanks all for your help, really appreciated. :slightly_smiling_face:

Here is my final code solution :

 public function add()
{
    $business = $this->Businesses->newEntity();

    if ($this->request->is('post')) {

        $business = $this->Businesses->patchEntity($business, $this->request->getData());

        $business->set('hash');
        $business->set('active', 0);
        $business->set('slug');

        $profile = TableRegistry::getTableLocator()->get('Profiles')->get($this->_currentUser->profile_id);

        if ($this->Businesses->save($business)) {

            $this->Businesses->Profiles->link($business, [$profile]);

            /* Profile is now linked to Business (trough business_profiles pivot), we need to set the user as admin of his own business */

            $business = $this->Businesses->get($business->id, [
                'contain' => ['Profiles'] // Required to access _joinData pivot properties
            ]);

            $business->profiles[0]->_joinData->admin = true;

            // Required because changing property directly
            $business->setDirty('profiles', true);

            $this->Businesses->save($business);

            // End update pivot table

            $this->Flash->success(__('The business has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        else
        {
            debug($business->getErrors()); // debug
            $this->Flash->error(__('The business could not be saved. Please, try again.'));
        }
    }
    $this->set(compact('business'));
}