Skip to content

Commit

Permalink
Merge pull request #2325 from dreamsxin/2319
Browse files Browse the repository at this point in the history
Fix #2319 In `Model::_postSaveRelatedRecords` add check whether value is object
  • Loading branch information
Phalcon committed Apr 12, 2014
2 parents 3b58151 + ef335fe commit c8bb0f9
Showing 1 changed file with 55 additions and 4 deletions.
59 changes: 55 additions & 4 deletions ext/mvc/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -6386,6 +6386,12 @@ PHP_METHOD(Phalcon_Mvc_Model, __callStatic){
PHP_METHOD(Phalcon_Mvc_Model, __set){

zval *property, *value, *lower_property = NULL;
zval *related, *key = NULL, *lower_key = NULL, *item = NULL, *model_name, *manager = NULL;
zval *relation = NULL, *new_instance, *referenced_model_name = NULL, *referenced_model = NULL;
HashTable *ah0;
HashPosition hp0;
zval **hd;
int i = 0;

PHALCON_MM_GROW();

Expand All @@ -6409,11 +6415,56 @@ PHP_METHOD(Phalcon_Mvc_Model, __set){
/**
* Check if the value is an array
*/
if (Z_TYPE_P(value) == IS_ARRAY) {
PHALCON_INIT_NVAR(lower_property);
if (Z_TYPE_P(value) == IS_ARRAY) {
PHALCON_INIT_VAR(related);
array_init(related);

PHALCON_INIT_VAR(lower_property);
phalcon_fast_strtolower(lower_property, property);
phalcon_update_property_array(this_ptr, SL("_related"), lower_property, value TSRMLS_CC);
phalcon_update_property_long(this_ptr, SL("_dirtyState"), 1 TSRMLS_CC);

PHALCON_INIT_VAR(model_name);
phalcon_get_class(model_name, this_ptr, 0 TSRMLS_CC);

PHALCON_CALL_METHOD(&manager, this_ptr, "getmodelsmanager");

phalcon_is_iterable(value, &ah0, &hp0, 0, 0);

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

PHALCON_GET_HKEY(key, ah0, hp0);
PHALCON_GET_HVALUE(item);

if (Z_TYPE_P(item) == IS_OBJECT) {
if (instanceof_function_ex(Z_OBJCE_P(item), phalcon_mvc_modelinterface_ce, 1 TSRMLS_CC)) {
i++;
phalcon_array_append(&related, item, 0);
}
} else {
PHALCON_INIT_NVAR(lower_key);
phalcon_fast_strtolower(lower_key, key);

phalcon_update_property_zval_zval(this_ptr, lower_key, item TSRMLS_CC);

PHALCON_CALL_METHOD(&relation, manager, "getrelationbyalias", model_name, lower_property);
if (Z_TYPE_P(relation) == IS_OBJECT) {
PHALCON_INIT_VAR(new_instance);
ZVAL_FALSE(new_instance);

PHALCON_CALL_METHOD(&referenced_model_name, relation, "getreferencedmodel");
PHALCON_CALL_METHOD(&referenced_model, manager, "load", referenced_model_name, new_instance);
PHALCON_CALL_METHOD(NULL, referenced_model, "writeattribute", lower_key, item);
}
}

zend_hash_move_forward_ex(ah0, &hp0);
}

if (i > 0) {
phalcon_update_property_zval_zval(this_ptr, lower_property, related TSRMLS_CC);
phalcon_update_property_array(this_ptr, SL("_related"), lower_property, related TSRMLS_CC);
phalcon_update_property_long(this_ptr, SL("_dirtyState"), 1 TSRMLS_CC);
}

RETURN_CTOR(value);
}

Expand Down

0 comments on commit c8bb0f9

Please sign in to comment.