cphalcon: Bug in Phalcon\Mvc\Model `__set()` method for array data params

As this forum post. I think there is a bug in __set() method and the relation alias should be checked in array assignment before fall in relation logics (or just use __isset() method):

/**
 * Magic method to assign values to the the model
 *
 * @param string property
 * @param mixed value
 */
public function __set(string property, value)
{
    var lowerProperty, related, modelName, manager, lowerKey, relation, referencedModel,
        key, item;

    /**
     * Values are probably relationships if they are objects
     */
    if typeof value == "object" {
        if value instanceof ModelInterface {
            let lowerProperty = strtolower(property),
                this->{lowerProperty} = value,
                this->_related[lowerProperty] = value,
                this->_dirtyState = self::DIRTY_STATE_TRANSIENT;
            return value;
        }
    }

    /**
     * Check if the value is an array
     */
    if typeof value == "array" {

        let lowerProperty = strtolower(property),
            modelName = get_class(this),
            manager = this->getModelsManager();

        /* check relation here */
        let relation = <RelationInterface> manager->getRelationByAlias(modelName, lowerProperty); // <-- LOGIC HERE
        if typeof relation == "object" {

            let related = [];
            for key, item in value {
                if typeof item == "object" {
                    if item instanceof ModelInterface {
                        let related[] = item;
                    }
                } else {
                    let lowerKey = strtolower(key),
                        this->{lowerKey} = item,
                        relation = <RelationInterface> manager->getRelationByAlias(modelName, lowerProperty);
                        if typeof relation == "object" {
                            let referencedModel = manager->load(relation->getReferencedModel());
                            referencedModel->writeAttribute(lowerKey, item);
                        }
                }
            }

            if count(related) > 0 {
                let this->_related[lowerProperty] = related,
                    this->_dirtyState = self::DIRTY_STATE_TRANSIENT;
            }

            return value;
        }
    }

    /**
     * Fallback assigning the value to the instance
     */
    let this->{property} = value;

    return value;
}

Any idea? or workaround for this bug?

Thanks

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 22 (11 by maintainers)

Most upvoted comments