# |
Jul 12th 2017, 21:50 |
aaronc |
So I'm still struggling with something that is certainly a basic feature. How do I just query my table manually? I have my controller ``` use Cake\ORM\Query; class ListsController extends AppController { public function index() { $result = $this->Tests->query(); pr($result); } } ``` But it throws an error "Call to a member function query() on boolean. So my Tests variable is a boolean? Does it need to referenced somehow so that it |
# |
Jul 12th 2017, 21:49 |
chris-andre |
@konstanting Have a look https://stackoverflow.com/questions/37658120/linking-two-tables-with-two-composite-foreign-key-relation |
# |
Jul 12th 2017, 21:47 |
konstanting |
Alright then thank you all for your help and input :thumbsup: |
# |
Jul 12th 2017, 21:45 |
konstanting |
yes I think thats the only solution i belongstomany is used |
# |
Jul 12th 2017, 21:44 |
chris-andre |
Right, only I can think of is to have two associations with alias UserA and UserB, then `->contain(['UserA', 'UserB'])` |
# |
Jul 12th 2017, 21:43 |
konstanting |
It would be nice to just contain(['Friends']) but this only works for UserA -> UserB and not if I select userB to contain UserA |
# |
Jul 12th 2017, 21:40 |
konstanting |
(sorry for answering this late) |
# |
Jul 12th 2017, 21:39 |
konstanting |
@chris-andre Thats essentially what the users_users table is. This works in one direction but not in both. |
# |
Jul 12th 2017, 21:25 |
ericadeefox |
^^ty for this, I am so bad at convention |
# |
Jul 12th 2017, 21:23 |
chris-andre |
Just to give you an input on join tables, according the docs: >Join tables, used in BelongsToMany relationships between models, should be named after the model tables they will join, arranged in alphabetical order (articles_tags rather than tags_articles). |
# |
Jul 12th 2017, 21:22 |
ericadeefox |
^^oh no that's much better, to have a users_groups and a users_friendships |
# |
Jul 12th 2017, 21:20 |
chris-andre |
That would be "easier". That would be a groups (or something similar) with groups_users. |
# |
Jul 12th 2017, 21:19 |
ericadeefox |
@chris-andre ahh good point if there are just 2 users per friendship, I was thinking a "friendship" would have >= 2 users, as many as necessary lol |
# |
Jul 12th 2017, 21:18 |
chris-andre |
@konstanting I would go for one table named whatever (friendships) with id:integer user_a:integer user_b:integer. Two rows for one friendship should not be necessary. Now, I'm haven't thought about the a nice cake-way to create the association. |
# |
Jul 12th 2017, 21:16 |
ericadeefox |
in the Friendships model, you'd put that Friendships entities have many Users entities. in this instance, a Friendship is just, like, an entity that multiple Users can collaborate on. no different than if an article had multiple authors for example. |
# |
Jul 12th 2017, 21:14 |
ericadeefox |
if UserA and UserB have Friendship1, then that's how they relate. in `users_friends`, there'd be a row for UserA and Friendship1, and a separate row for UserB and Friendship1 |
# |
Jul 12th 2017, 21:13 |
konstanting |
```$this->belongsToMany('Friends', [ 'className' => 'Users', 'joinTable' => 'users_users', 'targetForeignKey' => 'friend_id' ]);``` |
# |
Jul 12th 2017, 21:13 |
konstanting |
Maybe the following makes it clearer |
# |
Jul 12th 2017, 21:13 |
konstanting |
@ericadeefox But what would I put in the Friendships Model? If I want relate UserA to UserB I need to connect these Models and not another one. |
# |
Jul 12th 2017, 21:12 |
ericadeefox |
gbye @sturm!! :) |
# |
Jul 12th 2017, 21:12 |
saeideng |
good by |
# |
Jul 12th 2017, 21:12 |
saeideng |
goof by |
# |
Jul 12th 2017, 21:11 |
sturm |
End of workday; I’m heading home. See you all tomorrow, most likely. |
# |
Jul 12th 2017, 21:10 |
saeideng |
if your table is `users_friends` you should use `users_friends`] |
# |
Jul 12th 2017, 21:09 |
ericadeefox |
you want many users to be able to have many friendships, and many friendships to be able to have many users, like social media yes? those 'Friendships' are entities also and need their own models. |
# |
Jul 12th 2017, 21:08 |
ericadeefox |
probably, I'm horrible at following convention. @konstanting you're gonna want a model for your 'Friendships' as well. |
# |
Jul 12th 2017, 21:08 |
sturm |
Also, @ericadeefox, doesn’t the join table have to be in alphabetical order? i.e., `friends_users` instead of `users_friends`? Not sure if it makes that much of a difference, though. |
# |
Jul 12th 2017, 21:08 |
konstanting |
@ericadeefox No it's just imaginary |
# |
Jul 12th 2017, 21:07 |
ericadeefox |
@konstanting do you have a model built for 'Friendships'? |
# |
Jul 12th 2017, 21:06 |
sturm |
Okay, thanks @saeideng. |
# |
Jul 12th 2017, 21:06 |
ericadeefox |
yes, 'localproject' in this case would be the same as your 'friendship's. |
# |
Jul 12th 2017, 21:06 |
konstanting |
I think the Problem with my relation ist that It relates to the User Modal in both directions |
# |
Jul 12th 2017, 21:06 |
konstanting |
@ericadeefox Is "localproject" a Model too? |
# |
Jul 12th 2017, 21:05 |
ericadeefox |
``` $this->belongsToMany('Users', [ 'foreignKey' => 'localproject_id', 'targetForeignKey' => 'user_id', 'joinTable' => 'users_localprojects' ]);``` |
# |
Jul 12th 2017, 21:04 |
ericadeefox |
this is a similar snippet of code from a many-to-many relation in something that was left untouched after I just baked it: |
# |
Jul 12th 2017, 21:03 |
konstanting |
@ericadeefox If I delete this line it won't work at all |
# |
Jul 12th 2017, 21:03 |
saeideng |
within `request` tab |
# |
Jul 12th 2017, 21:03 |
nicolas.r |
it creates autommaticly model and relations |
# |
Jul 12th 2017, 21:03 |
nicolas.r |
no probleme like dat |
# |
Jul 12th 2017, 21:03 |
saeideng |
also you can see these params on debug_kit panel |
# |
Jul 12th 2017, 21:02 |
nicolas.r |
@konstanting personnaly i create my schema db with SQL and I do a cake bake model myModel |