From 1fec6545dcc383e193afec085b6835ce761323fe Mon Sep 17 00:00:00 2001 From: Sid Roberts Date: Sun, 28 Feb 2016 21:16:55 +0000 Subject: [PATCH] ModelsManager::getHasOneAndHasMany() always returns an array. --- phalcon/mvc/model.zep | 259 +++++++++++++++++++++--------------------- 1 file changed, 127 insertions(+), 132 deletions(-) diff --git a/phalcon/mvc/model.zep b/phalcon/mvc/model.zep index 0fe7fa7152b..4cddc6a0f02 100644 --- a/phalcon/mvc/model.zep +++ b/phalcon/mvc/model.zep @@ -1636,83 +1636,80 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface */ let relations = manager->getHasOneAndHasMany(this); - if count(relations) { + for relation in relations { - for relation in relations { + /** + * Check if the relation has a virtual foreign key + */ + let foreignKey = relation->getForeignKey(); + if foreignKey !== false { /** - * Check if the relation has a virtual foreign key + * By default action is restrict */ - let foreignKey = relation->getForeignKey(); - if foreignKey !== false { - - /** - * By default action is restrict - */ - let action = Relation::NO_ACTION; + let action = Relation::NO_ACTION; - /** - * Try to find a different action in the foreign key's options - */ - if typeof foreignKey == "array" { - if isset foreignKey["action"] { - let action = (int) foreignKey["action"]; - } + /** + * Try to find a different action in the foreign key's options + */ + if typeof foreignKey == "array" { + if isset foreignKey["action"] { + let action = (int) foreignKey["action"]; } + } + + /** + * Check only if the operation is restrict + */ + if action == Relation::ACTION_CASCADE { /** - * Check only if the operation is restrict + * Load a plain instance from the models manager */ - if action == Relation::ACTION_CASCADE { - - /** - * Load a plain instance from the models manager - */ - let referencedModel = manager->load(relation->getReferencedModel()); + let referencedModel = manager->load(relation->getReferencedModel()); - let fields = relation->getFields(), - referencedFields = relation->getReferencedFields(); + let fields = relation->getFields(), + referencedFields = relation->getReferencedFields(); - /** - * Create the checking conditions. A relation can has many fields or a single one - */ - let conditions = [], bindParams = []; + /** + * Create the checking conditions. A relation can has many fields or a single one + */ + let conditions = [], bindParams = []; - if typeof fields == "array" { - for position, field in fields { - fetch value, this->{field}; - let conditions[] = "[". referencedFields[position] . "] = ?" . position, - bindParams[] = value; - } - } else { - fetch value, this->{fields}; - let conditions[] = "[" . referencedFields . "] = ?0", + if typeof fields == "array" { + for position, field in fields { + fetch value, this->{field}; + let conditions[] = "[". referencedFields[position] . "] = ?" . position, bindParams[] = value; } + } else { + fetch value, this->{fields}; + let conditions[] = "[" . referencedFields . "] = ?0", + bindParams[] = value; + } - /** - * Check if the virtual foreign key has extra conditions - */ - if fetch extraConditions, foreignKey["conditions"] { - let conditions[] = extraConditions; - } + /** + * Check if the virtual foreign key has extra conditions + */ + if fetch extraConditions, foreignKey["conditions"] { + let conditions[] = extraConditions; + } - /** - * We don't trust the actual values in the object and then we're passing the values using bound parameters - * Let's make the checking - */ - let resultset = referencedModel->find([ - join(" AND ", conditions), - "bind": bindParams - ]); + /** + * We don't trust the actual values in the object and then we're passing the values using bound parameters + * Let's make the checking + */ + let resultset = referencedModel->find([ + join(" AND ", conditions), + "bind": bindParams + ]); - /** - * Delete the resultset - * Stop the operation if needed - */ - if resultset->delete() === false { - return false; - } + /** + * Delete the resultset + * Stop the operation if needed + */ + if resultset->delete() === false { + return false; } } } @@ -1743,106 +1740,104 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface * We check if some of the hasOne/hasMany relations is a foreign key */ let relations = manager->getHasOneAndHasMany(this); - if count(relations) { - let error = false; - for relation in relations { + let error = false; + for relation in relations { + + /** + * Check if the relation has a virtual foreign key + */ + let foreignKey = relation->getForeignKey(); + if foreignKey !== false { /** - * Check if the relation has a virtual foreign key + * By default action is restrict */ - let foreignKey = relation->getForeignKey(); - if foreignKey !== false { + let action = Relation::ACTION_RESTRICT; - /** - * By default action is restrict - */ - let action = Relation::ACTION_RESTRICT; - - /** - * Try to find a different action in the foreign key's options - */ - if typeof foreignKey == "array" { - if isset foreignKey["action"] { - let action = (int) foreignKey["action"]; - } + /** + * Try to find a different action in the foreign key's options + */ + if typeof foreignKey == "array" { + if isset foreignKey["action"] { + let action = (int) foreignKey["action"]; } + } + + /** + * Check only if the operation is restrict + */ + if action == Relation::ACTION_RESTRICT { + + let relationClass = relation->getReferencedModel(); /** - * Check only if the operation is restrict + * Load a plain instance from the models manager */ - if action == Relation::ACTION_RESTRICT { + let referencedModel = manager->load(relationClass); - let relationClass = relation->getReferencedModel(); + let fields = relation->getFields(), + referencedFields = relation->getReferencedFields(); - /** - * Load a plain instance from the models manager - */ - let referencedModel = manager->load(relationClass); + /** + * Create the checking conditions. A relation can has many fields or a single one + */ + let conditions = [], bindParams = []; - let fields = relation->getFields(), - referencedFields = relation->getReferencedFields(); + if typeof fields == "array" { - /** - * Create the checking conditions. A relation can has many fields or a single one - */ - let conditions = [], bindParams = []; + for position, field in fields { + fetch value, this->{field}; + let conditions[] = "[" . referencedFields[position] . "] = ?" . position, + bindParams[] = value; + } - if typeof fields == "array" { + } else { + fetch value, this->{fields}; + let conditions[] = "[" . referencedFields . "] = ?0", + bindParams[] = value; + } - for position, field in fields { - fetch value, this->{field}; - let conditions[] = "[" . referencedFields[position] . "] = ?" . position, - bindParams[] = value; - } + /** + * Check if the virtual foreign key has extra conditions + */ + if fetch extraConditions, foreignKey["conditions"] { + let conditions[] = extraConditions; + } - } else { - fetch value, this->{fields}; - let conditions[] = "[" . referencedFields . "] = ?0", - bindParams[] = value; - } + /** + * We don't trust the actual values in the object and then we're passing the values using bound parameters + * Let's make the checking + */ + if referencedModel->count([join(" AND ", conditions), "bind": bindParams]) { /** - * Check if the virtual foreign key has extra conditions + * Create a new message */ - if fetch extraConditions, foreignKey["conditions"] { - let conditions[] = extraConditions; + if !fetch message, foreignKey["message"] { + let message = "Record is referenced by model " . relationClass; } /** - * We don't trust the actual values in the object and then we're passing the values using bound parameters - * Let's make the checking + * Create a message */ - if referencedModel->count([join(" AND ", conditions), "bind": bindParams]) { - - /** - * Create a new message - */ - if !fetch message, foreignKey["message"] { - let message = "Record is referenced by model " . relationClass; - } - - /** - * Create a message - */ - this->appendMessage(new Message(message, fields, "ConstraintViolation")); - let error = true; - break; - } + this->appendMessage(new Message(message, fields, "ConstraintViolation")); + let error = true; + break; } } } + } - /** - * Call validation fails event - */ - if error === true { - if globals_get("orm.events") { - this->fireEvent("onValidationFails"); - this->_cancelOperation(); - } - return false; + /** + * Call validation fails event + */ + if error === true { + if globals_get("orm.events") { + this->fireEvent("onValidationFails"); + this->_cancelOperation(); } + return false; } return true;