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 relation issues with aliasing

I have 3 models, Product, ProductAttribute, and Attribute. When I try to do:

<?php
    public function getLockedAttributes(Model $product)
    {
        return $product->getAttributes([
            'locked = :lock:',
            'bind' => ['lock' => Attribute::LOCKED]
        ]);
    }

I get the error that locked does not belong to any models. But it belongs to the Attribute model.

Column 'locked' doesn't belong to any of the selected models (2), when preparing: SELECT [\Greatmedia\Models\V1\Attribute].* FROM [\Greatmedia\Models\V1\Attribute] INNER JOIN [\Greatmedia\Models\V1\ProductAttribute] ON [\Greatmedia\Models\V1\ProductAttribute].[attribute_id] = [\Greatmedia\Models\V1\Attribute].[id] WHERE [\Greatmedia\Models\V1\ProductAttribute].[product_id] = ?0 AND locked = :lock:

Attribute model

<?php
namespace Greatmedia\Models\V1;

class Attribute extends \Phalcon\Mvc\Model
{
    const LOCKED = 1;

    public $id;

    public $value;

    public $label;

    public $attribute_code;

    public $locked;

    public function getSource()
    {
        return 'attribute';
    }

    public function initialize()
    {
        $this->hasMany('id', '\Greatmedia\Models\V1\ProductAttribute', 'attribute_id', [
          'alias' => 'product_attribute'
        ]);
    }
}

Product model

<?php
namespace Greatmedia\Models\V1;

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

    public $name;

    public $price;

    public function getSource()
    {
        return 'product';
    }

    public function initialize()
    {
//         $this->hasMany('id', '\Greatmedia\Models\V1\ProductAttribute', 'product_id', [
//             'alias' => 'attributes'
//         ]);

        $this->hasManyToMany(
            'id',
            '\Greatmedia\Models\V1\ProductAttribute',
            'product_id',
            'attribute_id',
            '\Greatmedia\Models\V1\Attribute',
            'id',
            ['alias' => 'attributes']
        );
    }
}


98.9k

The meaning of that error is:

  • There's no column in the mapped table called 'locked'
  • The meta-data adapter is not the memory one and it's not reloading recently added columns to the mapped table