Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Many to Many whit extra fields

I don't pass extra fields to many to many relations.

May schema is :

create table attributes
(
    id int auto_increment
        primary key,
    name varchar(255) not null,
    status enum('active', 'deleted', 'unactive') default 'active' not null,
    creared_at datetime not null,
    updated_at datetime null
)
;

create table products
(
    id int auto_increment
        primary key,
    title varchar(255) null,
    summary varchar(255) null,
    content text not null,
    meta_title varchar(255) null,
    meta_key varchar(255) null,
    meta_descrp varchar(255) null,
    status enum('active', 'deleted', 'unactive', '') default '' not null,
    created_at datetime not null,
    updated_at datetime null,
    deleted_at datetime null
)
;

create index status_idx
    on products (status)
;

create table products_attributes
(
    product_id int not null,
    attribute_id int not null,
    attributes_values text not null,
    constraint products_attributes_ibfk_1
        foreign key (product_id) references mobile.products (id)
            on delete cascade,
    constraint products_attributes_ibfk_2
        foreign key (attribute_id) references mobile.attributes (id)
            on update cascade on delete cascade
)
;

create index attribute_id
    on products_attributes (attribute_id)
;

create index product_id
    on products_attributes (product_id)
;

create table users
(
    id int auto_increment
        primary key,
    username int not null,
    user_role set('admin') default 'admin' not null,
    password varchar(255) not null,
    status enum('acitive', 'unactive', 'deleted', '') default 'acitive' not null,
    created_at datetime not null,
    updated_at datetime null,
    deleted_at datetime null,
    constraint username
        unique (username)
);

My relations :

$this->hasManyToMany(
            "id",
            "\ProductsAttributes",
            "product_id",
            "attribute_id",
            "\Attributes",
            'id',
            ['alias'=>'attributes','params'=>['columns'=>' Products.*, Attributes.*, ProductsAttributes.*']]
        );

But generated error :

Phalcon \ Mvc \ Model \ Exception
Unknown model or alias 'Products' (11), when preparing: SELECT Products.id, Attributes.value FROM [\Attributes] INNER JOIN [\ProductsAttributes] ON [\ProductsAttributes].[attribute_id] = [\Attributes].[id] WHERE [\ProductsAttributes].[product_id] = :APR0


124
Accepted
answer

I find solutions edit my relations defenition

public function initialize()
    {
        $this->hasManyToMany(
            "id",
            "\ProductsAttributes",
            "product_id",
            "attribute_id",
            "\Attributes",
            'id',
            [
                'alias'=>'attributes',
                'params'=>
                    [
                        //Add array and define models name in square brackets
                        'columns'=>[
                            '[\Attributes].*', 
                             '[\ProductsAttributes].attributes_values AS vals '
                            ]
                    ]
            ]
        );
    }