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

Fix #2319 In Model::_postSaveRelatedRecords add check whether value is object #2325

Merged
merged 3 commits into from
Apr 12, 2014
Merged
Changes from 1 commit
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
Next Next commit
Fix #2319 In Model::__set check whether value is object
  • Loading branch information
dreamsxin committed Apr 11, 2014
commit 870e994c3354faccb878ac1a72add5a36b07ae8a
50 changes: 46 additions & 4 deletions ext/mvc/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -6313,6 +6313,11 @@ PHP_METHOD(Phalcon_Mvc_Model, __callStatic){
PHP_METHOD(Phalcon_Mvc_Model, __set){

zval *property, *value, *lower_property = NULL;
zval *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;

PHALCON_MM_GROW();

Expand All @@ -6336,11 +6341,48 @@ 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(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);

PHALCON_INIT_NVAR(lower_key);
phalcon_fast_strtolower(lower_key, key);

if (Z_TYPE_P(item) == IS_OBJECT) {
if (instanceof_function_ex(Z_OBJCE_P(item), phalcon_mvc_modelinterface_ce, 1 TSRMLS_CC)) {
phalcon_update_property_array(this_ptr, SL("_related"), lower_property, item TSRMLS_CC);
phalcon_update_property_long(this_ptr, SL("_dirtyState"), 1 TSRMLS_CC);
}
} else {
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);
}


RETURN_CTOR(value);
}

Expand Down