Skip to content

Commit

Permalink
Merge pull request #1604 from sjinks/issue-1506
Browse files Browse the repository at this point in the history
Backport #1506 from 1.2.5
  • Loading branch information
Phalcon committed Nov 27, 2013
2 parents 9388b10 + 1e53e53 commit c57fdd4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
18 changes: 18 additions & 0 deletions ext/mvc/model/row.c
Expand Up @@ -141,3 +141,21 @@ PHP_METHOD(Phalcon_Mvc_Model_Row, offsetUnset){
return;
}

/**
* Returns the instance as an array representation
*
* @return array
*/
PHP_METHOD(Phalcon_Mvc_Model_Row, toArray){

HashTable *properties;

properties = Z_OBJ_HT_P(this_ptr)->get_properties(this_ptr TSRMLS_CC);

if (!properties) {
RETURN_FALSE;
}

array_init_size(return_value, zend_hash_num_elements(properties));
zend_hash_copy(Z_ARRVAL_P(return_value), properties, (copy_ctor_func_t)zval_add_ref, NULL, sizeof(zval*));
}
4 changes: 3 additions & 1 deletion ext/mvc/model/row.h
Expand Up @@ -26,6 +26,7 @@ PHP_METHOD(Phalcon_Mvc_Model_Row, offsetExists);
PHP_METHOD(Phalcon_Mvc_Model_Row, offsetGet);
PHP_METHOD(Phalcon_Mvc_Model_Row, offsetSet);
PHP_METHOD(Phalcon_Mvc_Model_Row, offsetUnset);
PHP_METHOD(Phalcon_Mvc_Model_Row, toArray);

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_mvc_model_row_setdirtystate, 0, 0, 1)
ZEND_ARG_INFO(0, dirtyState)
Expand Down Expand Up @@ -53,7 +54,8 @@ PHALCON_INIT_FUNCS(phalcon_mvc_model_row_method_entry){
PHP_ME(Phalcon_Mvc_Model_Row, offsetExists, arginfo_phalcon_mvc_model_row_offsetexists, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Model_Row, offsetGet, arginfo_phalcon_mvc_model_row_offsetget, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Model_Row, offsetSet, arginfo_phalcon_mvc_model_row_offsetset, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Model_Row, offsetUnset, arginfo_phalcon_mvc_model_row_offsetunset, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Model_Row, offsetUnset, arginfo_phalcon_mvc_model_row_offsetunset, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Model_Row, toArray, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
};

11 changes: 10 additions & 1 deletion unit-tests/ModelsTest.php
Expand Up @@ -467,6 +467,16 @@ protected function _executeTestsNormal($di){
// Issue 1314
$parts = new Parts2();
$parts->save();

// Issue 1506
$persona = Personas::findFirst(array('columns' => 'nombres, telefono, estado', "nombres = 'LOST CREATE'"));
$expected = array(
'nombres' => 'LOST CREATE',
'telefono' => '1',
'estado' => 'A'
);

$this->assertEquals($expected, $persona->toArray());
}

protected function _executeTestsRenamed($di)
Expand Down Expand Up @@ -718,5 +728,4 @@ protected function _executeTestsRenamed($di)
$personer->refresh();
$this->assertEquals($personerData, $personer->toArray());
}

}

0 comments on commit c57fdd4

Please sign in to comment.