Skip to content

Commit

Permalink
criteria: fix issue phalcon#2157 (multiple joins)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxgalbu committed Mar 22, 2014
1 parent bbbfd7a commit 7b95517
Showing 1 changed file with 64 additions and 48 deletions.
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

0 comments on commit 7b95517

Please sign in to comment.