Saving id of hasOne association - cakephp

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

Related

Conditionally reuse a `hasOne` association multiple times inside the same query

What I have
In my OrdersTable.php:
$this->hasOne('Total', [
'className' => 'App\Model\Table\TotalsTable',
'foreignKey' => 'order_id',
'propertyName' => 'Total'
]);
Actual totals table:
| id | order_id | type | value |
|----|----------|----------|-------|
| 1 | 1 | total | 100 |
| 2 | 1 | tax | 20 |
| 3 | 1 | shipping | 5 |
The structure and logic come from opencart/opencart and I have no control over that.
What I want
This is a non-functional concept:
$query = $ordersTable->find('all', array(
'contain' => array(
'TotalTax' => [
'associationName' => 'Total',
'conditions' => function($query) {
return $query->where([
'TotalTax.type' => 'tax',
]);
},
],
'TotalShipping' => [
'associationName' => 'Total',
'conditions' => function($query) {
return $query->where([
'TotalShipping.type' => 'shipping',
]);
},
],
),
));
Do you guys think something like this is possible?
UPD: Creating an association for each type isn't an option since there may be too many of them
If this functionality is something that will be reused with your codebase, I would implement this logic at the table level and have two different conditional associations:
In OrdersTable.php
$this->hasOne('TotalTax', [
'className' => 'Totals'
])
->setConditions(['TotalTax.type' => 'tax'])
->setDependent(true);
$this->hasOne('TotalShipping', [
'className' => 'Totals'
])
->setConditions(['TotalShipping.type' => 'shipping'])
->setDependent(true);
Then you can simply contain them in the query:
$query = $ordersTable->find()->contain(['TotalTax', 'TotalShipping'];
An example of this can be found in the CakePHP documentation

Cannot associate user role with user

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

how to save belongstomany checkboxes in cakephp

I have a set of checkboxes with a belongs to many relationship. I couldnt use a multiple checkbox option for various reasons. The table Tutors has a belongsToMany relationship with subjects table. I get the correct checkboxes checked when it loads and when I update the checboxes I get the correct checkbox data returned but how do i save this? The docs say i need to put the id's in an array for the correct Subjects to be checked . I tried a few things but I couldnt save the data. I dont get an error, just not saving.
//view
foreach ($subjects as $key => $item) {
$sub=$item->name;
$val=0;
foreach ($tutor->subjects as $key2 => $item2) {
$sub2=$item2->name;
if ($sub==$sub2){
$val=1;
break;
}
}
echo $this->Form->hidden('subjects.'.$key.'.id', ['value'=>$item->id]);
echo $this->Form->input('subjects.'.$key.'.checkedvalue', ['label'=>$sub,
'checked'=> $val,'type'=>'checkbox']);
}
//controller
public function edittest5($id = null)
{
$tutor = $this->Tutors->get($id, [
'contain' => ['AvailabilityForTutors','Subjects']
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$tutor = $this->Tutors->patchEntity($tutor, $this->request->data(),['associated' => ['AvailabilityForTutors','Subjects']] );
// $tutor->dirty('availability_for_tutors', true);
debug($tutor);
$this->Tutors->save($tutor,[
// 'atomic'=>false,
'validate' => false,
'associated' => ['Subjects']
]);
return $this->redirect(['action' => 'edittest5',$id]);
}
$subjects = $this->Tutors->Subjects->find('all', ['limit' => 200]);
returned data
'first_name' => 'drew',
'subjects' => [
(int) 0 => [
'id' => '1',
'checkedvalue' => '1'
],
(int) 1 => [
'checkedvalue' => '0'
],
(int) 2 => [
'checkedvalue' => '0'
],
(int) 3 => [
'checkedvalue' => '0'
],
(int) 4 => [
'id' => '5',
'checkedvalue' => '1'

CakePHP 3: Upload multiple files and save file names in associated model?

I need to create the upload process where the user can upload multiple files at once, using the file field with html5 multiple attr. Name of the file must be saved in the associated model.
I can run successfully upload one file and save the file name in the photos table, across the field:
echo $this->Form->file('photos.name');
But if I want to enable upload more photos with
echo $this->Form->input('title'); // post title
echo $this->Form->input('maintext'); // post main text,
... etc
echo $this->Form->file('photos[].name',['multiple'=>true]);
I get into the problem, and try to understand where I make mistakes, but without success.
PostsController:
public function add()
{
$post = $this->Posts->newEntity();
if ($this->request->is('post')) {
$post = $this->Posts->patchEntity($post, $this->request->data);
if ($this->Posts->save($post)) {
$this->Flash->success(__('The post has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The post could not be saved. Please, try again.'));
}
}
$this->set(compact('post'));
$this->set('_serialize', ['post']);
}
PostsTable:
$this->addBehavior('Upload');
$this->hasMany('Photos', [
'foreignKey' => 'post_id'
]);
UploadBehavior
All standard callbacks where I currently perform debug $data / $entity, but only in beforeMarshal i use:
$data = Hash::get($data,'name');
debug($data);
// debug output
[
'name' => 'HPIM3869.JPG',
'type' => 'image/jpeg',
'tmp_name' => 'C:\xampp\tmp\phpF02D.tmp',
'error' => (int) 0,
'size' => (int) 1295448
],
...
In beforeSave and afterSave
My form is OK, the data properly come in before Marshal method, if I upload 3 files, I also see the same number of debug outputs, but in beforSave and afterSave debug only show the first file like this:
debug($entity);
object(App\Model\Entity\Photos) {
'name' => [
'name' => 'HPIM3435.JPG',
'type' => 'image/jpeg',
'tmp_name' => 'C:\xampp\tmp\php5839.tmp',
'error' => (int) 0,
'size' => (int) 1517410
],
'post_id' => (int) 469,
'created' => object(Cake\I18n\Time) {
'time' => '2015-10-07T09:22:44+0200',
'timezone' => 'Europe/Berlin',
'fixedNowTime' => false
},
'modified' => object(Cake\I18n\Time) {
'time' => '2015-10-07T09:22:44+0200',
'timezone' => 'Europe/Berlin',
'fixedNowTime' => false
},
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'name' => true,
'post_id' => true,
'created' => true,
'modified' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[repository]' => 'Photos'
}
Edit:
In the purpose of the test, I create such a form:
echo $this->Form->input('name',['value'=>'zzz']);
echo $this->Form->input('photos.0.name',['value'=>'zzz']);
echo $this->Form->input('photos.1.name',['value'=>'hhh']);
echo $this->Form->input('photos.2.name',['value'=>'fff']);
Also it only be saved the first result.
I need help to understand how to save multiple form data. Where I go wrong?
I think your field should be like this
echo $this->Form->file('photos.name.',['multiple'=>true]); //note dot notation at end of name. It will generate input name as array

cakephp 3.x saving multiple entities - newEntities

I'm having the hardest time with saving multiple records. I've tried a million things, but I end up with the same problem: my records are not saved and I can't see any errors. Bear in mind that I'm new to cakephp and a novice coder.
Am I missing something obvious and crucial?
Table:
$this->table('splits');
$this->displayField('id');
$this->primaryKey('id');
$this->belongsTo('Transactions', [
'foreignKey' => 'transaction_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Accounts', [
'foreignKey' => 'account_credit_id',
'joinType' => 'INNER'
]);
Controller:
$splits = $this->Splits->newEntity();
if ($this->request->is('post')) {
$splits = $this->Splits->newEntities($this->request->data());
debug($splits);
foreach ($splits as $split){
$this->Splits->save($split);
}
}
$transactions = $this->Splits->Transactions->find('list', ['limit' => 200]);
$accounts = $this->Splits->Accounts->find('list', ['limit' => 200]);
$this->set(compact('split', 'transactions', 'accounts'));
$this->set('_serialize', ['split']);
Template:
echo $this->Form->input('Splits.1.transaction_id', ['options' => $transactions]);
echo $this->Form->input('Splits.1.amount', ['type' => 'float']);
echo $this->Form->input('Splits.1.account_id', ['options' => $accounts]);
echo $this->Form->input('Splits.2.transaction_id', ['options' => $transactions]);
echo $this->Form->input('Splits.2.amount', ['type' => 'float']);
echo $this->Form->input('Splits.2.account_id', ['options' => $accounts]);
echo $this->Form->input('Splits.3.transaction_id', ['options' => $transactions]);
echo $this->Form->input('Splits.3.amount', ['type' => 'float']);
echo $this->Form->input('Splits.3.account_id', ['options' => $accounts]);
Debug on $splits:
[
(int) 0 => object(App\Model\Entity\Split) {
(int) 1 => [
'transaction_id' => '108',
'amount' => '100.33',
'account_id' => '2'
],
(int) 2 => [
'transaction_id' => '108',
'amount' => '50.22',
'account_id' => '4'
],
(int) 3 => [
'transaction_id' => '108',
'amount' => '65.22',
'account_id' => '5'
],
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
(int) 1 => true,
(int) 2 => true,
(int) 3 => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[repository]' => 'Splits'
}
]
Did you somewhere saw this Table.index.field style being used, or did you just tried something and hoped it would work?
When saving many records respectively creating many entities, the expected format is a numerically indexed array that holds the data for the individual records, just as shown in the docs
Cookbook > Database Access & ORM > Saving Data > Converting Multiple Records
When creating forms that create/update multiple records at once you
can use newEntities():
[...]
In this situation, the request data for multiple articles should look
like:
$data = [
[
'title' => 'First post',
'published' => 1
],
[
'title' => 'Second post',
'published' => 1
],
];
So your inputs should not use the table name, but just the index and the field name, like
echo $this->Form->input('0.transaction_id', /* ... */);
echo $this->Form->input('0.amount', /* ... */);
echo $this->Form->input('0.account_id', /* ... */);
echo $this->Form->input('1.transaction_id', /* ... */);
echo $this->Form->input('1.amount', /* ... */);
echo $this->Form->input('1.account_id', /* ... */);
echo $this->Form->input('2.transaction_id', /* ... */);
echo $this->Form->input('2.amount', /* ... */);
echo $this->Form->input('3.account_id', /* ... */);
Try this:
$splits = TableRegistry::get('splits');
$entities = $splits->newEntities($this->request->data());
foreach ($entities as $entity) {
$splits->save($entity);
}
Try this example to insert multiple recored in cakphp 3.x
$passwords = $this->request->data('password_id');
foreach ($passwords as $password) {
$data = [
'event_id' => $this->request->data('event_id'),
'password_id' => $password
];
$eventPasswordAll = $this->EventPasswordAll->newEntity();
$this->EventPasswordAll->patchEntity($eventPasswordAll, $data);
$this->EventPasswordAll->save($eventPasswordAll );}
I hope it is useful to your question
please reviwe it, Thanks !
If you’d like to process all the entities as a single transaction you can use transactional():
Although using a loop, "transations" avoids errors if the "save" does not work.
// In a controller.
$articles->getConnection()->transactional(function () use ($articles, $entities) {
foreach ($entities as $entity) {
$articles->save($entity, ['atomic' => false]);
}
});
Converting Multiple Records - CakePHP 3

Resources