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

'reusable' flag in Many-to-Many relation does not work!?

Hello all, i noticed that 'reusable' flag works fine for one-to-one and many-to-one relations, but does not for many-to-many ((. It looks like another sad point in many-to-many land, along with this https://forum.phalcon.io/discussion/2190/many-to-many-expected-behaviour#C8434

Let's imagine we have an obvious many-to-many relation between Posts and Tags:

// Posts
 $this->hasManyToMany(
                "id",
                "PostsTags",
                "postref", "tagref",
                "Tags",
                "id", ['reusable' => true]
            );

// PostsTags
    public function initialize()
    {
        $this->belongsTo("tagref", "Tags", "id", ['reusable' => true]);
        $this->belongsTo("postref", "Posts", "id", ['reusable' => true]);
    }

// Tags
 $this->hasManyToMany(
            "id",
            "PostsTags",
             "tagref", "postref",
            "Posts",
            "id", ['reusable' => true]
        );

and this queries the database again and again:

$post->Tags; // 'reusable' does not work at all 

Any ideas?



2.0k

Its wrong:

// Tags
 $this->hasManyToMany(
            "id",
            "PostsTags",
             "tagref", "postref",
            "Posts",
            "id", ['reusable' => true]
        );

maybe this way:

// Tags
 $this->hasManyToMany(
            "id",
            "Tags",
             "tagref", "postref",
            "PostsTags",
            "id", ['reusable' => true]
        );


2.3k

it does not work the way you suggested. But my version works like a charm, but just 'reusable' functionality does not. Look at this sample from the official docs

class Robots extends \Phalcon\Mvc\Model
{
    public $id;

    public $name;

    public function initialize()
    {
        $this->hasManyToMany(
            "id",
            "RobotsParts",
            "robots_id", "parts_id",
            "Parts",
            "id"
        );
    }

}