From 1e370a74489e1e01c28ef4ba5db1f6e883af4916 Mon Sep 17 00:00:00 2001 From: phalcon Date: Tue, 16 Oct 2012 09:13:35 -0500 Subject: [PATCH] Fixing bug passing an array to magic methods in Phalcon\Mvc\Model (2) --- ext/mvc/model.c | 11 +++------- ext/mvc/model/manager.c | 22 ++++++++++++++++---- unit-tests/ModelsRelationsTest.php | 32 ++++++++++++++++++++++++------ unit-tests/models/Some/Robots.php | 4 ++-- 4 files changed, 49 insertions(+), 20 deletions(-) diff --git a/ext/mvc/model.c b/ext/mvc/model.c index 39bd326ea2d..3207573493e 100755 --- a/ext/mvc/model.c +++ b/ext/mvc/model.c @@ -3637,7 +3637,7 @@ PHP_METHOD(Phalcon_Mvc_Model, getRelated){ zval *model_name, *arguments = NULL, *dependency_injector; zval *service, *manager, *class_name, *exists = NULL, *manager_method = NULL; zval *query_method = NULL, *exception_message, *call_object; - zval *model_args, *arguments_merge = NULL, *result; + zval *model_args, *result; PHALCON_MM_GROW(); @@ -3716,15 +3716,10 @@ PHP_METHOD(Phalcon_Mvc_Model, getRelated){ phalcon_array_append(&model_args, class_name, PH_SEPARATE TSRMLS_CC); phalcon_array_append(&model_args, model_name, PH_SEPARATE TSRMLS_CC); phalcon_array_append(&model_args, this_ptr, PH_SEPARATE TSRMLS_CC); - if (Z_TYPE_P(arguments) == IS_ARRAY) { - PHALCON_INIT_VAR(arguments_merge); - PHALCON_CALL_FUNC_PARAMS_2(arguments_merge, "array_merge", model_args, arguments); - } else { - PHALCON_CPY_WRT(arguments_merge, model_args); - } + phalcon_array_append(&model_args, arguments, PH_SEPARATE TSRMLS_CC); PHALCON_INIT_VAR(result); - PHALCON_CALL_FUNC_PARAMS_2(result, "call_user_func_array", call_object, arguments_merge); + PHALCON_CALL_FUNC_PARAMS_2(result, "call_user_func_array", call_object, model_args); RETURN_CCTOR(result); } diff --git a/ext/mvc/model/manager.c b/ext/mvc/model/manager.c index 903f26a2932..b9b9c94efc4 100755 --- a/ext/mvc/model/manager.c +++ b/ext/mvc/model/manager.c @@ -722,9 +722,9 @@ PHP_METHOD(Phalcon_Mvc_Model_Manager, _getRelationRecords){ zval *pre_conditions = NULL, *conditions = NULL, *fields, *field = NULL; zval *value = NULL, *referenced_field = NULL, *condition = NULL, *i; zval *referenced_fields, *join_conditions; - zval *find_params, *arguments, *reference_table; - zval *referenced_entity, *connection_service; - zval *call_object, *records; + zval *find_params, *find_arguments = NULL, *arguments; + zval *reference_table, *referenced_entity; + zval *connection_service, *call_object, *records; HashTable *ah0; HashPosition hp0; zval **hd; @@ -740,6 +740,8 @@ PHP_METHOD(Phalcon_Mvc_Model_Manager, _getRelationRecords){ if (!parameters) { PHALCON_INIT_NVAR(parameters); + } else { + PHALCON_SEPARATE_PARAM(parameters); } if (Z_TYPE_P(parameters) == IS_ARRAY) { @@ -747,6 +749,8 @@ PHP_METHOD(Phalcon_Mvc_Model_Manager, _getRelationRecords){ if (eval_int) { PHALCON_INIT_VAR(placeholders); phalcon_array_fetch_string(&placeholders, parameters, SL("bind"), PH_NOISY_CC); + PHALCON_SEPARATE_PARAM(parameters); + phalcon_array_unset_string(parameters, SS("bind")); } else { PHALCON_INIT_NVAR(placeholders); array_init(placeholders); @@ -761,11 +765,15 @@ PHP_METHOD(Phalcon_Mvc_Model_Manager, _getRelationRecords){ eval_int = phalcon_array_isset_long(parameters, 0); if (eval_int) { phalcon_array_fetch_long(&pre_conditions, parameters, 0, PH_NOISY_CC); + PHALCON_SEPARATE_PARAM(parameters); + phalcon_array_unset_long(parameters, 0); } else { eval_int = phalcon_array_isset_string(parameters, SS("conditions")); if (eval_int) { PHALCON_INIT_NVAR(pre_conditions); phalcon_array_fetch_string(&pre_conditions, parameters, SL("conditions"), PH_NOISY_CC); + PHALCON_SEPARATE_PARAM(parameters); + phalcon_array_unset_string(parameters, SS("conditions")); } } } else { @@ -848,10 +856,16 @@ PHP_METHOD(Phalcon_Mvc_Model_Manager, _getRelationRecords){ array_init(find_params); phalcon_array_append(&find_params, join_conditions, PH_SEPARATE TSRMLS_CC); phalcon_array_update_string(&find_params, SL("bind"), &placeholders, PH_COPY | PH_SEPARATE TSRMLS_CC); + if (Z_TYPE_P(parameters) == IS_ARRAY) { + PHALCON_INIT_VAR(find_arguments); + PHALCON_CALL_FUNC_PARAMS_2(find_arguments, "array_merge", find_params, parameters); + } else { + PHALCON_CPY_WRT(find_arguments, find_params); + } PHALCON_INIT_VAR(arguments); array_init(arguments); - phalcon_array_append(&arguments, find_params, PH_SEPARATE TSRMLS_CC); + phalcon_array_append(&arguments, find_arguments, PH_SEPARATE TSRMLS_CC); PHALCON_INIT_VAR(reference_table); phalcon_array_fetch_string(&reference_table, relation, SL("rt"), PH_NOISY_CC); diff --git a/unit-tests/ModelsRelationsTest.php b/unit-tests/ModelsRelationsTest.php index 74ceff30579..13e3d3e4983 100644 --- a/unit-tests/ModelsRelationsTest.php +++ b/unit-tests/ModelsRelationsTest.php @@ -119,19 +119,34 @@ public function _executeTests($di) $robot = Robots::findFirst(); $this->assertNotEquals($robot, false); - /*$robotsParts = $robot->getRobotsParts(); + $robotsParts = $robot->getRobotsParts(); $this->assertEquals(get_class($robotsParts), 'Phalcon\Mvc\Model\Resultset\Simple'); - $this->assertEquals(count($robotsParts), 3);*/ + $this->assertEquals(count($robotsParts), 3); + /** Passing parameters to magic methods **/ $robotsParts = $robot->getRobotsParts("parts_id = 1"); $this->assertEquals(get_class($robotsParts), 'Phalcon\Mvc\Model\Resultset\Simple'); $this->assertEquals(count($robotsParts), 1); - $robotsParts = $robot->getRobotsParts(array("parts_id = :parts_id:", "bind" => array("parts_id" => 1))); + $robotsParts = $robot->getRobotsParts(array( + "parts_id > :parts_id:", + "bind" => array("parts_id" => 1) + )); $this->assertEquals(get_class($robotsParts), 'Phalcon\Mvc\Model\Resultset\Simple'); - $this->assertEquals(count($robotsParts), 1); + $this->assertEquals(count($robotsParts), 2); + $this->assertEquals($robotsParts->getFirst()->parts_id, 2); + + $robotsParts = $robot->getRobotsParts(array( + "parts_id > :parts_id:", + "bind" => array("parts_id" => 1), + "order" => "parts_id DESC" + )); + $this->assertEquals(get_class($robotsParts), 'Phalcon\Mvc\Model\Resultset\Simple'); + $this->assertEquals(count($robotsParts), 2); + $this->assertEquals($robotsParts->getFirst()->parts_id, 3); - /*$number = $robot->countRobotsParts(); + /** Magic counting */ + $number = $robot->countRobotsParts(); $this->assertEquals($number, 3); $part = Parts::findFirst(); @@ -153,12 +168,17 @@ public function _executeTests($di) $part = $robotPart->getParts(); $this->assertEquals(get_class($part), 'Parts'); + /** Relations in namespaced models */ $robot = Some\Robots::findFirst(); $this->assertNotEquals($robot, false); $robotsParts = $robot->getRobotsParts(); $this->assertEquals(get_class($robotsParts), 'Phalcon\Mvc\Model\Resultset\Simple'); - $this->assertEquals(count($robotsParts), 3);*/ + $this->assertEquals(count($robotsParts), 3); + + $robotsParts = $robot->getRobotsParts("parts_id = 1"); + $this->assertEquals(get_class($robotsParts), 'Phalcon\Mvc\Model\Resultset\Simple'); + $this->assertEquals(count($robotsParts), 1); } diff --git a/unit-tests/models/Some/Robots.php b/unit-tests/models/Some/Robots.php index 9fb2f51fee6..a747e8c679e 100644 --- a/unit-tests/models/Some/Robots.php +++ b/unit-tests/models/Some/Robots.php @@ -17,9 +17,9 @@ public function initialize() )); } - public function getRobotsParts() + public function getRobotsParts($arguments=null) { - return $this->getRelated('Some\RobotsParts'); + return $this->getRelated('Some\RobotsParts', $arguments); } } \ No newline at end of file