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 save

Hello Phalconers!!!! I'm trying save relationed models N:N, but i'm already searched everywhere, and nothing works..


class PainelPerfil extends BaseModel {
    public function initialize()
    {
        $this->hasManyToMany('id', PainelPerfilAction::class, 'painel_perfil_usuario_id', 'painel_action_id', PainelAction::class, 'id', ['alias' => 'PainelAction']);
    }
}

class PainelPerfilAction extends BaseModel {
    public function initialize()
    {
        $this->belongsTo('painel_perfil_id', PainelPerfil::class, 'id', ['alias' => 'PainelPerfil']);
        $this->belongsTo('painel_action_id', PainelAction::class, 'id', ['alias' => 'PainelAction']);
    }
}

class PainelAction extends BaseModel {
    public function initialize()
    {
        $this->hasManyToMany('id', PainelPerfilAction::class, 'painel_action_id', 'painel_perfil_usuario_id', PainelPerfil::class, 'id', ['alias' => 'PainelPerfil']);
    }
}

if i use


$perfil->painelAction();

works fine to get related objects, but i can't insert or update this relationship

variable $actions has an collection of PainelAction.


$perfil->painelAction = $actions;

$perfil->save();

but not save relations :(

Anyone has any idea to save it? Thx!

Can you try lowercased alias ? like painelAction ? Also you sure you don't have by any change property in your class painelAction ?