# |
Feb 20th 2020, 12:36 |
tomrwaller |
Thinking back over my tables/models earlier. I have three tables - [recommendations], [technologies] and [recommendations_technologies]. I want a recommendation to have one 'primary technology' and multiple 'associated technologies'. I thought I could do two associations to technologies on the RecommendationsTable using className but I'm struggling to save the associated technologies. Am I doing this this right? |
# |
Feb 20th 2020, 12:34 |
spriz |
@dereuromark No no, I was just surprised that it works in 3.x - I have never seen the syntax before! |
# |
Feb 20th 2020, 12:32 |
viraj.khatavkar |
going further, do we have some form of sync method for belongsToMany where in I can pass an array of ids and those are the ones which will be kept in the join table? |
# |
Feb 20th 2020, 11:22 |
dereuromark |
I cant do everything on my own. |
# |
Feb 20th 2020, 11:22 |
dereuromark |
all fine in 3.x code here. you are free to PR updates though :) |
# |
Feb 20th 2020, 11:21 |
spriz |
`$actsAs` |
# |
Feb 20th 2020, 11:21 |
spriz |
And this one? :O https://github.com/dereuromark/cakephp-sandbox/blob/master/plugins/Sandbox/src/Model/Table/SandboxPostsTable.php#L33-L36 |
# |
Feb 20th 2020, 11:21 |
spriz |
what sorcery is this one? :) :) https://github.com/dereuromark/cakephp-sandbox/blob/master/plugins/Sandbox/src/Model/Table/SandboxPostsTable.php#L41-L56 cake 2.x syntax? |
# |
Feb 20th 2020, 11:19 |
dereuromark |
hmm, maybe the Tags behavior does this.. : ) ok, then you are probably right. |
# |
Feb 20th 2020, 11:19 |
spriz |
Wat! I have had to add the `$this->belongsToMany('Roles')` and `$this->hasMany('UsersRoles')` |
# |
Feb 20th 2020, 11:18 |
dereuromark |
you only need the manual one if you want to provide custom table and more logic. |
# |
Feb 20th 2020, 11:18 |
dereuromark |
@spriz Shouldnt the HABTM relation auto provide the pivot table? afaik it does |
# |
Feb 20th 2020, 11:18 |
spriz |
@viraj.khatavkar you will need to have `$this->hasMany('UsersRoles')` in `UsersTable` :) |
# |
Feb 20th 2020, 11:18 |
dereuromark |
Yeah, in my case with tags the pivot table is setup as "tagged" - thus the idehelper added https://github.com/dereuromark/cakephp-sandbox/blob/master/plugins/Sandbox/src/Model/Table/SandboxPostsTable.php#L10-L11 , so $this->Tagged->updateAll() etc then. |
# |
Feb 20th 2020, 11:13 |
viraj.khatavkar |
thanks, will see how that goes.. |
# |
Feb 20th 2020, 11:10 |
dereuromark |
usually $roles->roles_users etc, depending on your relation setup. |
# |
Feb 20th 2020, 11:09 |
dereuromark |
usually you have access, and If you used IdeHelper to generate the annotations for IDE auto complete and discoverability, you find them even by just typing the first letter :) |
# |
Feb 20th 2020, 11:09 |
dereuromark |
You need to go to the pivot table |
# |
Feb 20th 2020, 11:07 |
viraj.khatavkar |
I am having a hard time thinking the syntax here. so would following work? - `$roles->users->updateAll()`? I don’t think so because it will update the users table right? |
# |
Feb 20th 2020, 11:06 |
dereuromark |
atomic queries always work on any table. |
# |
Feb 20th 2020, 11:05 |
dereuromark |
sure. |
# |
Feb 20th 2020, 11:05 |
viraj.khatavkar |
Is it possible to run `updateAll` on the join table in a belongsToMany association? The join table doesn’t have it’s own model. Eg. I have a `roles` , `users` and `roles_users` tables. I want to bulk update on the `roles_users` table. |
# |
Feb 20th 2020, 11:04 |
tomrwaller |
No worries - thank you for having a look anyway, much appreciated! |
# |
Feb 20th 2020, 11:03 |
challgren |
Hmm sorry thats my only solution, don’t really know the forms that well |
# |
Feb 20th 2020, 11:03 |
tomrwaller |
Thanks @challgren - I swapped out the input for the below but I'm still seeing the same behaviour. `echo $this->Form->input('associated_technologies_.ids', array(` `'type' => 'select',` `'multiple'=>'checkbox',` `'options' => $associatedTechnologies` `));` |
# |
Feb 20th 2020, 10:58 |
challgren |
I think you need a ‘multiple’ => ‘checkbox’ there for associated_technologies |
# |
Feb 20th 2020, 10:44 |
slackebot |
actually selected. |
# |
Feb 20th 2020, 10:44 |
tomrwaller |
If I debug the data on the add the request looks like this: ```[ 'recommendation_group_id' => '1', 'name' => 'e1', 'recommendation' => 'e1', 'rationale' => 'e1', 'primary_technology_id' => '1', 'associated_technologies' => [ '_ids' => '4' ], 'assessments' => [ '_ids' => '2' ]``` I was expecting to see an array in _ids => 4 as I selected 4 entries in the select but it looks like it is giving a count, not a list of ids I |
# |
Feb 20th 2020, 10:42 |
tomrwaller |
This is what I have in my add.php. |
# |
Feb 20th 2020, 10:41 |
tomrwaller |
echo $this->Form->control('primary_technology_id', ['options' => $primaryTechnologies]);echo $this->Form->control('associated_technologies._ids', ['options' => $associatedTechnologies]); |
# |
Feb 20th 2020, 10:41 |
tomrwaller |
That's my RecommendationsTechnologiesTable. |
# |
Feb 20th 2020, 10:40 |
tomrwaller |
`$this->belongsTo('Recommendations', [` `'foreignKey' => 'recommendation_id',` `'joinType' => 'INNER',` `]);` `$this->belongsTo('Technologies', [` `'foreignKey' => 'technology_id',` `'joinType' => 'INNER',` `]);` |
# |
Feb 20th 2020, 10:40 |
tomrwaller |
That's my RecommendationsController. |
# |
Feb 20th 2020, 10:40 |
slackebot |
`'targetForeignKey' => 'technology_id',` `'joinTable' => 'recommendations_technologies',` `]);` |
# |
Feb 20th 2020, 10:40 |
tomrwaller |
`$this->belongsTo('PrimaryTechnologies', [` `'className' => 'Technologies',` `'foreignKey' => 'primary_technology_id',` `'joinType' => 'INNER',` `]);` `$this->belongsToMany('AssociatedTechnologies', [` `'className' => 'Technologies',` `'foreignKey' => 'recommendation_id',` |
# |
Feb 20th 2020, 10:39 |
slackebot |
Recommendations I have defined two associations to Technologies. Trouble is, the multiselect on add.php doesn't set the Associated Technologies. The Primary Technology does however get set. |
# |
Feb 20th 2020, 10:39 |
tomrwaller |
Hey all - would someone mind casting their eyes over my models? Not sure I'm doing this right. I have Technologies and I have Recommendations. They are joined using the recommendations_technologies table. In the recommendation, I have two fields, one is Primary Technology which is a choice of one from the technologies table, and another field Associated Technologies which can be multiple entries from the technology field. So essentially in |
# |
Feb 20th 2020, 09:31 |
alexdd55976 |
yeah.. i figured it out.. the docs have been better then expected :) |
# |
Feb 20th 2020, 09:23 |
challgren |
@alexdd55976 do you mean when you run `bin/cake bake ….` |
# |
Feb 20th 2020, 08:32 |
alexdd55976 |
ah got it |
# |
Feb 20th 2020, 08:30 |
alexdd55976 |
is there a way to change bake templates so they won't be overwritten during an update |