# |
Nov 7th 2019, 15:49 |
dev.cyrusjayson |
okay I will look closely |
# |
Nov 7th 2019, 15:49 |
javier.villanueva |
I advice you that if you dont need a model for gavefruits, dont use a model. Define the relations in UsersModel and FruitsModel |
# |
Nov 7th 2019, 15:47 |
javier.villanueva |
but you need define better in your model |
# |
Nov 7th 2019, 15:47 |
javier.villanueva |
you dont need this complex find |
# |
Nov 7th 2019, 15:46 |
javier.villanueva |
https://book.cakephp.org/3/en/orm/associations.html#belongstomany-associations |
# |
Nov 7th 2019, 15:46 |
javier.villanueva |
you should read the doc |
# |
Nov 7th 2019, 15:44 |
slackebot1 |
}]); ``` |
# |
Nov 7th 2019, 15:44 |
dev.cyrusjayson |
not working. what is wrong here ``` Model $this->belongsToMany('GaveFruits', [ 'foreignKey' => 'fruit_id' ]); ---------- in Controller $fruits = $this->Fruits->find('all') ->contain(['GaveFruits'=>function(Query $q){ return $q ->where(['GaveFruits.user_id' => 1]); |
# |
Nov 7th 2019, 15:40 |
javier.villanueva |
not need in this case |
# |
Nov 7th 2019, 15:39 |
javier.villanueva |
you can specify you type of join in definition |
# |
Nov 7th 2019, 15:37 |
javier.villanueva |
yes |
# |
Nov 7th 2019, 15:36 |
dev.cyrusjayson |
in raw query it will be like this ``` select * from fruits left join gave_fruits on fruits.id = gave_fruits.fruits_id and gave_fruits.user_id = 1 where <condition here>``` |
# |
Nov 7th 2019, 15:33 |
dev.cyrusjayson |
okay I will have a look |
# |
Nov 7th 2019, 15:33 |
javier.villanueva |
but if only needs the relationship you dont need it |
# |
Nov 7th 2019, 15:32 |
javier.villanueva |
https://book.cakephp.org/3/en/orm/associations.html#using-the-through-option |
# |
Nov 7th 2019, 15:32 |
javier.villanueva |
If you have another fields for gave_fruits (for instance gave_date) then you can use throught optionn |
# |
Nov 7th 2019, 15:31 |
dev.cyrusjayson |
ok I will try this belongsToMany |
# |
Nov 7th 2019, 15:31 |
dev.cyrusjayson |
because I know who is he already so I just want to pull all fruits and giving fruits to him |
# |
Nov 7th 2019, 15:31 |
javier.villanueva |
fruits belongsToMany users |
# |
Nov 7th 2019, 15:31 |
javier.villanueva |
could be a model, but really is a relationship |
# |
Nov 7th 2019, 15:31 |
dev.cyrusjayson |
but I don’t mind about details of users |
# |
Nov 7th 2019, 15:31 |
javier.villanueva |
gave_fruits is not a model |
# |
Nov 7th 2019, 15:30 |
dev.cyrusjayson |
no. user_id is foreign key to Gave_Fruits |
# |
Nov 7th 2019, 15:30 |
javier.villanueva |
three tables, fruits, users, fruits_users |
# |
Nov 7th 2019, 15:30 |
javier.villanueva |
its true? |
# |
Nov 7th 2019, 15:30 |
javier.villanueva |
only two models, not three |
# |
Nov 7th 2019, 15:30 |
javier.villanueva |
there are FRUITS AND USERS |
# |
Nov 7th 2019, 15:30 |
javier.villanueva |
and there are not model b |
# |
Nov 7th 2019, 15:29 |
javier.villanueva |
you need belongsToMany assoc |
# |
Nov 7th 2019, 15:29 |
javier.villanueva |
ok then |
# |
Nov 7th 2019, 15:29 |
dev.cyrusjayson |
hhhmmm… im not good at explaining. let me give an example. there are 3 fruits apple, orange, banana I gave apple and orange to user 1 and I want to display all fruits and what fruits I gave to user 1 Fruits Gave Fruits apple apple orange orange banana |
# |
Nov 7th 2019, 15:29 |
javier.villanueva |
IF YES |
# |
Nov 7th 2019, 15:27 |
javier.villanueva |
no, my question is if model b is a MODEL of only a relationship between model A and users |
# |
Nov 7th 2019, 15:27 |
dev.cyrusjayson |
model b could have 2 data from model a but model a always greater |
# |
Nov 7th 2019, 15:26 |
javier.villanueva |
it has more fields than model_a_id, user_id |
# |
Nov 7th 2019, 15:25 |
javier.villanueva |
and do you need model b? |
# |
Nov 7th 2019, 15:24 |
dev.cyrusjayson |
example ```Model A id 1 id 2 id 3 id 4 Model B model_a_id (1), user 1 model_a_id (2), user 1``` The output should be ``` [model_a_1][model_b_model_a_1], [model_a_2][model_b_model_a_2], [model_a_3][], [model_a_4][] ``` something like that. I hope it make sense. I tried ;) |
# |
Nov 7th 2019, 15:21 |
dev.cyrusjayson |
yes and once per user |
# |
Nov 7th 2019, 15:12 |
jotpe |
Does Model B can contain the ID of Model a just once or several times? |
# |
Nov 7th 2019, 15:02 |
dev.cyrusjayson |
I need to discuss this and maybe you guys can share your inputs ```I want to get the data from Model A and Model B Model B will be then compare to Model A id. basically the Model B contain id of Model A Model A Model B id 1 id 1 (matched) id 2 id 2 (matched) id 3 id 4 ``` what will be the association os this? how ti will look like in find/get query |
# |
Nov 7th 2019, 14:26 |
alexdd55976 |
You are welcome |