phalcon/cphalcon · error · Phalcon\Mvc\Model\Exceptions\ReferencedFieldsMismatch

Number of referenced fields are not the same in the…

Error message

Number of referenced fields are not the same in the BelongsTo relation of model '{entityName}' with Reference Model '{referencedEntity}'

What it means

Thrown by ModelsManager::addBelongsTo when both fields and referencedFields are arrays but their counts differ — a compound foreign key must pair each local field with exactly one referenced field, and the pairing is ambiguous otherwise.

Solutions

  1. Make the fields and referencedFields arrays the same length, pairing local and referenced columns positionally
  2. Pass scalars instead of arrays for single-field relations
  3. Double-check the column lists in the addBelongsTo() call inside the model's initialize()
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at phalcon/Mvc/Model/Manager.zep:319 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21). Data as JSON: /api/errors/5e689213b1a61813. Report an issue: GitHub.

Appendix: source

Thrown at phalcon/Mvc/Model/Manager.zep:319

        var entityName, referencedEntity, relation, relations, alias,
            lowerAlias, singleRelations;
        string keyRelation;

        let entityName = get_class_lower(model),
            referencedEntity = strtolower(referencedModel);

        let keyRelation = entityName . "$" . referencedEntity;

        if !fetch relations, this->belongsTo[keyRelation] {
            let relations = [];
        }

        /**
         * Check if the number of fields are the same
         */
        if unlikely typeof referencedFields == "array" {
            if unlikely count(fields) != count(referencedFields) {
                throw new ReferencedFieldsMismatch("BelongsTo", entityName, referencedEntity);
            }
        }

        /**
         * Create a relationship instance
         */
        let relation = new Relation(
            Relation::BELONGS_TO,
            referencedModel,
            fields,
            referencedFields,
            options
        );

        /**
         * Check an alias for the relation
         */
        if fetch alias, options["alias"] {

View on GitHub (pinned to b7419de9cd)