We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Unable to get hasmanytomany relationship to work

I'm having trouble getting a manytomany relationship to work for some reason, specifically getting the 'end' result e.g when there is a table that joins to models. My set-up is as follows:

model User id, name

model Note id

model UserHasNote user_id, note_id,

Now if I call $user->gerUserHasNote() it returns the valid resultset if I call $user->getNote() it returns nothing, no error or anything (error_reporting is on) but it does seem to stop execution as I can't see any other output after this point.

My initialize in User is like so

<?php
 $this->hasManyToMany("id", "UserHasNote", "user_id", note_id", "Note", id");
 ?>

In my UserHasNote I have

<?php
         $this->belongsTo("note_id", "Note", "id");
        $this->belongsTo("id", "User", "id");
 ?>

And finally note

<?php
        $this->hasMany("id", "UserHasNote", "note_id");
 ?>

I've been going through forum posts and the docs but the hasmanytomany docs have all the relevant info to understand the relationships too spread out and the reference isn't that great about what parameter srelate to what either.

finally I call this as so

$hasnotes = $user->getUserHasNotes();
            var_dump($hasnotes); //works
            var_dump($user->getNotes()); // halts execution
            var_dump('foo'); // won't show

calling the ->getNotes() method on an instance of UserHasNotes doesn't seem to work either.

Not sure what I'm doing wrong here as, as far as I can tell from the docs this is the correct usage. All my data exists as well (at least in the relationships I think I have set-up here)

thanks



12.2k
Accepted
answer

Managed to get this working, there was actually an error in one of my models in a property name that was causing this not to return anything. Interstingly enough I had error reporting set to errors only, and this actually appeared as a warning. So the best advice I can give is to set error_reporting(E_ALL) when debugging!