Selecting Only Specified Attributes In Relational Model

Hi guys.

First of all, before creating this post, i searched a lot at yii forum about similar problems, but I have not found anything like.

I have one relation MANY_TO_MANY, that the model CmlProdServicos has many CmlVlrProdServ, and it’s representing this way:

public function relations()


		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'CmlVlrProdServ' => array(self::HAS_MANY, 'CmlVlrProdServ', 'int_cod_prod_serv','alias'=>'valores'




So, i want select all attributes from CmlProdServicos(That already works) but i want to show only specified attributes from relational model CmlVlrProdServ, so i did:

$Criteria = new CDbCriteria();

Criteria->with = array("CmlVlrProdServ"=>array("select"=>"valores.num_vlr_total,valores.str_inf_descricao")); 


But didn’t work. So i created one scope:

public function scopes()


        return array(



                'order'=>'int_cod_vlr_prod_serv DESC',







What happened ? Showed correctly the attributes chosen to select, in this case: ‘valores.num_vlr_total’,'valores.str_inf_descricao, but still show the other attributes from CmlVlrProdServ with NULL values as follow:

//JSON code to facilitate the presentation of data



        "int_cod_prod_serv": 15818,

        "str_cod_referencia": "126874",

        "str_inf_sigla": null,

        "str_inf_descricao": "Pep Toe com Borda Dourada",

        "opt_tip_prod_serv": "P",

        "opt_tip_migrado": "A",

        "num_vlr_unitario": "109.9900",

        "txt_inf_especificacao": "Modelo=AND3133819",

        "dat_ref_registro": "2014-06-13",

        "str_ref_image": "126874.jpg",

        "opt_tip_ativo": "S",

        "CmlVlrProdServ": [


                "num_vlr_total": "219.99",

                "str_inf_descricao": "ou 3x 73.33",

                "int_cod_vlr_prod_serv": 1008,

                "int_cod_prod_serv": null,

                "int_seq_controle": null,

                "str_cod_referencia": null,

                "num_vlr_unitario": null,

                "num_vlr_parcelado": null,

                "txt_inf_detalhamento": null,

                "dat_ref_registro": null,

                "": null



        "": null



What i’m doing wrong ? What i need to do to show only fields relational chosen ?

Can anyone help me please ?

The generated data is correct, only the selected fields are retrieved from the database.

The Model still represents all the standard fields.

If you want to limit the shown fields, you have to filter them in your application code.

Thank you so much le_top for the explanation

About that "If you want to limit the shown fields, you have to filter them in your application code."

How can i do that without write sql code ?

Thanks again!

Many options are possible; supposing that $array is the content of "CmlVlrProdServ":

array_filter($array, create_function('$a','return in_array(array("num_vlr_total","num_vlr_total","str_inf_descricao","int_cod_vlr_prod_serv"), $a);'));

Where do i put this code in my app ?

Thanks again!