diff --git a/phalcon/db/adapter/pdo/mysql.zep b/phalcon/db/adapter/pdo/mysql.zep index c9e6fa743e3..38400540381 100644 --- a/phalcon/db/adapter/pdo/mysql.zep +++ b/phalcon/db/adapter/pdo/mysql.zep @@ -128,161 +128,108 @@ class Mysql extends PdoAdapter implements AdapterInterface */ let columnType = field[1]; - loop { - + if memstr(columnType, "enum") { /** * Enum are treated as char */ - if memstr(columnType, "enum") { - let definition["type"] = Column::TYPE_CHAR; - break; - } - + let definition["type"] = Column::TYPE_CHAR; + } elseif memstr(columnType, "bigint") { /** * Smallint/Bigint/Integers/Int are int */ - if memstr(columnType, "bigint") { - let definition["type"] = Column::TYPE_BIGINTEGER, - definition["isNumeric"] = true, - definition["bindType"] = Column::BIND_PARAM_INT; - break; - } - + let definition["type"] = Column::TYPE_BIGINTEGER, + definition["isNumeric"] = true, + definition["bindType"] = Column::BIND_PARAM_INT; + } elseif memstr(columnType, "int") { /** * Smallint/Bigint/Integers/Int are int */ - if memstr(columnType, "int") { - let definition["type"] = Column::TYPE_INTEGER, - definition["isNumeric"] = true, - definition["bindType"] = Column::BIND_PARAM_INT; - break; - } - + let definition["type"] = Column::TYPE_INTEGER, + definition["isNumeric"] = true, + definition["bindType"] = Column::BIND_PARAM_INT; + } elseif memstr(columnType, "varchar") { /** * Varchar are varchars */ - if memstr(columnType, "varchar") { - let definition["type"] = Column::TYPE_VARCHAR; - break; - } - + let definition["type"] = Column::TYPE_VARCHAR; + } elseif memstr(columnType, "datetime") { /** * Special type for datetime */ - if memstr(columnType, "datetime") { - let definition["type"] = Column::TYPE_DATETIME; - break; - } - + let definition["type"] = Column::TYPE_DATETIME; + } elseif memstr(columnType, "char") { /** * Chars are chars */ - if memstr(columnType, "char") { - let definition["type"] = Column::TYPE_CHAR; - break; - } - + let definition["type"] = Column::TYPE_CHAR; + } elseif memstr(columnType, "date") { /** * Date are dates */ - if memstr(columnType, "date") { - let definition["type"] = Column::TYPE_DATE; - break; - } - + let definition["type"] = Column::TYPE_DATE; + } elseif memstr(columnType, "timestamp") { /** * Timestamp are dates */ - if memstr(columnType, "timestamp") { - let definition["type"] = Column::TYPE_TIMESTAMP; - break; - } - + let definition["type"] = Column::TYPE_TIMESTAMP; + } elseif memstr(columnType, "text") { /** * Text are varchars */ - if memstr(columnType, "text") { - let definition["type"] = Column::TYPE_TEXT; - break; - } - + let definition["type"] = Column::TYPE_TEXT; + } elseif memstr(columnType, "decimal") { /** * Decimals are floats */ - if memstr(columnType, "decimal"){ - let definition["type"] = Column::TYPE_DECIMAL, - definition["isNumeric"] = true, - definition["bindType"] = Column::BIND_PARAM_DECIMAL; - break; - } - + let definition["type"] = Column::TYPE_DECIMAL, + definition["isNumeric"] = true, + definition["bindType"] = Column::BIND_PARAM_DECIMAL; + } elseif memstr(columnType, "double") { /** * Doubles */ - if memstr(columnType, "double"){ - let definition["type"] = Column::TYPE_DOUBLE, - definition["isNumeric"] = true, - definition["bindType"] = Column::BIND_PARAM_DECIMAL; - break; - } - + let definition["type"] = Column::TYPE_DOUBLE, + definition["isNumeric"] = true, + definition["bindType"] = Column::BIND_PARAM_DECIMAL; + } elseif memstr(columnType, "float") { /** * Float/Smallfloats/Decimals are float */ - if memstr(columnType, "float") { - let definition["type"] = Column::TYPE_FLOAT, - definition["isNumeric"] = true, - definition["bindType"] = Column::BIND_PARAM_DECIMAL; - break; - } - + let definition["type"] = Column::TYPE_FLOAT, + definition["isNumeric"] = true, + definition["bindType"] = Column::BIND_PARAM_DECIMAL; + } elseif memstr(columnType, "bit") { /** * Boolean */ - if memstr(columnType, "bit") { - let definition["type"] = Column::TYPE_BOOLEAN, - definition["bindType"] = Column::BIND_PARAM_BOOL; - break; - } - + let definition["type"] = Column::TYPE_BOOLEAN, + definition["bindType"] = Column::BIND_PARAM_BOOL; + } elseif memstr(columnType, "tinyblob") { /** * Tinyblob */ - if memstr(columnType, "tinyblob") { - let definition["type"] = Column::TYPE_TINYBLOB, - definition["bindType"] = Column::BIND_PARAM_BOOL; - break; - } - + let definition["type"] = Column::TYPE_TINYBLOB, + definition["bindType"] = Column::BIND_PARAM_BOOL; + } elseif memstr(columnType, "mediumblob") { /** * Mediumblob */ - if memstr(columnType, "mediumblob") { - let definition["type"] = Column::TYPE_MEDIUMBLOB; - break; - } - + let definition["type"] = Column::TYPE_MEDIUMBLOB; + } elseif memstr(columnType, "longblob") { /** * Longblob */ - if memstr(columnType, "longblob") { - let definition["type"] = Column::TYPE_LONGBLOB; - break; - } - + let definition["type"] = Column::TYPE_LONGBLOB; + } elseif memstr(columnType, "blob") { /** * Blob */ - if memstr(columnType, "blob") { - let definition["type"] = Column::TYPE_BLOB; - break; - } - + let definition["type"] = Column::TYPE_BLOB; + } else { /** * By default is string */ let definition["type"] = Column::TYPE_VARCHAR; - break; } /** diff --git a/phalcon/db/adapter/pdo/postgresql.zep b/phalcon/db/adapter/pdo/postgresql.zep index ba5458a2b6e..3b90bafa8b4 100644 --- a/phalcon/db/adapter/pdo/postgresql.zep +++ b/phalcon/db/adapter/pdo/postgresql.zep @@ -121,154 +121,107 @@ class Postgresql extends PdoAdapter implements AdapterInterface numericSize = field[3], numericScale = field[4]; - loop { - + if memstr(columnType, "smallint(1)") { /** * Smallint(1) is boolean */ - if memstr(columnType, "smallint(1)") { - let definition["type"] = Column::TYPE_BOOLEAN, - definition["bindType"] = Column::BIND_PARAM_BOOL; - break; - } - + let definition["type"] = Column::TYPE_BOOLEAN, + definition["bindType"] = Column::BIND_PARAM_BOOL; + } elseif memstr(columnType, "bigint") { /** * Bigint */ - if memstr(columnType, "bigint") { - let definition["type"] = Column::TYPE_BIGINTEGER, - definition["isNumeric"] = true, - definition["bindType"] = Column::BIND_PARAM_INT; - break; - } - + let definition["type"] = Column::TYPE_BIGINTEGER, + definition["isNumeric"] = true, + definition["bindType"] = Column::BIND_PARAM_INT; + } elseif memstr(columnType, "int") { /** * Int */ - if memstr(columnType, "int") { - let definition["type"] = Column::TYPE_INTEGER, - definition["isNumeric"] = true, - definition["size"] = numericSize, - definition["bindType"] = Column::BIND_PARAM_INT; - break; - } - + let definition["type"] = Column::TYPE_INTEGER, + definition["isNumeric"] = true, + definition["size"] = numericSize, + definition["bindType"] = Column::BIND_PARAM_INT; + } elseif memstr(columnType, "varying") { /** * Varchar */ - if memstr(columnType, "varying") { - let definition["type"] = Column::TYPE_VARCHAR, - definition["size"] = charSize; - break; - } - + let definition["type"] = Column::TYPE_VARCHAR, + definition["size"] = charSize; + } elseif memstr(columnType, "date") { /** * Special type for datetime */ - if memstr(columnType, "date") { - let definition["type"] = Column::TYPE_DATE, - definition["size"] = 0; - break; - } - + let definition["type"] = Column::TYPE_DATE, + definition["size"] = 0; + } elseif memstr(columnType, "timestamp") { /** * Timestamp */ - if memstr(columnType, "timestamp") { - let definition["type"] = Column::TYPE_TIMESTAMP; - break; - } - + let definition["type"] = Column::TYPE_TIMESTAMP; + } elseif memstr(columnType, "numeric") { /** * Numeric */ - if memstr(columnType, "numeric") { - let definition["type"] = Column::TYPE_DECIMAL, - definition["isNumeric"] = true, - definition["size"] = numericSize, - definition["scale"] = numericScale, - definition["bindType"] = Column::BIND_PARAM_DECIMAL; - break; - } - + let definition["type"] = Column::TYPE_DECIMAL, + definition["isNumeric"] = true, + definition["size"] = numericSize, + definition["scale"] = numericScale, + definition["bindType"] = Column::BIND_PARAM_DECIMAL; + } elseif memstr(columnType, "char") { /** * Chars are chars */ - if memstr(columnType, "char") { - let definition["type"] = Column::TYPE_CHAR, - definition["size"] = charSize; - break; - } - + let definition["type"] = Column::TYPE_CHAR, + definition["size"] = charSize; + } elseif memstr(columnType, "timestamp") { /** * Date */ - if memstr(columnType, "timestamp") { - let definition["type"] = Column::TYPE_DATETIME, - definition["size"] = 0; - break; - } - + let definition["type"] = Column::TYPE_DATETIME, + definition["size"] = 0; + } elseif memstr(columnType, "text") { /** * Text are varchars */ - if memstr(columnType, "text") { - let definition["type"] = Column::TYPE_TEXT, - definition["size"] = charSize; - break; - } - + let definition["type"] = Column::TYPE_TEXT, + definition["size"] = charSize; + } elseif memstr(columnType, "float") { /** * Float/Smallfloats/Decimals are float */ - if memstr(columnType, "float") { - let definition["type"] = Column::TYPE_FLOAT, - definition["isNumeric"] = true, - definition["size"] = numericSize, - definition["bindType"] = Column::BIND_PARAM_DECIMAL; - break; - } - + let definition["type"] = Column::TYPE_FLOAT, + definition["isNumeric"] = true, + definition["size"] = numericSize, + definition["bindType"] = Column::BIND_PARAM_DECIMAL; + } elseif memstr(columnType, "bool") { /** * Boolean */ - if memstr(columnType, "bool") { - let definition["type"] = Column::TYPE_BOOLEAN, - definition["size"] = 0, - definition["bindType"] = Column::BIND_PARAM_BOOL; - break; - } - + let definition["type"] = Column::TYPE_BOOLEAN, + definition["size"] = 0, + definition["bindType"] = Column::BIND_PARAM_BOOL; + } elseif memstr(columnType, "jsonb") { /** * Jsonb */ - if memstr(columnType, "jsonb") { - let definition["type"] = Column::TYPE_JSONB; - break; - } - + let definition["type"] = Column::TYPE_JSONB; + } elseif memstr(columnType, "json") { /** * Json */ - if memstr(columnType, "json") { - let definition["type"] = Column::TYPE_JSON; - break; - } - + let definition["type"] = Column::TYPE_JSON; + } elseif memstr(columnType, "uuid") { /** * UUID */ - if memstr(columnType, "uuid") { - let definition["type"] = Column::TYPE_CHAR, - definition["size"] = 36; - break; - } - + let definition["type"] = Column::TYPE_CHAR, + definition["size"] = 36; + } else { /** * By default is string */ let definition["type"] = Column::TYPE_VARCHAR; - break; } /** diff --git a/phalcon/db/adapter/pdo/sqlite.zep b/phalcon/db/adapter/pdo/sqlite.zep index 3444542f3c0..50b97d1fa60 100644 --- a/phalcon/db/adapter/pdo/sqlite.zep +++ b/phalcon/db/adapter/pdo/sqlite.zep @@ -108,124 +108,85 @@ class Sqlite extends PdoAdapter implements AdapterInterface */ let columnType = field[2]; - loop { - + if memstr(columnType, "tinyint(1)") { /** * Tinyint(1) is boolean */ - if memstr(columnType, "tinyint(1)") { - let definition["type"] = Column::TYPE_BOOLEAN, - definition["bindType"] = Column::BIND_PARAM_BOOL, - columnType = "boolean"; // Change column type to skip size check - break; - } - + let definition["type"] = Column::TYPE_BOOLEAN, + definition["bindType"] = Column::BIND_PARAM_BOOL, + columnType = "boolean"; // Change column type to skip size check + } elseif memstr(columnType, "bigint") { /** * Bigint are int */ - if memstr(columnType, "bigint") { - let definition["type"] = Column::TYPE_BIGINTEGER, - definition["isNumeric"] = true, - definition["bindType"] = Column::BIND_PARAM_INT; - break; - } - + let definition["type"] = Column::TYPE_BIGINTEGER, + definition["isNumeric"] = true, + definition["bindType"] = Column::BIND_PARAM_INT; + } elseif memstr(columnType, "int") || memstr(columnType, "INT") { /** * Smallint/Integers/Int are int */ - if memstr(columnType, "int") || memstr(columnType, "INT") { - - let definition["type"] = Column::TYPE_INTEGER, - definition["isNumeric"] = true, - definition["bindType"] = Column::BIND_PARAM_INT; + let definition["type"] = Column::TYPE_INTEGER, + definition["isNumeric"] = true, + definition["bindType"] = Column::BIND_PARAM_INT; - if field[5] { - let definition["autoIncrement"] = true; - } - break; + if field[5] { + let definition["autoIncrement"] = true; } - + } elseif memstr(columnType, "varchar") { /** * Varchar are varchars */ - if memstr(columnType, "varchar") { - let definition["type"] = Column::TYPE_VARCHAR; - break; - } - + let definition["type"] = Column::TYPE_VARCHAR; + } elseif memstr(columnType, "date") { /** * Date/Datetime are varchars */ - if memstr(columnType, "date") { - let definition["type"] = Column::TYPE_DATE; - break; - } - + let definition["type"] = Column::TYPE_DATE; + } elseif memstr(columnType, "timestamp") { /** * Timestamp as date */ - if memstr(columnType, "timestamp") { - let definition["type"] = Column::TYPE_TIMESTAMP; - break; - } - + let definition["type"] = Column::TYPE_TIMESTAMP; + } elseif memstr(columnType, "decimal") { /** * Decimals are floats */ - if memstr(columnType, "decimal") { - let definition["type"] = Column::TYPE_DECIMAL, - definition["isNumeric"] = true, - definition["bindType"] = Column::BIND_PARAM_DECIMAL; - break; - } - + let definition["type"] = Column::TYPE_DECIMAL, + definition["isNumeric"] = true, + definition["bindType"] = Column::BIND_PARAM_DECIMAL; + } elseif memstr(columnType, "char") { /** * Chars are chars */ - if memstr(columnType, "char") { - let definition["type"] = Column::TYPE_CHAR; - break; - } - + let definition["type"] = Column::TYPE_CHAR; + } elseif memstr(columnType, "datetime") { /** * Special type for datetime */ - if memstr(columnType, "datetime") { - let definition["type"] = Column::TYPE_DATETIME; - break; - } - + let definition["type"] = Column::TYPE_DATETIME; + } elseif memstr(columnType, "text") { /** * Text are varchars */ - if memstr(columnType, "text") { - let definition["type"] = Column::TYPE_TEXT; - break; - } - + let definition["type"] = Column::TYPE_TEXT; + } elseif memstr(columnType, "float") { /** * Float/Smallfloats/Decimals are float */ - if memstr(columnType, "float") { - let definition["type"] = Column::TYPE_FLOAT, - definition["isNumeric"] = true, - definition["bindType"] = Column::TYPE_DECIMAL; - break; - } - + let definition["type"] = Column::TYPE_FLOAT, + definition["isNumeric"] = true, + definition["bindType"] = Column::TYPE_DECIMAL; + } elseif memstr(columnType, "enum") { /** * Enum are treated as char */ - if memstr(columnType, "enum") { - let definition["type"] = Column::TYPE_CHAR; - break; - } - + let definition["type"] = Column::TYPE_CHAR; + } else { /** * By default is string */ let definition["type"] = Column::TYPE_VARCHAR; - break; } /** diff --git a/phalcon/mvc/model.zep b/phalcon/mvc/model.zep index ccff9c1d898..f15bfddf6ae 100644 --- a/phalcon/mvc/model.zep +++ b/phalcon/mvc/model.zep @@ -1493,31 +1493,22 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface // Call the validation, if it returns not the boolean // we append the messages to the current object - if typeof messages != "boolean" { - - messages->rewind(); - - // for message in iterator(messages) { - while messages->valid() { - - let message = messages->current(); - - this->appendMessage( - new Message( - message->getMessage(), - message->getField(), - message->getType() - ) - ); - - messages->next(); - } + if typeof messages == "boolean" { + return messages; + } - // If there is a message, it returns false otherwise true - return !count(messages); + for message in iterator(messages) { + this->appendMessage( + new Message( + message->getMessage(), + message->getField(), + message->getType() + ) + ); } - return messages; + // If there is a message, it returns false otherwise true + return !count(messages); } /** @@ -1622,133 +1613,132 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface * We check if some of the belongsTo relations act as virtual foreign key */ let belongsTo = manager->getBelongsTo(this); - if count(belongsTo) { - - let error = false; - for relation in belongsTo { - let validateWithNulls = false; - let foreignKey = relation->getForeignKey(); - if foreignKey !== false { + let error = false; + for relation in belongsTo { - /** - * 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"]; - } - } - - /** - * Check only if the operation is restrict - */ - if action == Relation::ACTION_RESTRICT { - - /** - * Load the referenced model if needed - */ - let referencedModel = manager->load(relation->getReferencedModel()); + let validateWithNulls = false; + let foreignKey = relation->getForeignKey(); + if foreignKey === false { + continue; + } - /** - * Since relations can have multiple columns or a single one, we need to build a condition for each of these cases - */ - let conditions = [], bindParams = []; + /** + * By default action is restrict + */ + let action = Relation::ACTION_RESTRICT; - let numberNull = 0, - fields = relation->getFields(), - referencedFields = relation->getReferencedFields(); + /** + * 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"]; + } + } - if typeof fields == "array" { - /** - * Create a compound condition - */ - for position, field in fields { - fetch value, this->{field}; - let conditions[] = "[" . referencedFields[position] . "] = ?" . position, - bindParams[] = value; - if typeof value == "null" { - let numberNull++; - } - } + /** + * Check only if the operation is restrict + */ + if action != Relation::ACTION_RESTRICT { + continue; + } - let validateWithNulls = numberNull == count(fields); + /** + * Load the referenced model if needed + */ + let referencedModel = manager->load(relation->getReferencedModel()); - } else { + /** + * Since relations can have multiple columns or a single one, we need to build a condition for each of these cases + */ + let conditions = [], bindParams = []; - fetch value, this->{fields}; - let conditions[] = "[" . referencedFields . "] = ?0", - bindParams[] = value; + let numberNull = 0, + fields = relation->getFields(), + referencedFields = relation->getReferencedFields(); - if typeof value == "null" { - let validateWithNulls = true; - } - } + if typeof fields == "array" { + /** + * Create a compound condition + */ + for position, field in fields { + fetch value, this->{field}; + let conditions[] = "[" . referencedFields[position] . "] = ?" . position, + bindParams[] = value; + if typeof value == "null" { + let numberNull++; + } + } - /** - * Check if the virtual foreign key has extra conditions - */ - if fetch extraConditions, foreignKey["conditions"] { - let conditions[] = extraConditions; - } + let validateWithNulls = numberNull == count(fields); - /** - * Check if the relation definition allows nulls - */ - if validateWithNulls { - if fetch allowNulls, foreignKey["allowNulls"] { - let validateWithNulls = (boolean) allowNulls; - } else { - let validateWithNulls = false; - } - } + } else { - /** - * We don't trust the actual values in the object and pass the values using bound parameters - * Let's make the checking - */ - if !validateWithNulls && !referencedModel->count([join(" AND ", conditions), "bind": bindParams]) { + fetch value, this->{fields}; + let conditions[] = "[" . referencedFields . "] = ?0", + bindParams[] = value; - /** - * Get the user message or produce a new one - */ - if !fetch message, foreignKey["message"] { - if typeof fields == "array" { - let message = "Value of fields \"" . join(", ", fields) . "\" does not exist on referenced table"; - } else { - let message = "Value of field \"" . fields . "\" does not exist on referenced table"; - } - } + if typeof value == "null" { + let validateWithNulls = true; + } + } - /** - * Create a message - */ - this->appendMessage(new Message(message, fields, "ConstraintViolation")); - let error = true; - break; - } + /** + * Check if the virtual foreign key has extra conditions + */ + if fetch extraConditions, foreignKey["conditions"] { + let conditions[] = extraConditions; + } - } + /** + * Check if the relation definition allows nulls + */ + if validateWithNulls { + if fetch allowNulls, foreignKey["allowNulls"] { + let validateWithNulls = (boolean) allowNulls; + } else { + let validateWithNulls = false; } } /** - * Call 'onValidationFails' if the validation fails + * We don't trust the actual values in the object and pass the values using bound parameters + * Let's make the checking */ - if error === true { - if globals_get("orm.events") { - this->fireEvent("onValidationFails"); - this->_cancelOperation(); + if !validateWithNulls && !referencedModel->count([join(" AND ", conditions), "bind": bindParams]) { + + /** + * Get the user message or produce a new one + */ + if !fetch message, foreignKey["message"] { + if typeof fields == "array" { + let message = "Value of fields \"" . join(", ", fields) . "\" does not exist on referenced table"; + } else { + let message = "Value of field \"" . fields . "\" does not exist on referenced table"; + } } - return false; + + /** + * Create a message + */ + this->appendMessage(new Message(message, fields, "ConstraintViolation")); + let error = true; + break; } } + /** + * Call 'onValidationFails' if the validation fails + */ + if error === true { + if globals_get("orm.events") { + this->fireEvent("onValidationFails"); + this->_cancelOperation(); + } + return false; + } + return true; } @@ -1773,87 +1763,85 @@ 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 + */ + let foreignKey = relation->getForeignKey(); + if foreignKey === false { + continue; + } - /** - * By default action is restrict - */ - let action = Relation::NO_ACTION; + /** + * By default action is restrict + */ + 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 + */ + if action != Relation::ACTION_CASCADE { + continue; + } - /** - * Load a plain instance from the models manager - */ - let referencedModel = manager->load(relation->getReferencedModel()); + /** + * Load a plain instance from the models manager + */ + 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", - bindParams[] = value; - } + 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; } - } return true; @@ -1880,108 +1868,108 @@ 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 { - - /** - * Check if the relation has a virtual foreign key - */ - let foreignKey = relation->getForeignKey(); - if foreignKey !== false { - - /** - * 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"]; - } - } + let error = false; + for relation in relations { - /** - * Check only if the operation is restrict - */ - if action == Relation::ACTION_RESTRICT { + /** + * Check if the relation has a virtual foreign key + */ + let foreignKey = relation->getForeignKey(); + if foreignKey === false { + continue; + } - let relationClass = relation->getReferencedModel(); + /** + * By default action is restrict + */ + let action = Relation::ACTION_RESTRICT; - /** - * Load a plain instance from the models manager - */ - let referencedModel = manager->load(relationClass); + /** + * 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"]; + } + } - let fields = relation->getFields(), - referencedFields = relation->getReferencedFields(); + /** + * Check only if the operation is restrict + */ + if action != Relation::ACTION_RESTRICT { + continue; + } - /** - * Create the checking conditions. A relation can has many fields or a single one - */ - let conditions = [], bindParams = []; + let relationClass = relation->getReferencedModel(); - if typeof fields == "array" { + /** + * Load a plain instance from the models manager + */ + let referencedModel = manager->load(relationClass); - for position, field in fields { - fetch value, this->{field}; - let conditions[] = "[" . referencedFields[position] . "] = ?" . position, - bindParams[] = value; - } + let fields = relation->getFields(), + referencedFields = relation->getReferencedFields(); - } else { - fetch value, this->{fields}; - let conditions[] = "[" . referencedFields . "] = ?0", - bindParams[] = value; - } + /** + * Create the checking conditions. A relation can has many fields or a single one + */ + let conditions = [], bindParams = []; - /** - * Check if the virtual foreign key has extra conditions - */ - if fetch extraConditions, foreignKey["conditions"] { - let conditions[] = extraConditions; - } + if typeof fields == "array" { - /** - * 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]) { + for position, field in fields { + fetch value, this->{field}; + let conditions[] = "[" . referencedFields[position] . "] = ?" . position, + bindParams[] = value; + } - /** - * Create a new message - */ - if !fetch message, foreignKey["message"] { - let message = "Record is referenced by model " . relationClass; - } + } else { + fetch value, this->{fields}; + let conditions[] = "[" . referencedFields . "] = ?0", + bindParams[] = value; + } - /** - * Create a message - */ - this->appendMessage(new Message(message, fields, "ConstraintViolation")); - let error = true; - break; - } - } - } + /** + * Check if the virtual foreign key has extra conditions + */ + if fetch extraConditions, foreignKey["conditions"] { + let conditions[] = extraConditions; } /** - * Call validation fails event + * 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 error === true { - if globals_get("orm.events") { - this->fireEvent("onValidationFails"); - this->_cancelOperation(); + 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; } - return false; + + /** + * Create a message + */ + 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; + } + return true; } @@ -3462,16 +3450,8 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface */ protected function skipAttributes(array! attributes) { - var keysAttributes, metaData, attribute; - - let keysAttributes = []; - for attribute in attributes { - let keysAttributes[attribute] = null; - } - - let metaData = this->getModelsMetaData(); - metaData->setAutomaticCreateAttributes(this, keysAttributes); - metaData->setAutomaticUpdateAttributes(this, keysAttributes); + this->skipAttributesOnCreate(attributes); + this->skipAttributesOnUpdate(attributes); } /** @@ -3780,12 +3760,11 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface let snapshot[attribute] = value; } - - let this->_snapshot = snapshot; - return null; + } else { + let snapshot = data; } - let this->_snapshot = data; + let this->_snapshot = snapshot; } /** @@ -3815,106 +3794,18 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface */ public function hasChanged(var fieldName = null) -> boolean { - var snapshot, metaData, columnMap, allAttributes, value, - originalValue, name; - - let snapshot = this->_snapshot; - if typeof snapshot != "array" { - throw new Exception("The record doesn't have a valid data snapshot"); - } - - /** - * Dirty state must be DIRTY_PERSISTENT to make the checking - */ - if this->_dirtyState != self::DIRTY_STATE_PERSISTENT { - throw new Exception("Change checking cannot be performed because the object has not been persisted or is deleted"); - } + var changedFields; - /** - * Return the models meta-data - */ - let metaData = this->getModelsMetaData(); - - /** - * The reversed column map is an array if the model has a column map - */ - let columnMap = metaData->getReverseColumnMap(this); - - /** - * Data types are field indexed - */ - if typeof columnMap != "array" { - let allAttributes = metaData->getDataTypes(this); - } else { - let allAttributes = columnMap; - } + let changedFields = this->getChangedFields(); /** * If a field was specified we only check it */ if typeof fieldName == "string" { - - /** - * We only make this validation over valid fields - */ - if typeof columnMap == "array" { - if !isset columnMap[fieldName] { - throw new Exception("The field '" . fieldName . "' is not part of the model"); - } - } else { - if !isset allAttributes[fieldName] { - throw new Exception("The field '" . fieldName . "' is not part of the model"); - } - } - - /** - * The field is not part of the model, throw exception - */ - if !fetch value, this->{fieldName} { - throw new Exception("The field '" . fieldName . "' is not defined on the model"); - } - - /** - * The field is not part of the data snapshot, throw exception - */ - if !fetch originalValue, snapshot[fieldName] { - throw new Exception("The field '" . fieldName . "' was not found in the snapshot"); - } - - /** - * Check if the field has changed - */ - return value != originalValue; + return in_array(fieldName, changedFields); } - /** - * Check every attribute in the model - */ - for name, _ in allAttributes { - - /** - * If some attribute is not present in the snapshot, we assume the record as changed - */ - if !fetch originalValue, snapshot[name] { - return true; - } - - /** - * If some attribute is not present in the model, we assume the record as changed - */ - if !fetch value, this->{name} { - return true; - } - - /** - * Check if the field has changed - */ - if value != originalValue { - return true; - } - } - - return false; + return count(changedFields) > 0; } /** @@ -4067,28 +3958,26 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface /** * Calling count if the method starts with "count" */ - if typeof relation != "object" { - if starts_with(method, "count") { - let queryMethod = "count", - relation = manager->getRelationByAlias(modelName, substr(method, 5)); - } + elseif starts_with(method, "count") { + let queryMethod = "count", + relation = manager->getRelationByAlias(modelName, substr(method, 5)); } /** * If the relation was found perform the query via the models manager */ - if typeof relation == "object" { - fetch extraArgs, arguments[0]; - - return manager->getRelationRecords( - relation, - queryMethod, - this, - extraArgs - ); + if typeof relation != "object" { + return null; } - return null; + fetch extraArgs, arguments[0]; + + return manager->getRelationRecords( + relation, + queryMethod, + this, + extraArgs + ); } /** @@ -4116,21 +4005,17 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface /** * Check if the method starts with "find" */ - if extraMethod === null { - if starts_with(method, "findBy") { - let type = "find", - extraMethod = substr(method, 6); - } + elseif starts_with(method, "findBy") { + let type = "find", + extraMethod = substr(method, 6); } /** * Check if the method starts with "count" */ - if extraMethod === null { - if starts_with(method, "countBy") { - let type = "count", - extraMethod = substr(method, 7); - } + elseif starts_with(method, "countBy") { + let type = "count", + extraMethod = substr(method, 7); } /** diff --git a/phalcon/mvc/model/criteria.zep b/phalcon/mvc/model/criteria.zep index d5d4efb68d5..b8483e42029 100644 --- a/phalcon/mvc/model/criteria.zep +++ b/phalcon/mvc/model/criteria.zep @@ -232,7 +232,7 @@ class Criteria implements CriteriaInterface, InjectionAwareInterface */ public function where(string! conditions, var bindParams = null, var bindTypes = null) -> { - var currentBindParams, mergedParams, mergedParamsTypes, currentBindTypes; + var currentBindParams, currentBindTypes; let this->_params["conditions"] = conditions; @@ -241,11 +241,10 @@ class Criteria implements CriteriaInterface, InjectionAwareInterface */ if typeof bindParams == "array" { if fetch currentBindParams, this->_params["bind"] { - let mergedParams = array_merge(currentBindParams, bindParams); + let this->_params["bind"] = array_merge(currentBindParams, bindParams); } else { - let mergedParams = bindParams; + let this->_params["bind"] = bindParams; } - let this->_params["bind"] = mergedParams; } /** @@ -253,11 +252,10 @@ class Criteria implements CriteriaInterface, InjectionAwareInterface */ if typeof bindTypes == "array" { if fetch currentBindTypes, this->_params["bindTypes"] { - let mergedParamsTypes = array_merge(currentBindTypes, bindTypes); + let this->_params["bindTypes"] = array_merge(currentBindTypes, bindTypes); } else { - let mergedParamsTypes = bindTypes; + let this->_params["bindTypes"] = bindTypes; } - let this->_params["bindTypes"] = mergedParamsTypes; } return this; @@ -279,40 +277,13 @@ class Criteria implements CriteriaInterface, InjectionAwareInterface */ public function andWhere(string! conditions, var bindParams = null, var bindTypes = null) -> { - var currentBindParams, mergedParams, mergedParamsTypes, currentBindTypes, params, currentConditions; + var currentConditions; - let params = this->_params; - if fetch currentConditions, params["conditions"] { - let this->_params["conditions"] = "(" . currentConditions . ") AND (" . conditions . ")"; - } else { - let this->_params["conditions"] = conditions; - } - - /** - * Update or merge existing bound parameters - */ - if typeof bindParams == "array" { - if fetch currentBindParams, params["bind"] { - let mergedParams = array_merge(currentBindParams, bindParams); - } else { - let mergedParams = bindParams; - } - let this->_params["bind"] = mergedParams; + if fetch currentConditions, this->_params["conditions"] { + let conditions = "(" . currentConditions . ") AND (" . conditions . ")"; } - /** - * Update or merge existing bind types parameters - */ - if typeof bindTypes == "array" { - if fetch currentBindTypes, params["bindTypes"] { - let mergedParamsTypes = array_merge(currentBindTypes, bindTypes); - } else { - let mergedParamsTypes = bindTypes; - } - let this->_params["bindTypes"] = mergedParamsTypes; - } - - return this; + return this->where(conditions, bindParams, bindTypes); } /** @@ -320,40 +291,13 @@ class Criteria implements CriteriaInterface, InjectionAwareInterface */ public function orWhere(string! conditions, var bindParams = null, var bindTypes = null) -> { - var currentBindParams, mergedParams, mergedParamsTypes, currentBindTypes, params, currentConditions; + var currentConditions; - let params = this->_params; - if fetch currentConditions, params["conditions"] { - let this->_params["conditions"] = "(" . currentConditions . ") OR (" . conditions . ")"; - } else { - let this->_params["conditions"] = conditions; - } - - /** - * Update or merge existing bound parameters - */ - if typeof bindParams == "array" { - if fetch currentBindParams, params["bind"] { - let mergedParams = array_merge(currentBindParams, bindParams); - } else { - let mergedParams = bindParams; - } - let this->_params["bind"] = mergedParams; + if fetch currentConditions, this->_params["conditions"] { + let conditions = "(" . currentConditions . ") OR (" . conditions . ")"; } - /** - * Update or merge existing bind types parameters - */ - if typeof bindTypes == "array" { - if fetch currentBindTypes, params["bindTypes"] { - let mergedParamsTypes = array_merge(currentBindTypes, bindTypes); - } else { - let mergedParamsTypes = bindTypes; - } - let this->_params["bindTypes"] = mergedParamsTypes; - } - - return this; + return this->where(conditions, bindParams, bindTypes); } /** diff --git a/phalcon/mvc/model/manager.zep b/phalcon/mvc/model/manager.zep index d68aa9e4041..dbbfedfb01b 100644 --- a/phalcon/mvc/model/manager.zep +++ b/phalcon/mvc/model/manager.zep @@ -201,14 +201,13 @@ class Manager implements ManagerInterface, InjectionAwareInterface, EventsAwareI */ public function getCustomEventsManager( model) -> | boolean { - var customEventsManager, eventsManager; - let customEventsManager = this->_customEventsManager; - if typeof customEventsManager == "array" { - if fetch eventsManager, customEventsManager[get_class_lower(model)] { - return eventsManager; - } + var eventsManager; + + if !fetch eventsManager, this->_customEventsManager[get_class_lower(model)] { + return false; } - return false; + + return eventsManager; } /** @@ -284,34 +283,33 @@ class Manager implements ManagerInterface, InjectionAwareInterface, EventsAwareI let colonPos = strpos(modelName, ":"); if colonPos !== false { - let className = substr(modelName,colonPos+1); - let namespaceAlias = substr(modelName,0,colonPos); + let className = substr(modelName, colonPos + 1); + let namespaceAlias = substr(modelName, 0, colonPos); let namespaceName = this->getNamespaceAlias(namespaceAlias); - let modelName = namespaceName."\\".className; + let modelName = namespaceName . "\\" . className; } /** - * Check if a model with the same is already loaded + * The model doesn't exist throw an exception */ - if fetch model, this->_initialized[strtolower(modelName)] { - if newInstance { - return new {modelName}(null, this->_dependencyInjector, this); - } - model->reset(); - return model; + if !class_exists(modelName) { + throw new Exception("Model '" . modelName . "' could not be loaded"); } /** - * Load it using an autoloader + * Check if a model with the same is already loaded */ - if class_exists(modelName) { - return new {modelName}(null, this->_dependencyInjector, this); + if !newInstance { + if fetch model, this->_initialized[strtolower(modelName)] { + model->reset(); + return model; + } } /** - * The model doesn't exist throw an exception + * Load it using an autoloader */ - throw new Exception("Model '" . modelName . "' could not be loaded"); + return new {modelName}(null, this->_dependencyInjector, this); } /** @@ -353,20 +351,15 @@ class Manager implements ManagerInterface, InjectionAwareInterface, EventsAwareI */ public function getModelSource( model) -> string { - var sources, entityName, source; + var entityName; let entityName = get_class_lower(model); - let sources = this->_sources; - if typeof sources == "array" { - if fetch source, sources[entityName] { - return source; - } + if !isset this->_sources[entityName] { + let this->_sources[entityName] = uncamelize(get_class_ns(model)); } - let source = uncamelize(get_class_ns(model)), - this->_sources[entityName] = source; - return source; + return this->_sources[entityName]; } /** @@ -382,14 +375,13 @@ class Manager implements ManagerInterface, InjectionAwareInterface, EventsAwareI */ public function getModelSchema( model) -> string { - var schemas, schema; - let schemas = this->_schemas; - if typeof schemas == "array" { - if fetch schema, schemas[get_class_lower(model)] { - return schema; - } + var schema; + + if !fetch schema, this->_schemas[get_class_lower(model)] { + return ""; } - return ""; + + return schema; } /** @@ -397,10 +389,8 @@ class Manager implements ManagerInterface, InjectionAwareInterface, EventsAwareI */ public function setConnectionService( model, string! connectionService) -> void { - var entityName; - let entityName = get_class_lower(model), - this->_readConnectionServices[entityName] = connectionService, - this->_writeConnectionServices[entityName] = connectionService; + this->setReadConnectionService(model, connectionService); + this->setWriteConnectionService(model, connectionService); } /** @@ -484,13 +474,11 @@ class Manager implements ManagerInterface, InjectionAwareInterface, EventsAwareI { var connection; - if typeof connectionServices == "array" { - if fetch connection, connectionServices[get_class_lower(model)] { - return connection; - } + if !fetch connection, connectionServices[get_class_lower(model)] { + return "db"; } - return "db"; + return connection; } /** @@ -499,26 +487,22 @@ class Manager implements ManagerInterface, InjectionAwareInterface, EventsAwareI */ public function notifyEvent(string! eventName, model) { - var status, behavior, modelsBehaviors, eventsManager, - customEventsManager, behaviors; + var status, behavior, modelsBehaviors, eventsManager, customEventsManager; let status = null; /** * Dispatch events to the global events manager */ - let behaviors = this->_behaviors; - if typeof behaviors == "array" { - if fetch modelsBehaviors, behaviors[get_class_lower(model)] { - - /** - * Notify all the events on the behavior - */ - for behavior in modelsBehaviors { - let status = behavior->notify(eventName, model); - if status === false { - return false; - } + if fetch modelsBehaviors, this->_behaviors[get_class_lower(model)] { + + /** + * Notify all the events on the behavior + */ + for behavior in modelsBehaviors { + let status = behavior->notify(eventName, model); + if status === false { + return false; } } } @@ -537,13 +521,10 @@ class Manager implements ManagerInterface, InjectionAwareInterface, EventsAwareI /** * A model can has a specific events manager for it */ - let customEventsManager = this->_customEventsManager; - if typeof customEventsManager == "array" { - if fetch customEventsManager, customEventsManager[get_class_lower(model)] { - let status = customEventsManager->fire("model:" . eventName, model); - if status === false { - return false; - } + if fetch customEventsManager, this->_customEventsManager[get_class_lower(model)] { + let status = customEventsManager->fire("model:" . eventName, model); + if status === false { + return false; } } @@ -557,24 +538,20 @@ class Manager implements ManagerInterface, InjectionAwareInterface, EventsAwareI */ public function missingMethod( model, string! eventName, var data) { - var behaviors, modelsBehaviors, result, eventsManager, behavior; + var modelsBehaviors, result, eventsManager, behavior; /** * Dispatch events to the global events manager */ - let behaviors = this->_behaviors; - if typeof behaviors == "array" { - - if fetch modelsBehaviors, behaviors[get_class_lower(model)] { + if fetch modelsBehaviors, this->_behaviors[get_class_lower(model)] { - /** - * Notify all the events on the behavior - */ - for behavior in modelsBehaviors { - let result = behavior->missingMethod(model, eventName, data); - if result !== null { - return result; - } + /** + * Notify all the events on the behavior + */ + for behavior in modelsBehaviors { + let result = behavior->missingMethod(model, eventName, data); + if result !== null { + return result; } } } @@ -1133,14 +1110,13 @@ class Manager implements ManagerInterface, InjectionAwareInterface, EventsAwareI */ public function getRelationByAlias(string! modelName, string! alias) -> | boolean { - var aliases, relation; - let aliases = this->_aliases; - if typeof aliases == "array" { - if fetch relation, aliases[strtolower(modelName . "$" . alias)] { - return relation; - } + var relation; + + if !fetch relation, this->_aliases[strtolower(modelName . "$" . alias)] { + return false; } - return false; + + return relation; } /** @@ -1430,28 +1406,21 @@ class Manager implements ManagerInterface, InjectionAwareInterface, EventsAwareI public function getBelongsToRecords(string! method, string! modelName, var modelRelation, record, parameters = null) -> | boolean { - var belongsTo, keyRelation, relations; - - let belongsTo = this->_hasMany; - if typeof belongsTo == "array" { + var keyRelation, relations; - /** - * Check if there is a relation between them - */ - let keyRelation = strtolower(modelName) . "$" . strtolower(modelRelation); - if !isset belongsTo[keyRelation] { - return false; - } - - /** - * "relations" is an array with all the belongsTo relationships to that model - * Perform the query - */ - let relations = belongsTo[keyRelation]; - return this->getRelationRecords(relations[0], method, record, parameters); + /** + * Check if there is a relation between them + */ + let keyRelation = strtolower(modelName) . "$" . strtolower(modelRelation); + if !fetch relations, this->_hasMany[keyRelation] { + return false; } - return false; + /** + * "relations" is an array with all the belongsTo relationships to that model + * Perform the query + */ + return this->getRelationRecords(relations[0], method, record, parameters); } /** @@ -1460,28 +1429,21 @@ class Manager implements ManagerInterface, InjectionAwareInterface, EventsAwareI public function getHasManyRecords(string! method, string! modelName, var modelRelation, record, parameters = null) -> | boolean { - var hasMany, keyRelation, relations; + var keyRelation, relations; - let hasMany = this->_hasMany; - if typeof hasMany == "array" { - - /** - * Check if there is a relation between them - */ - let keyRelation = strtolower(modelName) . "$" . strtolower(modelRelation); - if !isset hasMany[keyRelation] { - return false; - } - - /** - * "relations" is an array with all the hasMany relationships to that model - * Perform the query - */ - let relations = hasMany[keyRelation]; - return this->getRelationRecords(relations[0], method, record, parameters); + /** + * Check if there is a relation between them + */ + let keyRelation = strtolower(modelName) . "$" . strtolower(modelRelation); + if !fetch relations, this->_hasMany[keyRelation] { + return false; } - return false; + /** + * "relations" is an array with all the hasMany relationships to that model + * Perform the query + */ + return this->getRelationRecords(relations[0], method, record, parameters); } /** @@ -1490,28 +1452,21 @@ class Manager implements ManagerInterface, InjectionAwareInterface, EventsAwareI public function getHasOneRecords(string! method, string! modelName, var modelRelation, record, parameters = null) -> | boolean { - var hasOne, keyRelation, relations; - - let hasOne = this->_hasOne; - if typeof hasOne == "array" { + var keyRelation, relations; - /** - * Check if there is a relation between them - */ - let keyRelation = strtolower(modelName) . "$" . strtolower(modelRelation); - if !isset hasOne[keyRelation] { - return false; - } - - /** - * "relations" is an array with all the belongsTo relationships to that model - * Perform the query - */ - let relations = hasOne[keyRelation]; - return this->getRelationRecords(relations[0], method, record, parameters); + /** + * Check if there is a relation between them + */ + let keyRelation = strtolower(modelName) . "$" . strtolower(modelRelation); + if !fetch relations, this->_hasOne[keyRelation] { + return false; } - return false; + /** + * "relations" is an array with all the belongsTo relationships to that model + * Perform the query + */ + return this->getRelationRecords(relations[0], method, record, parameters); } /** @@ -1525,14 +1480,13 @@ class Manager implements ManagerInterface, InjectionAwareInterface, EventsAwareI */ public function getBelongsTo( model) -> | array { - var belongsToSingle, relations; - let belongsToSingle = this->_belongsToSingle; - if typeof belongsToSingle == "array" { - if fetch relations, belongsToSingle[get_class_lower(model)] { - return relations; - } + var relations; + + if !fetch relations, this->_belongsToSingle[get_class_lower(model)] { + return []; } - return []; + + return relations; } /** @@ -1540,15 +1494,13 @@ class Manager implements ManagerInterface, InjectionAwareInterface, EventsAwareI */ public function getHasMany( model) -> | array { - var hasManySingle, relations; - let hasManySingle = this->_hasManySingle; - if typeof hasManySingle == "array" { - if fetch relations, hasManySingle[get_class_lower(model)] { - return relations; - } + var relations; + if !fetch relations, this->_hasManySingle[get_class_lower(model)] { + return []; } - return []; + + return relations; } /** @@ -1556,14 +1508,13 @@ class Manager implements ManagerInterface, InjectionAwareInterface, EventsAwareI */ public function getHasOne( model) -> array { - var hasOneSingle, relations; - let hasOneSingle = this->_hasOneSingle; - if typeof hasOneSingle == "array" { - if fetch relations, hasOneSingle[get_class_lower(model)] { - return relations; - } + var relations; + + if !fetch relations, this->_hasOneSingle[get_class_lower(model)] { + return []; } - return []; + + return relations; } /** @@ -1571,14 +1522,13 @@ class Manager implements ManagerInterface, InjectionAwareInterface, EventsAwareI */ public function getHasManyToMany( model) -> | array { - var hasManyToManySingle, relations; - let hasManyToManySingle = this->_hasManyToManySingle; - if typeof hasManyToManySingle == "array" { - if fetch relations, hasManyToManySingle[get_class_lower(model)] { - return relations; - } + var relations; + + if !fetch relations, this->_hasManyToManySingle[get_class_lower(model)] { + return []; } - return []; + + return relations; } /** @@ -1594,8 +1544,7 @@ class Manager implements ManagerInterface, InjectionAwareInterface, EventsAwareI */ public function getRelations(string! modelName) -> { - var entityName, allRelations, relations, - belongsTo, relation, hasOne, hasMany; + var entityName, allRelations, relations, relation; let entityName = strtolower(modelName), allRelations = []; @@ -1603,36 +1552,27 @@ class Manager implements ManagerInterface, InjectionAwareInterface, EventsAwareI /** * Get belongs-to relations */ - let belongsTo = this->_belongsToSingle; - if typeof belongsTo == "array" { - if fetch relations, belongsTo[entityName] { - for relation in relations { - let allRelations[] = relation; - } + if fetch relations, this->_belongsToSingle[entityName] { + for relation in relations { + let allRelations[] = relation; } } /** * Get has-many relations */ - let hasMany = this->_hasManySingle; - if typeof hasMany == "array" { - if fetch relations, hasMany[entityName] { - for relation in relations { - let allRelations[] = relation; - } + if fetch relations, this->_hasManySingle[entityName] { + for relation in relations { + let allRelations[] = relation; } } /** * Get has-one relations */ - let hasOne = this->_hasOneSingle; - if typeof hasOne == "array" { - if fetch relations, hasOne[entityName] { - for relation in relations { - let allRelations[] = relation; - } + if fetch relations, this->_hasOneSingle[entityName] { + for relation in relations { + let allRelations[] = relation; } } @@ -1644,38 +1584,29 @@ class Manager implements ManagerInterface, InjectionAwareInterface, EventsAwareI */ public function getRelationsBetween(string! first, string! second) -> | boolean { - var keyRelation, belongsTo, hasMany, hasOne, relations; + var keyRelation, relations; let keyRelation = strtolower(first) . "$" . strtolower(second); /** * Check if it's a belongs-to relationship */ - let belongsTo = this->_belongsTo; - if typeof belongsTo == "array" { - if fetch relations, belongsTo[keyRelation] { - return relations; - } + if fetch relations, this->_belongsTo[keyRelation] { + return relations; } /** * Check if it's a has-many relationship */ - let hasMany = this->_hasMany; - if typeof hasMany == "array" { - if fetch relations, hasMany[keyRelation] { - return relations; - } + if fetch relations, this->_hasMany[keyRelation] { + return relations; } /** * Check whether it's a has-one relationship */ - let hasOne = this->_hasOne; - if typeof hasOne == "array" { - if fetch relations, hasOne[keyRelation] { - return relations; - } + if fetch relations, this->_hasOne[keyRelation] { + return relations; } return false; diff --git a/phalcon/mvc/model/metadata.zep b/phalcon/mvc/model/metadata.zep index a44499222c5..09f6c254ff0 100644 --- a/phalcon/mvc/model/metadata.zep +++ b/phalcon/mvc/model/metadata.zep @@ -218,13 +218,11 @@ abstract class MetaData implements InjectionAwareInterface, MetaDataInterface */ public function getStrategy() -> { - var strategy; - let strategy = this->_strategy; - if typeof strategy == "null" { - let strategy = new Introspection(), - this->_strategy = strategy; + if typeof this->_strategy == "null" { + let this->_strategy = new Introspection(); } - return strategy; + + return this->_strategy; } /** @@ -270,7 +268,7 @@ abstract class MetaData implements InjectionAwareInterface, MetaDataInterface */ public final function readMetaDataIndex( model, int index) { - var source, schema, key, metaData; + var source, schema, key; let source = model->getSource(), schema = model->getSchema(); @@ -280,11 +278,10 @@ abstract class MetaData implements InjectionAwareInterface, MetaDataInterface */ let key = get_class_lower(model) . "-" . schema . source; - if fetch metaData, this->_metaData[key][index] { - return metaData; + if !isset this->_metaData[key][index] { + this->_initialize(model, key, source, schema); } - this->_initialize(model, key, source, schema); return this->_metaData[key][index]; } @@ -323,9 +320,7 @@ abstract class MetaData implements InjectionAwareInterface, MetaDataInterface this->_initialize(model, key, source, schema); } - let metaData = this->_metaData, - metaData[key][index] = data, - this->_metaData = metaData; + let this->_metaData[key][index] = data; } /** diff --git a/phalcon/mvc/model/query/builder.zep b/phalcon/mvc/model/query/builder.zep index 82b4994b446..b2bdf0efe50 100644 --- a/phalcon/mvc/model/query/builder.zep +++ b/phalcon/mvc/model/query/builder.zep @@ -571,7 +571,7 @@ class Builder implements BuilderInterface, InjectionAwareInterface */ public function where(var conditions, var bindParams = null, var bindTypes = null) -> { - var currentBindParams, currentBindTypes, mergedParams, mergedTypes; + var currentBindParams, currentBindTypes; let this->_conditions = conditions; @@ -581,11 +581,10 @@ class Builder implements BuilderInterface, InjectionAwareInterface if typeof bindParams == "array" { let currentBindParams = this->_bindParams; if typeof currentBindParams == "array" { - let mergedParams = currentBindParams + bindParams; + let this->_bindParams = currentBindParams + bindParams; } else { - let mergedParams = bindParams; + let this->_bindParams = bindParams; } - let this->_bindParams = mergedParams; } /** @@ -594,11 +593,10 @@ class Builder implements BuilderInterface, InjectionAwareInterface if typeof bindTypes == "array" { let currentBindTypes = this->_bindTypes; if typeof currentBindParams == "array" { - let mergedTypes = currentBindTypes + bindTypes; + let this->_bindTypes = currentBindTypes + bindTypes; } else { - let mergedTypes = bindTypes; + let this->_bindTypes = bindTypes; } - let this->_bindTypes = mergedTypes; } return this; @@ -626,8 +624,7 @@ class Builder implements BuilderInterface, InjectionAwareInterface */ public function andWhere(string! conditions, var bindParams = null, var bindTypes = null) -> { - var currentBindParams, currentBindTypes, mergedParams, - mergedTypes, currentConditions, newConditions; + var currentConditions; let currentConditions = this->_conditions; @@ -635,39 +632,10 @@ class Builder implements BuilderInterface, InjectionAwareInterface * Nest the condition to current ones or set as unique */ if currentConditions { - let newConditions = "(" . currentConditions . ") AND (" . conditions . ")"; - } else { - let newConditions = conditions; - } - let this->_conditions = newConditions; - - /** - * Merge the bind params to the current ones - */ - if typeof bindParams == "array" { - let currentBindParams = this->_bindParams; - if typeof currentBindParams == "array" { - let mergedParams = currentBindParams + bindParams; - } else { - let mergedParams = bindParams; - } - let this->_bindParams = mergedParams; + let conditions = "(" . currentConditions . ") AND (" . conditions . ")"; } - /** - * Merge the bind types to the current ones - */ - if typeof bindTypes == "array" { - let currentBindTypes = this->_bindTypes; - if typeof currentBindParams == "array" { - let mergedTypes = currentBindTypes + bindTypes; - } else { - let mergedTypes = bindTypes; - } - let this->_bindTypes = mergedTypes; - } - - return this; + return this->where(conditions, bindParams, bindTypes); } /** @@ -692,46 +660,18 @@ class Builder implements BuilderInterface, InjectionAwareInterface */ public function orWhere(string! conditions, var bindParams = null, var bindTypes = null) -> { - var currentBindParams, currentBindTypes, mergedParams, - mergedTypes, currentConditions; + var currentConditions; - /** - * Nest the condition to current ones or set as unique - */ let currentConditions = this->_conditions; - if currentConditions { - let this->_conditions = "(" . currentConditions . ") OR (" . conditions . ")"; - } else { - let this->_conditions = conditions; - } - - /** - * Merge the bind params to the current ones - */ - if typeof bindParams == "array" { - let currentBindParams = this->_bindParams; - if typeof currentBindParams == "array" { - let mergedParams = currentBindParams + bindParams; - } else { - let mergedParams = bindParams; - } - let this->_bindParams = mergedParams; - } /** - * Merge the bind types to the current ones + * Nest the condition to current ones or set as unique */ - if typeof bindTypes == "array" { - let currentBindTypes = this->_bindTypes; - if typeof currentBindParams == "array" { - let mergedTypes = currentBindTypes + bindTypes; - } else { - let mergedTypes = bindTypes; - } - let this->_bindTypes = mergedTypes; + if currentConditions { + let conditions = "(" . currentConditions . ") OR (" . conditions . ")"; } - return this; + return this->where(conditions, bindParams, bindTypes); } /** diff --git a/phalcon/mvc/model/relation.zep b/phalcon/mvc/model/relation.zep index dd450fa9074..d1b298a4a51 100644 --- a/phalcon/mvc/model/relation.zep +++ b/phalcon/mvc/model/relation.zep @@ -157,14 +157,7 @@ class Relation implements RelationInterface */ public function isForeignKey() -> boolean { - var options; - let options = this->_options; - if typeof options == "array" { - if isset options["foreignKey"] { - return true; - } - } - return false; + return isset this->_options["foreignKey"]; } /** diff --git a/phalcon/mvc/model/transaction.zep b/phalcon/mvc/model/transaction.zep index e23f14eb504..cb1f14e5c50 100644 --- a/phalcon/mvc/model/transaction.zep +++ b/phalcon/mvc/model/transaction.zep @@ -130,7 +130,7 @@ class Transaction implements TransactionInterface let manager = this->_manager; if typeof manager == "object" { - call_user_func_array([manager, "notifyCommit"], [this]); + manager->notifyCommit(this); } return this->_connection->commit(); @@ -149,7 +149,7 @@ class Transaction implements TransactionInterface let manager = this->_manager; if typeof manager == "object" { - call_user_func_array([manager, "notifyRollback"], [this]); + manager->notifyRollback(this); } let connection = this->_connection; @@ -226,5 +226,4 @@ class Transaction implements TransactionInterface { let this->_rollbackRecord = record; } - }