Skip to content

Commit

Permalink
Implement phalcon#1903
Browse files Browse the repository at this point in the history
  • Loading branch information
sjinks committed Feb 25, 2014
1 parent caa95ca commit 3e4ae3a
Showing 1 changed file with 43 additions and 28 deletions.
71 changes: 43 additions & 28 deletions ext/mvc/model/criteria.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "kernel/hash.h"

#include "interned-strings.h"
#include "../modelinterface.h"

/**
* Phalcon\Mvc\Model\Criteria
Expand Down Expand Up @@ -1316,8 +1317,9 @@ PHP_METHOD(Phalcon_Mvc_Model_Criteria, fromInput){

zval *dependency_injector, *model_name, *data;
zval *conditions, *service, *meta_data = NULL, *model;
zval *data_types = NULL, *bind, *value = NULL, *field = NULL, *type = NULL, *condition = NULL;
zval *data_types = NULL, *bind, *value = NULL, *field = NULL, *type, *condition = NULL;
zval *value_pattern = NULL, *join_conditions;
zval *column_map = NULL;
HashTable *ah0;
HashPosition hp0;
zval **hd;
Expand All @@ -1331,6 +1333,7 @@ PHP_METHOD(Phalcon_Mvc_Model_Criteria, fromInput){
PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "Input data must be an Array");
return;
}

if (Z_TYPE_P(dependency_injector) != IS_OBJECT) {
PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_model_exception_ce, "A dependency injector container is required to obtain the ORM services");
return;
Expand All @@ -1352,7 +1355,19 @@ PHP_METHOD(Phalcon_Mvc_Model_Criteria, fromInput){
if (phalcon_has_constructor(model TSRMLS_CC)) {
PHALCON_CALL_METHOD(NULL, model, "__construct");
}


PHALCON_VERIFY_INTERFACE_EX(model, phalcon_mvc_modelinterface_ce, phalcon_mvc_model_exception_ce, 1);

if (PHALCON_GLOBAL(orm).column_renaming) {
PHALCON_CALL_METHOD(&column_map, meta_data, "getcolumnmap", model);
if (Z_TYPE_P(column_map) != IS_ARRAY) {
PHALCON_INIT_NVAR(column_map);
}
}
else {
column_map = PHALCON_GLOBAL(z_null);
}

PHALCON_CALL_METHOD(&data_types, meta_data, "getdatatypes", model);

PHALCON_INIT_VAR(bind);
Expand All @@ -1367,37 +1382,37 @@ PHP_METHOD(Phalcon_Mvc_Model_Criteria, fromInput){
phalcon_is_iterable(data, &ah0, &hp0, 0, 0);

while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
zval *real_field;

PHALCON_GET_HKEY(field, ah0, hp0);
PHALCON_GET_HVALUE(value);

if (Z_TYPE_P(column_map) != IS_ARRAY || !phalcon_array_isset_fetch(&real_field, column_map, field)) {
real_field = field;
}

if (phalcon_array_isset(data_types, field)) {
if (Z_TYPE_P(value) != IS_NULL) {
if (!PHALCON_IS_STRING(value, "")) {

PHALCON_OBS_NVAR(type);
phalcon_array_fetch(&type, data_types, field, PH_NOISY);
if (PHALCON_IS_LONG(type, 2)) {
/**
* For varchar types we use LIKE operator
*/
PHALCON_INIT_NVAR(condition);
PHALCON_CONCAT_VSVS(condition, field, " LIKE :", field, ":");

PHALCON_INIT_NVAR(value_pattern);
PHALCON_CONCAT_SVS(value_pattern, "%", value, "%");
phalcon_array_update_zval(&bind, field, value_pattern, PH_COPY);
} else {
/**
* For the rest of data types we use a plain = operator
*/
PHALCON_INIT_NVAR(condition);
PHALCON_CONCAT_VSVS(condition, field, "=:", field, ":");
phalcon_array_update_zval(&bind, field, value, PH_COPY);
}

phalcon_array_append(&conditions, condition, 0);
if (phalcon_array_isset_fetch(&type, data_types, real_field)) {
if (Z_TYPE_P(value) != IS_NULL && !PHALCON_IS_STRING(value, "")) {
if (PHALCON_IS_LONG(type, 2)) {
/**
* For varchar types we use LIKE operator
*/
PHALCON_INIT_NVAR(condition);
PHALCON_CONCAT_VSVS(condition, field, " LIKE :", field, ":");

PHALCON_INIT_NVAR(value_pattern);
PHALCON_CONCAT_SVS(value_pattern, "%", value, "%");
phalcon_array_update_zval(&bind, field, value_pattern, PH_COPY);
} else {
/**
* For the rest of data types we use a plain = operator
*/
PHALCON_INIT_NVAR(condition);
PHALCON_CONCAT_VSVS(condition, field, "=:", field, ":");
phalcon_array_update_zval(&bind, field, value, PH_COPY);
}

phalcon_array_append(&conditions, condition, 0);
}
}

Expand Down

0 comments on commit 3e4ae3a

Please sign in to comment.