Skip to content

Commit

Permalink
Merge pull request #1733 from dreamsxin/nfr_1701
Browse files Browse the repository at this point in the history
[NFR]Add param for Model::toArray
  • Loading branch information
Phalcon committed Dec 23, 2013
2 parents 2014df0 + da68ade commit 39de9d2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
15 changes: 12 additions & 3 deletions ext/mvc/model.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/*
+------------------------------------------------------------------------+
| Phalcon Framework |
Expand Down Expand Up @@ -6538,11 +6537,12 @@ PHP_METHOD(Phalcon_Mvc_Model, dump){
* print_r($robot->toArray());
*</code>
*
* @param array $columns
* @return array
*/
PHP_METHOD(Phalcon_Mvc_Model, toArray){

zval *meta_data, *data, *null_value, *attributes;
zval *columns = NULL, *meta_data, *data, *null_value, *attributes;
zval *column_map, *attribute = NULL, *exception_message = NULL;
zval *attribute_field = NULL, *value = NULL;
HashTable *ah0;
Expand All @@ -6551,6 +6551,8 @@ PHP_METHOD(Phalcon_Mvc_Model, toArray){

PHALCON_MM_GROW();

phalcon_fetch_params(1, 0, 1, &columns);

PHALCON_INIT_VAR(meta_data);
phalcon_call_method(meta_data, this_ptr, "getmodelsmetadata");

Expand All @@ -6576,7 +6578,14 @@ PHP_METHOD(Phalcon_Mvc_Model, toArray){
while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {

PHALCON_GET_HVALUE(attribute);


if (columns && Z_TYPE_P(columns) == IS_ARRAY) {
if (!phalcon_fast_in_array(attribute, columns TSRMLS_CC)) {
zend_hash_move_forward_ex(ah0, &hp0);
continue;
}
}

/**
* Check if the columns must be renamed
*/
Expand Down
6 changes: 5 additions & 1 deletion ext/mvc/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_mvc_model_setup, 0, 0, 1)
ZEND_ARG_INFO(0, options)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_mvc_model_toarray, 0, 0, 0)
ZEND_ARG_INFO(0, columns)
ZEND_END_ARG_INFO()

PHALCON_INIT_FUNCS(phalcon_mvc_model_method_entry){
PHP_ME(Phalcon_Mvc_Model, __construct, arginfo_phalcon_mvc_model___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL|ZEND_ACC_CTOR)
PHP_ME(Phalcon_Mvc_Model, setDI, arginfo_phalcon_mvc_model_setdi, ZEND_ACC_PUBLIC)
Expand Down Expand Up @@ -370,7 +374,7 @@ PHALCON_INIT_FUNCS(phalcon_mvc_model_method_entry){
PHP_ME(Phalcon_Mvc_Model, serialize, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Model, unserialize, arginfo_phalcon_mvc_model_unserialize, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Model, dump, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Model, toArray, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Model, toArray, arginfo_phalcon_mvc_model_toarray, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Model, setup, arginfo_phalcon_mvc_model_setup, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_FE_END
};
Expand Down
2 changes: 1 addition & 1 deletion ext/mvc/view.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ PHP_METHOD(Phalcon_Mvc_View, setTemplateAfter){
}

/**
* Resets any template before layouts
* Resets any template after layouts
*
* @return Phalcon\Mvc\View
*/
Expand Down
9 changes: 9 additions & 0 deletions unit-tests/ModelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,15 @@ protected function _executeTestsNormal($di){

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

// Issue 1701
$expected = array(
'nombres' => 'LOST CREATE',
'cupo' => 21000,
'estado' => 'A',
);

$this->assertEquals($persona->toArray(array('nombres', 'cupo', 'estado')), $expected);

//Refresh
$persona = Personas::findFirst();

Expand Down

0 comments on commit 39de9d2

Please sign in to comment.