Cannot associate user role with user - cakephp

I have a list of existing user roles in database for example like this:
+----+-------+-----------+
| id | level | label |
+----+-------+-----------+
| 1 | 0 | admin |
| 2 | 1 | moderator |
| 3 | 2 | blogger |
+----+-------+-----------+
And I would like to attach a role to a user when its created. What I've tried is following:
In my BcUsersController:
$bcUser = $this->BcUsers->patchEntity($bcUser, $this->request->data,
[
'associated' => [
'BcUserInfos',
'BcUserRoles'
]
]
);
if ($this->BcUsers->save($bcUser))...
Associations:
//BcUserRolesTable
$this->belongsToMany('BcUsers', [
'className' => 'BcUsers',
'foreignKey' => 'role',
'propertyName' => 'BcUserRoles'
]);
//BcUsersTable
$this->hasMany('BcUserRoles', [
'className' => 'BcUserRoles',
'foreignKey' => 'id',
'bindingKey' => 'role',
'propertyName' => 'BcUserRoles',
'joinTable' => 'bc_user_roles'
]);
And this is how I try to inject userRoleId into a user table:
<?= $this->Form->input('BcUserRoles.level', ['type' => 'hidden', 'value' => '0']); ?>
or
<?= $this->Form->input('BcUserRoles.id', ['type' => 'hidden', 'value' => '1']); ?>
When I try to save it gives me no error just refreshes a page and with post data inside fields. If I remove all the associations code, it saves user + userInfo. What am I missing here?
EDIT
I just found an error in my $bcUser variable:
'[errors]' => [
'role' => [
'_required' => 'This field is required'
]
],
And I think thats why it does not save BcUser, but also userRole associations is empty:
'BcUserRoles' => [],
so it wouldnt save userRole to user table even if I would would remove
->requirePresence('role', 'create') from BcUsersTable, would it ?
Just tested. It throws me SQL error because role isnt in query parameters and in database it has to be there, null is not allowed. All users should have a role.

Looks like there are a couple issues here.
Associations
If BcUsersTable contains the foreign key for BcUserRoles then is it a belongsTo relationship.
belongsTo: the current model contains the foreign key.
http://book.cakephp.org/3.0/en/orm/associations.html#belongsto-associations
Equally this would also mean that BcUserRoles has a hasMany relationship to BcUsers not a belongsTo relationship.
hasMany: the other model contains the foreign key.
http://book.cakephp.org/3.0/en/orm/associations.html#hasmany-associations
Post data
I assume that the foreign key for BcUserRolesTable in the BcUsersTable is role. That means you will need to submit a value for role that is the id of the user role you wish to associate.
The following would create a form element to do that:
$this->Form->input('role', ['type' => 'hidden', 'value' => $useRoleId])
Where $userRoleId is the id of the role you wish to associate.

Try updating your belongsTo Relations
$this->belongsTo('BcUsers', [
'className' => 'BcUsers',
'foreignKey' => 'role',
'bindingKey' => 'id',
'propertyName' => 'BcUserRoles'
]);
make it
$this->belongsTo('BcUserRoles', [
'className' => 'BcUsers',
'foreignKey' => 'role',
'bindingKey' => 'id',
]);
Hope your problem will be solved

Related

How to insert a new record in an associated belongsTo table instead of editing the associated one

