Skip to content

Commit

Permalink
Merge pull request phalcon#449 from dreamsxin/model_beforequery
Browse files Browse the repository at this point in the history
Model beforequery
  • Loading branch information
dreamsxin committed Nov 5, 2015
2 parents a58834b + 0ac2dab commit 083322a
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 12 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ language:
- c

php:
- 5.3
- 5.4
- 5.5
- 5.6

services:
Expand Down
6 changes: 5 additions & 1 deletion ext/kernel/operators.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,11 @@ int phalcon_add_function_ex(zval *result, zval *op1, zval *op2 TSRMLS_DC){
int status;
int ref_count = Z_REFCOUNT_P(result);
int is_ref = Z_ISREF_P(result);
status = add_function(result, op1, op2 TSRMLS_CC);
#if PHP_VERSION_ID < 50400
status = add_function(result, op1, op2 TSRMLS_CC);
#else
status = fast_add_function(result, op1, op2 TSRMLS_CC);
#endif
Z_SET_REFCOUNT_P(result, ref_count);
Z_SET_ISREF_TO_P(result, is_ref);
return status;
Expand Down
5 changes: 3 additions & 2 deletions ext/kernel/operators.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,15 @@
#endif

void phalcon_make_printable_zval(zval *expr, zval *expr_copy, int *use_copy);

#define phalcon_add_function(result, left, right) phalcon_add_function_ex(result, left, right TSRMLS_CC)

#if PHP_VERSION_ID < 50400
#define phalcon_sub_function(result, left, right) sub_function(result, left, right TSRMLS_CC)
#define phalcon_add_function(result, left, right) add_function(result, left, right TSRMLS_CC)
#define phalcon_div_function(result, left, right) div_function(result, left, right TSRMLS_CC)
#define phalcon_mul_function(result, left, right) mul_function(result, left, right TSRMLS_CC)
#else
#define phalcon_sub_function(result, left, right) fast_sub_function(result, left, right TSRMLS_CC)
#define phalcon_add_function(result, left, right) fast_add_function(result, left, right TSRMLS_CC)
#define phalcon_div_function(result, left, right) fast_div_function(result, left, right TSRMLS_CC)
#define phalcon_mul_function(result, left, right) fast_mul_function(result, left, right TSRMLS_CC)
#endif
Expand Down
2 changes: 2 additions & 0 deletions ext/mvc/collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1873,6 +1873,8 @@ PHP_METHOD(Phalcon_Mvc_Collection, save){
PHALCON_INIT_NVAR(status);

if (PHALCON_IS_FALSE(mode)){
PHALCON_CALL_SELF(&id, "getidstring");
phalcon_array_update_zval(&data, attribute, value, PH_COPY);
ZVAL_STRING(&func, "save", 0);
} else {
ZVAL_STRING(&func, "insert", 0);
Expand Down
38 changes: 35 additions & 3 deletions ext/mvc/model/query/builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ PHP_METHOD(Phalcon_Mvc_Model_Query_Builder, getGroupBy){
PHP_METHOD(Phalcon_Mvc_Model_Query_Builder, getPhql){

zval *dependency_injector = NULL, *models, *conditions = NULL, *distinct;
zval *model = NULL, *phql, *columns;
zval *model = NULL, *model_instance, *phql, *columns;
zval *selected_columns = NULL, *column = NULL, *column_alias = NULL;
zval *aliased_column = NULL, *joined_columns = NULL, *model_column_alias = NULL;
zval *selected_column = NULL, *selected_models, *model_alias = NULL;
Expand All @@ -1437,9 +1437,10 @@ PHP_METHOD(Phalcon_Mvc_Model_Query_Builder, getPhql){
zval *join_type = NULL, *group, *group_items, *group_item = NULL;
zval *escaped_item = NULL, *joined_items = NULL, *having, *order;
zval *order_items, *order_item = NULL, *limit, *number, *for_update;
HashTable *ah0, *ah1, *ah2, *ah3, *ah4, *ah5;
HashPosition hp0, hp1, hp2, hp3, hp4, hp5;
HashTable *ah, *ah0, *ah1, *ah2, *ah3, *ah4, *ah5;
HashPosition hp, hp0, hp1, hp2, hp3, hp4, hp5;
zval **hd;
zend_class_entry *ce0;

PHALCON_MM_GROW();

Expand All @@ -1461,6 +1462,37 @@ PHP_METHOD(Phalcon_Mvc_Model_Query_Builder, getPhql){
return;
}

if (Z_TYPE_P(models) == IS_ARRAY) {
phalcon_is_iterable(models, &ah, &hp, 0, 0);
while (zend_hash_get_current_data_ex(ah, (void**) &hd, &hp) == SUCCESS) {
PHALCON_GET_HVALUE(model);
ce0 = phalcon_fetch_class(model TSRMLS_CC);

if (phalcon_method_exists_ce_ex(ce0, SS("beforequery") TSRMLS_CC) == SUCCESS) {
PHALCON_INIT_NVAR(model_instance);
object_init_ex(model_instance, ce0);
if (phalcon_has_constructor(model_instance TSRMLS_CC)) {
PHALCON_CALL_METHOD(NULL, model_instance, "__construct", dependency_injector);
}

PHALCON_CALL_METHOD(NULL, model_instance, "beforequery", this_ptr);
}

zend_hash_move_forward_ex(ah, &hp);
}
} else {
ce0 = phalcon_fetch_class(models TSRMLS_CC);
if (phalcon_method_exists_ce_ex(ce0, SS("beforequery") TSRMLS_CC) == SUCCESS) {
PHALCON_INIT_VAR(model_instance);
object_init_ex(model_instance, ce0);
if (phalcon_has_constructor(model_instance TSRMLS_CC)) {
PHALCON_CALL_METHOD(NULL, model_instance, "__construct", dependency_injector);
}

PHALCON_CALL_METHOD(NULL, model_instance, "beforequery", this_ptr);
}
}

PHALCON_CALL_SELF(&conditions, "getConditions");

PHALCON_INIT_VAR(phql);
Expand Down
2 changes: 1 addition & 1 deletion ext/mvc/model/transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ PHP_METHOD(Phalcon_Mvc_Model_Transaction, rollback){
RETURN_MM();
}

PHALCON_MM_RESTORE();
RETURN_CCTOR(success);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion ext/tag/select.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ PHALCON_INIT_CLASS(Phalcon_Tag_Select){
*/
PHP_METHOD(Phalcon_Tag_Select, selectField){

zval *parameters, *data = NULL, *params = NULL, *id = NULL, *name, *value = NULL;
zval *parameters, *data = NULL, *params = NULL, *default_params, *id = NULL, *name, *value = NULL;
zval *use_empty = NULL, *empty_value = NULL, *empty_text = NULL, *code;
zval *close_option, *options = NULL, *using = NULL;
zval *resultset_options = NULL, *array_options = NULL;
Expand All @@ -97,6 +97,11 @@ PHP_METHOD(Phalcon_Tag_Select, selectField){
PHALCON_CPY_WRT(params, parameters);
}

default_params = phalcon_fetch_static_property_ce(phalcon_tag_ce, SL("_defaultParams") TSRMLS_CC);
if (Z_TYPE_P(default_params) == IS_ARRAY) {
phalcon_array_merge_recursive_n2(&params, default_params);
}

if (!phalcon_array_isset_long(params, 0)) {
PHALCON_OBS_VAR(id);
phalcon_array_fetch_string(&id, params, SL("id"), PH_NOISY);
Expand Down
2 changes: 1 addition & 1 deletion unit-tests/ModelsDynamicOperationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function testModelsMysql()
$connection = new Phalcon\Db\Adapter\Pdo\Mysql($configMysql);

$eventsManager->attach('db', function($event, $connection) use (&$tracer) {
if ($event->getType() == 'beforeQuery') {
if ($event->getType() == 'beforeQuery' || $event->getType() == 'beforeExecute') {
$tracer[] = $connection->getSqlStatement();
}
});
Expand Down

0 comments on commit 083322a

Please sign in to comment.