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

Phalcon4 hasManyToMany throw error: Not Supported

It seems Phalcon (4.1.0) does not support multiple filelds in many to many relationship. Foreign keys have been created in MySQL database. This is my code in initialize() function.

$this->hasManyToMany(
    ['id1', 'id2'],
    RelationshipTable::class,
    ['id3', 'id4'],
    ['id5', 'id6'],
    Parts::class,
    ['id1', 'id2'],
    [
        'reusable' => true,
        'alias'    => 'parts'
    ]
 );

It throws error: "Not supported", [internal function]: Phalcon\Mvc\Model\Manager->getRelationRecords()



8.4k
edited Dec '20

i don't know why it says in documentation that it could be an array and by going through the source code its clearly not

hasManyToMany(
    string|array $fields, 
    string $intermediateModel, 
    string|array $intermediateFields, 
    string|array $intermediateReferencedFields,
    string $referenceModel, 
    string|array $referencedFields, 
    array $options = null
)

if the relation is through an intermediate model ( which is true here ) it will check fileds and intermediateReferencedFields if either of them is an array it will throw the exception

Phalcon\Mvc\Model\Manager::getRelationRecords()

if relation->isThrough() {
    let conditions = [];

    let intermediateModel = relation->getIntermediateModel(),
        intermediateFields = relation->getIntermediateFields();

    /**
     * Appends conditions created from the fields defined in the
     * relation
     */
    let fields = relation->getFields();

    if unlikely typeof fields == "array" {
        throw new Exception("Not supported");
    }

    let conditions[] = "[" . intermediateModel . "].[" . intermediateFields . "] = :APR0:",
        placeholders["APR0"] = record->readAttribute(fields);

    let joinConditions = [];

    /**
     * Create the join conditions
     */
    let intermediateFields = relation->getIntermediateReferencedFields();

    if unlikely typeof intermediateFields == "array" {
        throw new Exception("Not supported");
    }

    // the rest is omitted
}
edited Dec '20

Routing errors when upgrading Phalcon 4 (underscore in action names) ยท Routing, 0, 8, 4h ago. Insert Image to Database With Field Type Blob Not Working in Phalcon ... Phalcon\Image\Adapter\Gd throwing an exception for the text function ... Phalcon4 hasManyToMany throw error: Not Supported posted by Robert Qian alaskasworld