Reputation: 41
For simplicity sake, I have simplified my tables to get my core need across.
I have two tables: admins and users. The admin table simple holds certain information that is not entirely relevant to the question.
Admins:
+----+---------+
| id | user_id |
+----+---------+
| 1 | 20 |
+----+---------+
| 2 | 25 |
+----+---------+
| 3 | 27 |
+----+---------+
Users:
+----+--------+-----------+
| id | name | surname |
+----+--------+-----------+
| 20 | Name 1 | Surname 1 |
+----+--------+-----------+
| 25 | Name 2 | Surname 2 |
+----+--------+-----------+
| 27 | Name 3 | Surname 3 |
+----+--------+-----------+
The idea is that when a super admin saves a new user, the fill in a form, being a user registration for of sorts and upon saving it saves the user and creates the admin record with the newly created user id.
Obviously the admin record cannot be created without the user account existing as it requires the user id. Cake simple returns an error saying "user_id is required".
This is my form:
<?= $this->Form->create($user) ?>
<fieldset>
<legend><?= __('Add Admin') ?></legend>
<?php
echo $this->Form->input('user.name');
echo $this->Form->input('user.surname');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
I have tried creating a user with an admin association and the other way around:
$admin = $this->Admins->newEntity();
if($this->request->is('post')) {
$this->Admins->patchEntity($admin, $this->request->data,[
'associated' => ['Users']
]);
$savedAdmin = $this->Admins->save($admin);
}
and
$user = $this->Admins->Users->newEntity();
if($this->request->is('post')) {
$this->Admins->Users->patchEntity($user, $this->request->data,[
'associated' => ['Admins']
]);
$savedUser = $this->Admins->Users->save($user);
}
I do not what to save separate entities because I know it is possible to saved associated data, I'm just not really how to saved the admin record as it relies on the user record.
Upvotes: 1
Views: 1236
Reputation: 41
First, create your admin entity and then associate users (as admin requires the user id). Once you have done that create your new user entity and assign it to a variable.
The, manually assign the foreign key the newly created user, and Cake will figure out it needs to assign it the primary based on your model. Finally, save your admin, and it will automatically save your associated entity.
That's why it is important to define your belongs to and has one in the relevant models as mentioned in the previous answer.
if ($this->request->is('post')) {
$admin = $this->Admins->newEntity($this->request->data, [
'associated' => ['Users']
]);
$user = $this->Admins->Users->newEntity();
$admin->user_id = [$user];
$saved = $this->Admins->save($admin);
}
Upvotes: 0
Reputation: 93
It sounds like it might make more sense to have a boolean field in the user table that defines whether a user is an admin or not.
That being said, you may need to define the relationship in your models if it is not there already. Technically, based on your table names and and columns (user_id) Cake should bake this in for you, but I don't remember if it's conventional to have plural model names (Users, Admins), so this may not have happened.
User:
public $belongsTo = [
'Admins' => [
'classname' => 'Admins', //are your models really plural?
'foreignKey' => 'user_id',
];
Admin
public $hasOne= [
'Users' => [
'classname' => 'Users',
'foreignKey' => 'user_id',
];
Then, I think you'd need to specify some information about the admin, or you won't have any data to save the association. In the view:
echo $this->Form->input('admins.field1');
echo $this->Form->input('admins.field2');
echo $this->Form->input('admins.field3');
Note these fields can be hidden if you don't want anything inputted.
To save, make a new user from the request data (your form creates a user, but you could create an admin with the form, and then add inputs like user.name and user.surname). Controller:
$user = $this->Admins->Users->newEntity();
if($this->request->is('post')) {
$this->Admins->Users->patchEntity($user, $this->request->data, [
'associated' => ['Admins']
]);
$this->Admins->Users->save($user);
}
Upvotes: 1