how to get clean array result from relational query

Hi guys,

I am using relatinal query like such:


$folders=User::model()->findByPk($id)->folders;

the relation is defined in User model as:


'has_folder'=>array(self::HAS_MANY,'UserFolder','user_id'),

'folders'=>array(self::HAS_MANY,'Folder','folder_id','through'=>'has_folder'),

the query works but returns a lot of extra data in the array.


Array

(

    [0] => Folder Object

        (

            [_md:CActiveRecord:private] => CActiveRecordMetaData Object

                (

                    [tableSchema] => CMysqlTableSchema Object

                        (

                            [schemaName] => 

                            [name] => folder

                            [rawName] => `folder`

                            [primaryKey] => id

                            [sequenceName] => 

                            [foreignKeys] => Array

                                (

                                )


                            [columns] => Array

                                (

                                    [id] => CMysqlColumnSchema Object

                                        (

                                            [name] => id

                                            [rawName] => `id`

                                            [allowNull] => 

                                            [dbType] => bigint(20)

                                            [type] => string

                                            [defaultValue] => 

                                            [size] => 20

                                            [precision] => 20

                                            [scale] => 

                                            [isPrimaryKey] => 1

                                            [isForeignKey] => 

                                            [autoIncrement] => 1

                                            [_e:CComponent:private] => 

                                            [_m:CComponent:private] => 

I want to filter out only clean folder data corresonping to the table column in a array. any pointer appreciated.

it’s normal to have such kind of result and you don’t need to change anything.

you can just use the result:


foreach ($folders as $folder)

    echo $folder->id;

do you really need that complicate relation? I think you could just use:


'folders'=>array(self::HAS_MANY,'Folder','user_id'),

You can just add specialized method:




class User ...


public function getCleanFolders() {

  $folders = array()

  foreach($this->folders as $folder)

    $folders[] = $folder->attributes;

  return $folders;

}



Hi, thanks for reply, it seems not working…I got an exception: Invalid argument supplied for foreach()…

beside, the folder does not have a foregin key user_id, User and folder is many-to-many. I guess I cant use the relation you propose.

this is cool…it works very well…thanks a lot

oh… so if relation is many to many it should be something like


'folders'=>array(self::MANY_MANY,'Folder','table_user_folder(user_id, folder_id)'),

of course you have to change ‘table_user_folder’ with the right one.

Hi Attilio,

I actually overlooked that relation. thanks for a reminder. but do you know what is different between what I used and what you proposed?

what I used is:


'has_folder'=>array(self::HAS_MANY,'UserFolder','user_id'),

'folders'=>array(self::HAS_MANY,'Folder','folder_id','through'=>'has_folder'),

and you propose:


'folders'=>array(self::MANY_MANY,'Folder','table_user_folder(user_id, folder_id)'),

any performance difference, if you may know?

I guess mine is a bit quicker because there’s less processing. I don’t know if you way results into two queries, I hope it doesn’t.

The way I said is close to the manual. BUT if you want to delete an association between User and Folder with “my” way it’s a bit messy, unless you use extensions for advanced active record behavior; I think in your way it should be easier: you just delete a “has_folder” item…