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 / Update and delete . Bug / Help / Wrong config

Having the following structure:

<?php

namespace App\Core\Models\Post;

class Post extends \App\Core\Models\CoreModel
{
    public function initialize()
    {
        $this->hasManyToMany(
            "id",
            "App\Core\Models\Post\PostSignaturePost",
            "post_id",
            "signature_id",
            "App\Core\Models\Signature\Signature",
            "id",
            array(
                'alias' => 'signatures'
            )
        );
    }
}
<?php

namespace App\Core\Models\Post;

class PostSignaturePost extends \App\Core\Models\CoreModel
{
    public function initialize()
    {
        $this->belongsTo('signature_id', 'App\Core\Models\Signature\Signature', 'id',
            array('alias' => 'signature')
        );

        $this->belongsTo('post_id', 'App\Core\Models\Post\Post', 'id',
            array('alias' => 'post')
        );
    }
}
<?php

namespace App\Core\Models\Signature;

class Signature extends \App\Core\Models\CoreModel
{
    public function initialize()
    {
        $this->hasManyToMany(
            "id",
            "App\Core\Models\Post\PostSignaturePost",
            "signature_id",
            "post_id",
            "App\Core\Models\Post\Post",
            "id",
            array('alias' => 'posts')
        );
    }
}

Questions:

  1. If i call $post->signatures->delete() will delete signatures from signature table, not from the intermediate table. If this is a bug, this is HUGE and needs fix ASAP. If it's not a bug, then ?

  2. The proper way to update the signatures for a post ? What would be ? Example:

A) a post has N signatures. I want remove one. How ? Normally, if i pass the id's in orm should be enought. But it's not.

$post          = PostObject();
$signatures = SignaturesObject::find(array("id in (".implode(',', $post_data['signatures']).")");

// If i say
$post->signatures = $signatures;
$post->update();// it is not working. The ORM especially on update should remove all the non-related signatures and save the new ones.

//If i say
foreach ($post->signatures as $sig) { // maybe ORM should handle this ?
    $to_save[] = $sig;
}
$post->signatures = $to_save;
$post->update(); // It works, but does not remove the old ones.

// If is say
$post->signatures->delete(); // BUG ?! Removes the signatures from the signature table, not from the intermediate table
$post->signatures = $to_save[];
$post->save();

And again a bug that i opened months ago https://github.com/phalcon/cphalcon/issues/2039 and none really cares, but in the meanwhile bugs like image processing were fixed ...

please help someone..

In case someone is is looking for the bug:

The implementation of the delete() function is always referring to the table of the current object. The deletion of intermediate entries should happen in the _checkForeignKeysReverseCascade() function, which obviously is not behaving as expected. This function is called in delete() when virtualForeignKeys is set using the setup() function.

Could anyone test the behavior when $this->setup(array("virtualForeignKeys")); is executed during the model initialization?