Skip to content

Commit

Permalink
Fixing bug passing an array to magic methods in Phalcon\Mvc\Model (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
phalcon committed Oct 16, 2012
1 parent 4e043e1 commit 1e370a7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 20 deletions.
11 changes: 3 additions & 8 deletions ext/mvc/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
}
Expand Down
22 changes: 18 additions & 4 deletions ext/mvc/model/manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -740,13 +740,17 @@ 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) {
eval_int = phalcon_array_isset_string(parameters, SS("bind"));
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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
32 changes: 26 additions & 6 deletions unit-tests/ModelsRelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);

}

Expand Down
4 changes: 2 additions & 2 deletions unit-tests/models/Some/Robots.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

0 comments on commit 1e370a7

Please sign in to comment.