class Client extends CActiveRecord
{
public function relations()
{
return array(
'brands' => array(self::HAS_MANY, 'Brand', 'client_id'),
);
}
public function defaultScope() {
return array('select'=>'my, columns, to, select, from, client');
}
}
class Brand extends CActiveRecord
{
public function relations()
{
return array(
'client' => array(self::BELONGS_TO, 'Client', 'client_id'),
);
}
public function defaultScope() {
return array('select'=>'my, columns, to, select, from, brand');
}
}
/
$clients = Client::model()->with('brands')->findAllByAttributes(array('status'=>1));
$clientsArr = array();
if($clients) {
foreach($clients as $client) {
$clientsArr[$client->id]['name'] = $client->name;
$clientsArr[$client->id]['brands'] = array();
if($client->brands) {
foreach($client->brands as $brand) {
$clientsArr[$client->id]['brands'][] = $brand->id;
}
}
}
}
print_r($clientsArr);
?
, - ( ), sql GROUP_CONCAT (MySQL) , . 1,2,3,4,5,20,45,102.