Unexpected fields in SQL results

Hi there,

I’ve faced a problem while executing an query using models.

The problem is query returning all fields in model/table instead I’ve selected.

I’ve selected two fields [ id, CONCAT(first_name, ’ ', middle_name) FMName ]

but result returning all fields in table and fields are without values only id field has value. Fields are

I printed these fields by print_r($Results[0]->attributes)




Array (

	[id] => 1

	[first_name] =>

	[middle_name] =>

	[last_name] =>

	[alt_email_id] =>

	[mobile] =>

	[contact_address_line_1] =>

	[contact_address_line_2] =>

	[city] =>

	[joining_date] =>

	[email_verified] =>

	[active] =>

	[deleted] =>

)



Please see here




$Results = UserProfiles::model()

                	->published()

                	->findAll(array(

                                	'select' => "id, CONCAT(first_name, ' ', middle_name) FMName",

                                	'order' => 'FMName ASC', 'group' => 'FMName'

                    	));



Above published() is a scope defined in UserProfiles as




public function scopes() {

	return array(

    	...

    	'published' => array('condition' => "active='1' AND deleted='0'",),

    	...

	);

}



And there is also a defaultScope as




public function defaultScope() {

	return array(

    	'condition' => "active='1' AND deleted='0'",

    	'order' => "last_name ASC",

	);

}



But, Interesting is Query I found exact what I’m trying to execute. I found query in web-log




SELECT id, CONCAT(first_name, ' ', middle_name) fmname FROM `tbl_user_profiles` `t`

WHERE active='1' AND deleted='0' GROUP BY fmname ORDER BY fmname, last_name ASC



I executed this query in MySQLand results is actual as need.

Anybody help me about what’s wrong with above scripts or statements

Why You not just create method in model, for example:

public function getFullFirstName() {

return $this->first_name . ' ' . $this->middle_name;

}

In that case You will not need to CONCAT in mysql query and everything will work perfect…

p.s. Thanks for Yii, this kind of function you can access like rest of attribute:

$model->fullFirstName;

Just take care when you make name of function, because if you have real attribute with same name it will be first selected…

Yes, I already did that, but I want results in group by.

[size="5"]Problem is solved.[/size]

Thank you :)

Sorry, I miss that part…