In a edit-template, I created 2 forms. The first to edit a record from 'Table1' and a second form to add an record to the associated (belongsTo) table 'Table2'. I don't want to edit the associated record but add a new one in Table2 and change the bindingkey of the record from 'Table1'.
In the controller I use patchEntity(), but this way the original linked record in 'Table2' is edited.
The code for the second form look like this.
<?= $this->Form->create($machine, ['url' => ['action' => 'edit_foto']]);?>
<?= $this->Form->controls([
'id' => ['type' => 'hidden'],
'foto.omschrijving' => [
'type' => 'text',
'label' => __('naam van de machine'),
'value' => $result->type_machine
], []);?>
<?= $this->Form->button(__('save'), ['type' => 'submit']); ?>
<?= $this->Form->end(); ?>
In the controller for 'Table1' I use something like this.
public function editFoto($id) {
$data = $this->getRequest()->getData();
$machine = $this->Machines->get($id, [
'contain' => [
'Foto'
]
]);
if ($this->request->is([
'patch',
'post',
'put'
])) {
$machine = $this->Machines->patchEntity($machine, $data);
if ($this->Machines->save($machine)) {
$this->Flash->success(__('Machine {naam} has been saved.', [
'naam' => $machine['naam']
]));
return $this->redirect(['action' => 'edit', $id]);
}
}
}
How can I prevent the controller from editing the original record and instead force to add a new record in 'Table2' and change the bindingkey in 'Table1'?
Help is much appreciated.
added information:
the data send by the form looks like:
[
'id' => '87',
'foto' => [
'omschrijving' => 'WAKER 135 - ter - testje',
'foto' => [
'tmp_name' => 'C:\xampp\tmp\php6CFD.tmp',
'error' => (int) 0,
'name' => 'MAKITA DTD153RTJ accuschroevendraaier.jpg',
'type' => 'image/jpeg',
'size' => (int) 188971
],
'dir_id' => '5c9dd428cc4ef'
]
]
Instead of
$machine = $this->Machines->patchEntity($machine, $data);
$this->Machines->save($machine);
you should use
$machine = $this->Machines->newEntity($data, [
'associated' => ['Fotos']
]);
$this->Machines->save($machine);
to save the data. If the associations between Table1 and Table2 are correct in the models, this will also update the binding keys.
Make sure, the format of the ids in FormHelper is correct. See the docs, how to handle Saving With Associations. You can see this in $data. When submitting the form, $data should be in the following format:
$data = [
'id' => '...',
'foto' => [
// ... model data
],
];

Saving id of hasOne association

I have a table that looks like this:
,====,==============,============,==========,
| id | contact_from | contact_to | message |
|====|==============|============|==========|
| 1 | 1 | 2 | some msg |
| 2 | 2 | 1 | reply |
'----'--------------'------------'----------'
I create a new row, doing this:
public function add()
{
$message = $this->Messages->newEntity();
if ($this->request->is('post') && $this->request->is('ajax')) {
$data = $this->request->getData();
$data['contact_to'] = (int)$data['contact_to'];
$data['contact_from'] = (int)$this->Auth->user('id');
$message = $this->Messages->patchEntity($message, $data);
if ($this->Messages->save($message)) {
echo json_encode(['status' => 'success']);
exit;
}
echo json_encode(['status' => 'error']);
exit;
}
}
And this is my hasOne association:
$this->hasOne('ContactFrom', [
'className' => 'Contacts',
'foreignKey' => 'id',
'bindingKey' => 'contact_from',
'joinType' => 'INNER',
'propertyName' => 'contact_from'
]);
$this->hasOne('ContactTo', [
'className' => 'Contacts',
'foreignKey' => 'id',
'bindingKey' => 'contact_to',
'joinType' => 'INNER',
'propertyName' => 'contact_to'
]);
As you can see, I pass an ID to a new row, however it saves everything, except the id's. When I debug($message) after the patchEntity call, it comes back like this:
object(App\Model\Entity\Message) {
'message' => 'asdfasdf',
'date_sent' => object(Carbon\Carbon) {},
'[new]' => true,
'[accessible]' => [
'contact_to' => true,
'contact_from' => true,
'message' => true,
],
'[dirty]' => [
'message' => true,
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Messages'
}
It drops my ID's. I assume it's because I need to pass the Entity to it, but to save on db calls, how can I make it save the contact_to and contact_from id's to the table?
The names that you've choosen are causing a clash in the marshaller.
You cannot use the same name for the binding/foreign key and the property name, these two need to be different, as the former are ment to hold an identifier, and the latter is ment to hold either an entity, or an array that can be marshalled into an entity - neither of that applies to the value that you are passing, hence it will be discard.
You should ideally follow the CakePHP naming conventions, and append _id to your columns, ie name them contact_from_id and contact_to_id.
See also
Cookbook > CakePHP at a Glance > CakePHP Conventions > Database Conventions

Cakephp 3 : How to fetch joing table associated data?

I have a table name countries , countries has relation with users, users has relation with MoneyTransferTransactions
In MoneyTransferTransactions view I need fetch country name.
I already join Users table in MoneyTransferTransactionsTable by below code
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
In user table I also used inner join like below code
$this->belongsTo('Countries', [
'foreignKey' => 'country_id',
'joinType' => 'INNER'
]);
In MoneyTransferTransactions controller I have used below code to fetch all data with associated data.
$this->paginate = [
'contain' => ['Users','TransferOptions'],
'conditions'=> ['MoneyTransferTransactions.status'=>1],
'order' =>['MoneyTransferTransactions.id'=>'DESC']
];
I have used var_dump in index.ctp , I gotted country id from users table but not got country name from countries table. How can I get country name from MoneyTransferTransactions>index.ctp ?
Add association between Users and Countries in 'contain'
$this->paginate = [
'contain' => ['TransferOptions', 'Users' => ['Countries']],
'conditions' => ['MoneyTransferTransactions.status' => 1],
'order' => ['MoneyTransferTransactions.id' => 'DESC']
];

CakePhp 3 Save data in belongsTo

So, i have problem with saving data to DB
Model Table is
...
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
...
I want to store new Address in row and data structure is
object(App\Model\Entity\Address) {
'type' => (int) 1,
'Users' => [
'user_id' => '9'
],
'short_name' => 'Test',
'long_name' => 'test',
'additional_name' => 'test',
I have tried in controller
if ($this->request->is('post')) {
$address = $this->Addresses->patchEntity($address, $this->request->data, ['associated' => ['Users.user_id']]);
if ($this->Addresses->save($address)) {
$this->Flash->success(__('The address has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The address could not be saved. Please, try again.'));
}
And in my template view this is input field
echo $this->Form->input('Users.user_id', ['type' => 'select', 'options' => $groups, 'class' => 'form-control']);
echo $this->Form->input('short_name', ['class' => 'form-control']);
echo $this->Form->input('long_name', ['class' => 'form-control']);
So, what can I do? How to reorganize data stucture?
Thank you!
You are using the CakePHP <= 2.x styled naming conventions for your inputs, that's not how it works with CakePHP 3.x anymore.
Please refer to the docs on how to create inputs for associated data:
Cookbook > Views > Helpers > Form > Creating Inputs for Associated Data
You have to use the appropriate property name of the association, just as it appears in the entity when reading data. For belongsTo associations, this is by default the singular, underscored variant of the association alias, so in your case user, ie
echo $this->Form->input('user.user_id', [
'type' => 'select',
'options' => $groups,
'class' => 'form-control'
]);
See also
Cookbook > Database Access & ORM > Associations > BelongsTo Associations
Cookbook > Database Access & ORM > Saving Data > Saving BelongsTo Associations
Cookbook > Views > Helpers > Form > Field Naming Conventions

CakePHP - Add custom values to User Object

I have added 4 more columns to my CakePHP Users table and I am trying to figure out how I can include these columns in the $this->Auth->user() object.
I've added the column information to my Users Model and Entity but still no joy. Currently my user object looks like this;
[
'id' => (int) 1,
'username' => 'admin',
'name' => 'Web Admin',
'email' => 'webteam#',
'role' => 'admin',
'created' => object(Cake\I18n\Time) {
'time' => '2016-02-09T16:04:46+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'modified' => object(Cake\I18n\Time) {
'time' => '2016-02-12T08:53:16+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
}
]
Where is this object created and is there a way I can add my custom values to it, without editing core CakePHP files?
Thanks for your help .
By default the built-in authenticators will fetch all fields in the tables schema.
You most probably just forgot to clear your cache (tmp/cache/models), which you should do whenever you make changes to your schemas.
In case one would want to specify what fields are being fetched, a custom finder would be needed.
See Cookbook > Controllers > Components > Authentication > Customizing Find Query
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'finder' => 'auth'
]
],
]);
In your UsersTable class
public function findAuth(\Cake\ORM\Query $query, array $options)
{
return $query
->select(['id', 'username', 'password', 'column_x', 'column_y']);
}
It should be noted that the fields required for authentication must always be included, ie like username and password!

Resources