Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
864 views
in Technique[技术] by (71.8m points)

php - CakePHP 3: saving hasOne association ($_accessible not set)

I have read several Stack Overflow posts and the documentation pages about saving associated data in CakePHP 3, but I can't get my code to work. When creating a new Organisation, I also want to save the data of the new account (NewAccount) that belongs to that Organisation.

Below is a reproducible part of my code. The validation of the Organisations model are executed and if passed, the data is saved. The NewAccounts data does not get saved, and is not even being validated. I have already checked the naming conventions in all of these files, but I can't find anything that might be a problem anymore.

// Model/Table/OrganisationsTable.php
class OrganisationsTable extends Table
{
    public function initialize(array $config)
    {
        parent::initialize($config);
        $this->hasOne('NewAccounts');
    }
}

// Model/Table/NewAccountsTable.php
class NewAccountsTable extends Table
{
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->belongsTo('Organisations', [
            'foreignKey' => 'organisation_id'
        ]);
    }
}

// Template/Organisations/admin_add.ctp
echo $this->Form->create($organisation);
echo $this->Form->control('new_account.email', [
    'label' => __('Email address'),
    'class' => 'form-control',
]);
echo $this->Form->control('new_account.name', [
    'label' => __('Name'),
    'class' => 'form-control',
]);
echo $this->Form->control('name', [
    'label' => __('Organisation name'),
    'div' => 'form-group',
    'class' => 'form-control',
]);

// Controller/OrganisationsController.php
class OrganisationsController extends AppController
{
    public function adminAdd() {
        $organisation = $this->Organisations->newEntity();

        if($this->request->is('post')) {
            $this->Organisations->patchEntity($organisation, $this->request->getData(), [
                'associated' => ['NewAccounts']
            ]);
            if ($this->Organisations->save($organisation)) {
                $id = $organisation->id;
                debug("Success!");
            }
            else {
                debug("Error");
            }
        }

        $this->set(compact('organisation'));
    }
}

CakePHP documentation I have referenced:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I forgot to make $organisation->new_account accessible:

// Model/Entity/Organisation.php
class Organisation extends Entity
{
    protected $_accessible = [
        // ...
        'new_account' => true,
    ];
}

By doing this, the field is marked as to be safely assigned.

Entities: Mass Assignment (book) and EntityTrait::$_accessible (API docs)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...