Public function convertModelToArray ($ models, array $ filterAttributes = null ){
If (is_array ($ models ))
$ ArrayMode = TRUE;
Else {
$ Models = array ($ models );
$ ArrayMode = FALSE;
}
$ Result = array ();
Foreach ($ models as $ model ){
$ Attributes = $ model-> getAttributes ();
If (isset ($ filterAttributes) & is_array ($ filterAttributes )){
Foreach ($ filterAttributes as $ key => $ value ){
If (strtolower ($ key) = strtolower ($ model-> tableName () & strpos ($ value, '*') = FALSE ){
$ Value = str_replace ('','', $ value );
$ ArrColumn = explode (",", $ value );
Foreach ($ attributes as $ key => $ value)
If (! In_array ($ key, $ arrColumn ))
Unset ($ attributes [$ key]);
}
}
}
$ Relations = array ();
Foreach ($ model-> relations () as $ key => $ related ){
If ($ model-> hasRelated ($ key )){
If ($ model-> $ key instanceof CModel) | is_array ($ model-> $ key )){
$ Relations [$ key] = self: convertModelToArray ($ model-> $ key, $ filterAttributes );
} Else {
$ Relations [$ key] = $ model-> $ key;
}
}
}
$ All = array_merge ($ attributes, $ relations );
If ($ arrayMode)
Array_push ($ result, $ all );
Else
$ Result = $ all;
}
Return $ result;
}
$ This-> _ sendResponse (array ('errorcode' => int, 'errormessage' => '', 'data' => array ());
Public function _ sendResponse ($ attr = array (), $ ct = self: CONTENT_TYPE_JSON ){
If ($ ct === self: CONTENT_TYPE_JSON ){
Header ("Content-type: application/json ");
} Else {
Header ("Content-type: text/plain ");
}
$ Response = array ();
$ Response ['errorcode'] = isset ($ attr ['errorcode'])? $ Attr ['errorcode']: ApiError: ERROR_ OK;
$ Response ['errormessage'] = empty ($ attr ['errormessage'])? ApiError: getErrorMessage ($ response ['errorcode']): $ attr ['errormessage'];
$ Response ['data'] = empty ($ attr ['data'])? Null: $ attr ['data'];
Echo CJSON: encode ($ response );
Yii: app ()-> end ();
}