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

Many to Many relationship Issue

Hello. I am enjoying Vokuro and have been trying some things using it as a base on my own, just to get used to the framework. I am enjoying it quite a lot. I have added a couple more tables and thus models on my own. I added a Communities model and am joining that to the Users model by means of a join table, CommunityUsers. I have created a model for this as well and have set up the relationships as recommended by the docs and other posts in the forums. However, while I can pull Users communities, I cannot pull results from Communities to get Community Users. So the relationship seems to only work one way -> from Users.

Here's the Communities model code:

    <?php
    namespace Vokuro\Models;
    use Phalcon\Mvc\Model;

    class Communities extends Model
    {
    ...
    public function initalize(){
        $this->hasManyToMany(
            "id",
            "Vokuro\Models\CommunityUsers",
            "community_id", "user_id", 
            "Vokuro\Models\Users",
            "id",
            array('alias' => 'users')
        );
        $this->belongsTo("client_id", "Clients", "id");
    }
    }

And here's my linked CommunityUsers Model:

    <?php
    namespace Vokuro\Models;
    use Phalcon\Mvc\Model;

    class CommunityUsers extends Model
    {
    public function initialize()
    {
        $this->setSource('community_users');

        $this->belongsTo('community_id', 'Vokuro\Models\Communities', 'id', array(
            'alias' => 'community'
        ));

        $this->belongsTo('user_id', 'Vokuro\Models\Users', 'id', array(
            'alias' => 'users'
        ));
    }
    }

And finally, the Users (abbreviated model only association shown):

    <?php
      $this->hasManyToMany(
            "id",
            "Vokuro\Models\CommunityUsers",
            "user_id", "community_id", 
            "Vokuro\Models\Communities",
            "id",
            array('alias' => 'communities')
        );

And the client code from the controller throwing the error (note the users call works fine, not the communities):

    <?php
    public function testAction(){
        $users = Users::find();
        foreach($users as $user){
            $communities = $user->communities;
            foreach($communities as $community){
                echo $community->c_name;
            }

        }

        $communities = Communities::find();
        foreach($communities as $community){
            $users = $community->users;
            foreach($users as $user){
                echo $user->name;
            }

        }
        die();
    }


43.9k

Hi,

code looks good. What kind of error did you receive ?

Thanks for answering. Yes, I've been over the code over and over. I get a notice about the line below in the test function for the $communities model call.

    <?php
    ...
    $users = $community->users;

Notice: Access to undefined property Vokuro\Models\Communities::users

The notice error is as above, so the relationship simply doesn't exist. In the $users output, the communities show up.

Thanks, chas688



43.9k
edited Nov '15

try with

$user = $community->communityUsers;

or

$users = $community->getCommunityUsers();

https://docs.phalcon.io/en/latest/reference/models.html#taking-advantage-of-relationships

Tried them, but still get the same errors. What confounds me is that why should it work for users and not for communities? I have the calls and the loops set up the same way, so I'm not sure why the second part of the code in the test function fails to find its association, whereas getting communities for users works OK? I don't understand. Thanks, Charles



43.9k

yep, that's really strange indeed ! I'm afraid, I can't help you more

Maybe this is related to https://github.com/phalcon/cphalcon/issues/10886 issues.

Can you try to change first letter of the alias to uppercase?

$this->belongsTo('user_id', 'Vokuro\Models\Users', 'id', array(
            'alias' => 'Users'
        ));

In trying to isolate this issue, I tried to access the joins every single way I could think of. That included also a query directly to the join table Model and trying to get the associations both ways, to Users and to Communities with the same type of loop. Both worked well and as expected. @jimmy, I tried all combinations and even renaming the aliases, but due to the good results I have had in going to the other models directly, I don't think it's that, but thanks for the assistance.

At this point this seems either like a bug or something (more likely) stupid that I'm missing. I may just create a new model from scratch and try to join it to the users in the same way - to connect it through the join table and see if it works. Any other ideas are warmly suggested and will certainly be tried.

Best, Chas

Just an update to this issue, I've tried almost everything I could and am still at a loss. Any way to debug this? I can step through code, but at a certain point I can't see into the Phalcon compiled source. Thanks!



43.9k

hi,

did you also try to swap "user_id", "community_id", in Communities model hasManyToMany declaration ?