Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

criteria: fix issue #2157 and added tests #2220

Merged
merged 2 commits into from
Mar 31, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 64 additions & 48 deletions ext/mvc/model/criteria.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ PHP_METHOD(Phalcon_Mvc_Model_Criteria, columns){
*/
PHP_METHOD(Phalcon_Mvc_Model_Criteria, join){

zval *model, *conditions = NULL, *alias = NULL, *type = NULL, *join, *params;
zval *current_joins, *merged_joins = NULL;
zval *model, *conditions = NULL, *alias = NULL, *type = NULL, *new_join, *params;
zval *current_joins, *merged_joins = NULL, *new_join_array = NULL;

PHALCON_MM_GROW();

Expand All @@ -331,29 +331,33 @@ PHP_METHOD(Phalcon_Mvc_Model_Criteria, join){
type = PHALCON_GLOBAL(z_null);
}

PHALCON_INIT_VAR(join);
array_init_size(join, 4);
phalcon_array_append(&join, model, 0);
phalcon_array_append(&join, conditions, 0);
phalcon_array_append(&join, alias, 0);
phalcon_array_append(&join, type, 0);
PHALCON_INIT_VAR(new_join);
array_init_size(new_join, 4);
phalcon_array_append(&new_join, model, 0);
phalcon_array_append(&new_join, conditions, 0);
phalcon_array_append(&new_join, alias, 0);
phalcon_array_append(&new_join, type, 0);

PHALCON_OBS_VAR(params);
phalcon_read_property_this(&params, this_ptr, SL("_params"), PH_NOISY TSRMLS_CC);
if (phalcon_array_isset_string(params, SS("joins"))) {


PHALCON_INIT_VAR(new_join_array);
array_init_size(new_join_array, 1);
phalcon_array_append(&new_join_array, new_join, 0);

PHALCON_OBS_VAR(current_joins);
phalcon_array_fetch_string(&current_joins, params, SL("joins"), PH_NOISY);
if (Z_TYPE_P(current_joins) == IS_ARRAY) {
PHALCON_INIT_VAR(merged_joins);
phalcon_fast_array_merge(merged_joins, &current_joins, &join TSRMLS_CC);
phalcon_fast_array_merge(merged_joins, &current_joins, &new_join_array TSRMLS_CC);
} else {
PHALCON_CPY_WRT(merged_joins, join);
PHALCON_CPY_WRT(merged_joins, new_join_array);
}
} else {
PHALCON_INIT_NVAR(merged_joins);
array_init_size(merged_joins, 1);
phalcon_array_append(&merged_joins, join, 0);
phalcon_array_append(&merged_joins, new_join, 0);
}

phalcon_update_property_array_string(this_ptr, SL("_params"), SS("joins"), merged_joins TSRMLS_CC);
Expand All @@ -378,8 +382,8 @@ PHP_METHOD(Phalcon_Mvc_Model_Criteria, join){
*/
PHP_METHOD(Phalcon_Mvc_Model_Criteria, innerJoin){

zval *model, *conditions = NULL, *alias = NULL, *type, *join, *params;
zval *current_joins, *merged_joins = NULL;
zval *model, *conditions = NULL, *alias = NULL, *type, *new_join, *params;
zval *current_joins, *merged_joins = NULL, *new_join_array = NULL;

PHALCON_MM_GROW();

Expand All @@ -396,29 +400,33 @@ PHP_METHOD(Phalcon_Mvc_Model_Criteria, innerJoin){
PHALCON_INIT_VAR(type);
ZVAL_STRING(type, "INNER", 1);

PHALCON_INIT_VAR(join);
array_init_size(join, 4);
phalcon_array_append(&join, model, 0);
phalcon_array_append(&join, conditions, 0);
phalcon_array_append(&join, alias, 0);
phalcon_array_append(&join, type, 0);
PHALCON_INIT_VAR(new_join);
array_init_size(new_join, 4);
phalcon_array_append(&new_join, model, 0);
phalcon_array_append(&new_join, conditions, 0);
phalcon_array_append(&new_join, alias, 0);
phalcon_array_append(&new_join, type, 0);

PHALCON_OBS_VAR(params);
phalcon_read_property_this(&params, this_ptr, SL("_params"), PH_NOISY TSRMLS_CC);
if (phalcon_array_isset_string(params, SS("joins"))) {


PHALCON_INIT_VAR(new_join_array);
array_init_size(new_join_array, 1);
phalcon_array_append(&new_join_array, new_join, 0);

PHALCON_OBS_VAR(current_joins);
phalcon_array_fetch_string(&current_joins, params, SL("joins"), PH_NOISY);
if (Z_TYPE_P(current_joins) == IS_ARRAY) {
PHALCON_INIT_VAR(merged_joins);
phalcon_fast_array_merge(merged_joins, &current_joins, &join TSRMLS_CC);
phalcon_fast_array_merge(merged_joins, &current_joins, &new_join_array TSRMLS_CC);
} else {
PHALCON_CPY_WRT(merged_joins, join);
PHALCON_CPY_WRT(merged_joins, new_join_array);
}
} else {
PHALCON_INIT_NVAR(merged_joins);
array_init_size(merged_joins, 1);
phalcon_array_append(&merged_joins, join, PH_SEPARATE);
phalcon_array_append(&merged_joins, new_join, PH_SEPARATE);
}

phalcon_update_property_array_string(this_ptr, SL("_params"), SS("joins"), merged_joins TSRMLS_CC);
Expand All @@ -440,8 +448,8 @@ PHP_METHOD(Phalcon_Mvc_Model_Criteria, innerJoin){
*/
PHP_METHOD(Phalcon_Mvc_Model_Criteria, leftJoin){

zval *model, *conditions = NULL, *alias = NULL, *type, *join, *params;
zval *current_joins, *merged_joins = NULL;
zval *model, *conditions = NULL, *alias = NULL, *type, *new_join, *params;
zval *current_joins, *merged_joins = NULL, *new_join_array = NULL;

PHALCON_MM_GROW();

Expand All @@ -458,29 +466,33 @@ PHP_METHOD(Phalcon_Mvc_Model_Criteria, leftJoin){
PHALCON_INIT_VAR(type);
ZVAL_STRING(type, "LEFT", 1);

PHALCON_INIT_VAR(join);
array_init_size(join, 4);
phalcon_array_append(&join, model, 0);
phalcon_array_append(&join, conditions, 0);
phalcon_array_append(&join, alias, 0);
phalcon_array_append(&join, type, 0);
PHALCON_INIT_VAR(new_join);
array_init_size(new_join, 4);
phalcon_array_append(&new_join, model, 0);
phalcon_array_append(&new_join, conditions, 0);
phalcon_array_append(&new_join, alias, 0);
phalcon_array_append(&new_join, type, 0);

PHALCON_OBS_VAR(params);
phalcon_read_property_this(&params, this_ptr, SL("_params"), PH_NOISY TSRMLS_CC);
if (phalcon_array_isset_string(params, SS("joins"))) {


PHALCON_INIT_VAR(new_join_array);
array_init_size(new_join_array, 1);
phalcon_array_append(&new_join_array, new_join, 0);

PHALCON_OBS_VAR(current_joins);
phalcon_array_fetch_string(&current_joins, params, SL("joins"), PH_NOISY);
if (Z_TYPE_P(current_joins) == IS_ARRAY) {
PHALCON_INIT_VAR(merged_joins);
phalcon_fast_array_merge(merged_joins, &current_joins, &join TSRMLS_CC);
phalcon_fast_array_merge(merged_joins, &current_joins, &new_join_array TSRMLS_CC);
} else {
PHALCON_CPY_WRT(merged_joins, join);
PHALCON_CPY_WRT(merged_joins, new_join_array);
}
} else {
PHALCON_INIT_NVAR(merged_joins);
array_init_size(merged_joins, 1);
phalcon_array_append(&merged_joins, join, PH_SEPARATE);
phalcon_array_append(&merged_joins, new_join, PH_SEPARATE);
}

phalcon_update_property_array_string(this_ptr, SL("_params"), SS("joins"), merged_joins TSRMLS_CC);
Expand All @@ -502,8 +514,8 @@ PHP_METHOD(Phalcon_Mvc_Model_Criteria, leftJoin){
*/
PHP_METHOD(Phalcon_Mvc_Model_Criteria, rightJoin){

zval *model, *conditions = NULL, *alias = NULL, *type, *join, *params;
zval *current_joins, *merged_joins = NULL;
zval *model, *conditions = NULL, *alias = NULL, *type, *new_join, *params;
zval *current_joins, *merged_joins = NULL, *new_join_array = NULL;

PHALCON_MM_GROW();

Expand All @@ -520,29 +532,33 @@ PHP_METHOD(Phalcon_Mvc_Model_Criteria, rightJoin){
PHALCON_INIT_VAR(type);
ZVAL_STRING(type, "RIGHT", 1);

PHALCON_INIT_VAR(join);
array_init_size(join, 4);
phalcon_array_append(&join, model, 0);
phalcon_array_append(&join, conditions, 0);
phalcon_array_append(&join, alias, 0);
phalcon_array_append(&join, type, 0);
PHALCON_INIT_VAR(new_join);
array_init_size(new_join, 4);
phalcon_array_append(&new_join, model, 0);
phalcon_array_append(&new_join, conditions, 0);
phalcon_array_append(&new_join, alias, 0);
phalcon_array_append(&new_join, type, 0);

PHALCON_OBS_VAR(params);
phalcon_read_property_this(&params, this_ptr, SL("_params"), PH_NOISY TSRMLS_CC);
if (phalcon_array_isset_string(params, SS("joins"))) {


PHALCON_INIT_VAR(new_join_array);
array_init_size(new_join_array, 1);
phalcon_array_append(&new_join_array, new_join, 0);

PHALCON_OBS_VAR(current_joins);
phalcon_array_fetch_string(&current_joins, params, SL("joins"), PH_NOISY);
if (Z_TYPE_P(current_joins) == IS_ARRAY) {
PHALCON_INIT_VAR(merged_joins);
phalcon_fast_array_merge(merged_joins, &current_joins, &join TSRMLS_CC);
phalcon_fast_array_merge(merged_joins, &current_joins, &new_join_array TSRMLS_CC);
} else {
PHALCON_CPY_WRT(merged_joins, join);
PHALCON_CPY_WRT(merged_joins, new_join_array);
}
} else {
PHALCON_INIT_NVAR(merged_joins);
array_init_size(merged_joins, 1);
phalcon_array_append(&merged_joins, join, PH_SEPARATE);
phalcon_array_append(&merged_joins, new_join, PH_SEPARATE);
}

phalcon_update_property_array_string(this_ptr, SL("_params"), SS("joins"), merged_joins TSRMLS_CC);
Expand Down
64 changes: 60 additions & 4 deletions unit-tests/ModelsCriteriaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function testModelsMysql()
$this->_executeTestsRenamed($di);
$this->_executeTestsFromInput($di);
$this->_executeTestIssues2131($di);
$this->_executeJoinTests($di, "mysql");
}

public function testModelsPostgresql()
Expand All @@ -96,6 +97,7 @@ public function testModelsPostgresql()
$this->_executeTestsRenamed($di);
$this->_executeTestsFromInput($di);
$this->_executeTestIssues2131($di);
$this->_executeJoinTests($di, "postgresql");
}

public function testModelsSQLite()
Expand All @@ -117,11 +119,12 @@ public function testModelsSQLite()
$this->_executeTestsRenamed($di);
$this->_executeTestsFromInput($di);
$this->_executeTestIssues2131($di);
$this->_executeJoinTests($di, "sqlite");
}

protected function _executeTestsNormal($di)
{

//Where
$personas = Personas::query()->where("estado='I'")->execute();
$people = People::find("estado='I'");
$this->assertEquals(count($personas), count($people));
Expand All @@ -144,7 +147,7 @@ protected function _executeTestsNormal($di)
$somePeople = $people->getFirst();
$this->assertEquals($somePersona->cedula, $somePeople->cedula);

//Order + limit
//Where + Order + limit
$personas = Personas::query()
->where("estado='A'")
->orderBy("nombres")
Expand All @@ -161,7 +164,7 @@ protected function _executeTestsNormal($di)
$somePeople = $people->getFirst();
$this->assertEquals($somePersona->cedula, $somePeople->cedula);

//Bind params + Limit
//Where with bind params + order + Limit
$personas = Personas::query()
->where("estado=?1")
->bind(array(1 => "A"))
Expand All @@ -181,7 +184,7 @@ protected function _executeTestsNormal($di)
$somePeople = $people->getFirst();
$this->assertEquals($somePersona->cedula, $somePeople->cedula);

//Limit + Offset
//Where with bind params + order + limit + Offset
$personas = Personas::query()
->where("estado=?1")
->bind(array(1 => "A"))
Expand All @@ -201,6 +204,7 @@ protected function _executeTestsNormal($di)
$somePeople = $people->getFirst();
$this->assertEquals($somePersona->cedula, $somePeople->cedula);

//Where with named bind params + order + limit
$personas = Personas::query()
->where("estado=:estado:")
->bind(array("estado" => "A"))
Expand All @@ -219,7 +223,59 @@ protected function _executeTestsNormal($di)
$somePersona = $personas->getFirst();
$somePeople = $people->getFirst();
$this->assertEquals($somePersona->cedula, $somePeople->cedula);
}

protected function _executeJoinTests($di, $dbtype)
{
//Left join with Simple resultset
$robotparts = RobotsParts::query()
->columns("Robots.id, Robots.name, RobotsParts.id robotpart_id")
->leftJoin("Robots", "Robots.id = RobotsParts.robots_id")
->execute();
$this->assertTrue(is_object($robotparts));
$this->assertInstanceOf('Phalcon\Mvc\Model\Resultset\Simple', $robotparts);
$this->assertNotNull($robotparts->getFirst()->id);
$this->assertNotNull($robotparts->getFirst()->name);
$this->assertNotNull($robotparts->getFirst()->robotpart_id);

//Two left joins with Simple resultset
$robotparts = RobotsParts::query()
->columns("RobotsParts.id, r.id robot_id, p.id part_id")
->leftJoin("Robots", "r.id = RobotsParts.robots_id", "r")
->leftJoin("Parts", "p.id = RobotsParts.parts_id", "p")
->execute();
$this->assertTrue(is_object($robotparts));
$this->assertInstanceOf('Phalcon\Mvc\Model\Resultset\Simple', $robotparts);
$this->assertNotNull($robotparts->getFirst()->id);
$this->assertNotNull($robotparts->getFirst()->robot_id);
$this->assertNotNull($robotparts->getFirst()->part_id);

//Right join not supported in sqlite
if ($dbtype != "sqlite")
{
//Right join with Simple resultset
$robotparts = RobotsParts::query()
->columns("Robots.id, Robots.name, RobotsParts.id robotpart_id")
->rightJoin("Robots", "Robots.id = RobotsParts.robots_id")
->execute();
$this->assertTrue(is_object($robotparts));
$this->assertInstanceOf('Phalcon\Mvc\Model\Resultset\Simple', $robotparts);
$this->assertNotNull($robotparts->getFirst()->id);
$this->assertNotNull($robotparts->getFirst()->name);
$this->assertNotNull($robotparts->getFirst()->robotpart_id);

//Two right joins with Simple resultset
$robotparts = RobotsParts::query()
->columns("RobotsParts.id, r.id robot_id, p.id part_id")
->rightJoin("Robots", "r.id = RobotsParts.robots_id", "r")
->rightJoin("Parts", "p.id = RobotsParts.parts_id", "p")
->execute();
$this->assertTrue(is_object($robotparts));
$this->assertInstanceOf('Phalcon\Mvc\Model\Resultset\Simple', $robotparts);
$this->assertNotNull($robotparts->getFirst()->id);
$this->assertNotNull($robotparts->getFirst()->robot_id);
$this->assertNotNull($robotparts->getFirst()->part_id);
}
}

protected function _executeTestsRenamed($di)
Expand Down