# |
Mar 13th 2018, 17:03 |
neon1024 |
Ah, bad example I’ve not normalised that :P |
# |
Mar 13th 2018, 17:02 |
neon1024 |
Let’s try a Unit which Practises a Specialisation |
# |
Mar 13th 2018, 17:02 |
neon1024 |
Perhaps a code example will help |
# |
Mar 13th 2018, 17:01 |
willem |
well, i think because my association in the ApplicantTable is named ‘Degree’ instead of ‘ApplicantsDegrees’ ? |
# |
Mar 13th 2018, 17:01 |
neon1024 |
At least, that’s how I see it |
# |
Mar 13th 2018, 17:01 |
neon1024 |
The problem is what your Table class associations |
# |
Mar 13th 2018, 17:00 |
neon1024 |
If it did, why are you getting an exception for a missing association? |
# |
Mar 13th 2018, 17:00 |
willem |
i understand, but when i read this and check my db and classes it looks exactly like that |
# |
Mar 13th 2018, 17:00 |
neon1024 |
As it’s pretty key to database design and software design |
# |
Mar 13th 2018, 16:59 |
neon1024 |
This is important to learn |
# |
Mar 13th 2018, 16:59 |
neon1024 |
I feel it’s better to teach you why than tell you what code changes to make |
# |
Mar 13th 2018, 16:59 |
willem |
euh kinda, but i have all those table classes already? only ``` $this->belongsToMany('Degrees', [ 'foreignKey' => 'applicant_id', 'targetForeignKey' => 'degree_id', 'joinTable' => 'applicants_degrees' ]); ``` you say this should be hasMany ? |
# |
Mar 13th 2018, 16:58 |
neon1024 |
Does that make sense? :slightly_smiling_face: |
# |
Mar 13th 2018, 16:58 |
neon1024 |
As it is associated to the two tables to normalize that many to many database relationship |
# |
Mar 13th 2018, 16:57 |
neon1024 |
Which is why you can see the `CoursesMembershipsTable` from the book, has two `belongsTo` associations in it |
# |
Mar 13th 2018, 16:57 |
neon1024 |
This is what the book page is saying |
# |
Mar 13th 2018, 16:57 |
neon1024 |
So when you inverse that association, you also get `ApplicantsDegrees belongsTo Applicants` in your ApplicantsDegreesTable |
# |
Mar 13th 2018, 16:56 |
neon1024 |
Then change the association from `belongsToMany` to `Applicants hasMany ApplicantsDegrees` |
# |
Mar 13th 2018, 16:56 |
neon1024 |
So you want to actually create a join table class, ApplicantsDegreesTable |
# |
Mar 13th 2018, 16:56 |
neon1024 |
That will allow Cake to automatically understand the join, but you need more control! |
# |
Mar 13th 2018, 16:56 |
neon1024 |
However |
# |
Mar 13th 2018, 16:55 |
neon1024 |
Which is why you’ve declared a ‘joinTable’ in your association configuration in your Table class |
# |
Mar 13th 2018, 16:55 |
neon1024 |
So you have to put a join table in the middle |
# |
Mar 13th 2018, 16:55 |
neon1024 |
Instead of a belongsToMany |
# |
Mar 13th 2018, 16:55 |
neon1024 |
Then you have two belongsTo and hasMany |
# |
Mar 13th 2018, 16:55 |
neon1024 |
`[ ] >- [ ] -< [ ]` |
# |
Mar 13th 2018, 16:54 |
neon1024 |
So you put a table in the middle |
# |
Mar 13th 2018, 16:54 |
neon1024 |
You need to convert `[ ] >-< [ ]` style ERD into a One to Many, on either side to normalise the Many To Many |
# |
Mar 13th 2018, 16:54 |
neon1024 |
You know in a database, what normalisation is right? |
# |
Mar 13th 2018, 16:53 |
willem |
what association should i make and where? |
# |
Mar 13th 2018, 16:53 |
neon1024 |
You know that in a database you cannot have a Many to Many relationship right? |
# |
Mar 13th 2018, 16:53 |
willem |
I read that piece from the docs 100 time, just do not understand it, there is a ApplicantsDe |
# |
Mar 13th 2018, 16:52 |
neon1024 |
https://book.cakephp.org/3.0/en/orm/associations.html#using-the-through-option |
# |
Mar 13th 2018, 16:52 |
neon1024 |
Just copying and pasting random code from Slack into your project won’t magically make stuff work |
# |
Mar 13th 2018, 16:52 |
neon1024 |
You must create the associations |
# |
Mar 13th 2018, 16:52 |
neon1024 |
Well of course |
# |
Mar 13th 2018, 16:51 |
slackebot |
with ApplicantsDegrees_ |
# |
Mar 13th 2018, 16:51 |
willem |
if i try your code ``` $test = $this->ApplicantsTable ->find() ->where( [ 'Applicants.id' => '00000000-0000-0000-0000-100000000001', ] ) ->contain( [ 'ApplicantsDegrees' => ['Degrees'], ]) ->first(); ``` i get an error _InvalidArgumentException: Applicants is not associated |
# |
Mar 13th 2018, 16:51 |
willem |
not sure what you mean by this ```Your `applicants_degrees` join table has more than foreign keys in, so should really have a table instance``` |
# |
Mar 13th 2018, 16:47 |
neon1024 |
Making your join more verbose `$this->Applicants->find()->contain(['ApplicantsDegrees' => ['Degrees']])` |
# |
Mar 13th 2018, 16:47 |
neon1024 |
Your `applicants_degrees` join table has more than foreign keys in, so should really have a table instance |