你好,只有问题
我正在为一个在前端使用sproutcore的项目开发一个restful应用程序。
我的问题实际上是什么是在需要返回json时从其他相关模型的模型中获取数据的最有效方法。我昨天读到它建议在使用数组时使用DAO层,所以对于我的例子,这是我到目前为止所做的。
我有一个客户列表,每个客户HAS_MANY品牌和每个品牌HAS_MANY项目。不 得到一个很好的形成的客户阵列与他们的品牌继承我所拥有的
$clients = Yii::app()->db->createCommand('select client.* from client where client.status = 1')->queryAll();
foreach($clients as $ckey => $client)
{
$clients[$ckey] = $client;
$brand_ids = Yii::app()->db->createCommand('select brand.id as brand_id, brand.client_id as b_client_id from brand where brand.client_id ='.$client['id'])->queryAll();
foreach($brand_ids as $bkey => $brand_id)
{
$clients[$ckey]['brands'][] = $brand_id['brand_id'];
}
}
这是我想要的回归到目前为止,但它是实现后续的最有效的方法吗?
答案 0 :(得分:3)
设置客户端模型
class Client extends CActiveRecord
{
//...
/**
* @return array relational rules.
*/
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(
'brands' => array(self::HAS_MANY, 'Brand', 'client_id'),
);
}
//...
public function defaultScope() {
return array('select'=>'my, columns, to, select, from, client'); //or just comment this to select all "*"
}
}
设置品牌模型
class Brand extends CActiveRecord
{
//...
/**
* @return array relational rules.
*/
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(
'client' => array(self::BELONGS_TO, 'Client', 'client_id'),
);
}
//...
//...
public function defaultScope() {
return array('select'=>'my, columns, to, select, from, brand'); //or just comment this to select all "*"
}
}
在您的动作功能中进行客户/品牌搜索
$clients = Client::model()->with('brands')->findAllByAttributes(array('status'=>1));
$clientsArr = array();
if($clients) {
foreach($clients as $client) {
$clientsArr[$client->id]['name'] = $client->name; //assign only some columns not entire $client object.
$clientsArr[$client->id]['brands'] = array();
if($client->brands) {
foreach($client->brands as $brand) {
$clientsArr[$client->id]['brands'][] = $brand->id;
}
}
}
}
print_r($clientsArr);
/*
Array (
[1] => Array (
name => Client_A,
brands => Array (
0 => Brand_A,
1 => Brand_B,
2 => Brand_C
)
)
...
)
*/
这是你想要的吗?
我知道,如果你想只选择品牌ID(不再是其他数据),你可以通过sql和 GROUP_CONCAT (MySQL)进行搜索,并选择所有品牌ID,用于分隔一行的客户逗号。 1,2,3,4,5,20,45,102
。
答案 1 :(得分:1)
如果您不想使用with()
功能使用CActiveRecord,那么您应该编写一个加入brand
表的SQL查询。
$rows = Yii::app()->db
->createCommand(
'SELECT c.*, b.id as brand_id
FROM client c INNER JOIN brand b
WHERE c.status = 1 AND b.client_id = c.id')
->queryAll();
$clients = array();
foreach ($rows as row) {
if (!isset($clients[$row['id']])) {
$clients[$row['id']] = $row;
$clients[$row['id']]['brands'] = array();
}
$clients[$row['id']]['brands'][] = $row['brand_id'];
}
这比执行一个查询以检索所有客户端然后执行N个查询以获取其品牌(其中N是客户端数量)更有效。您也可以加入第三个表projects
并检索每个品牌的所有相关项目。
答案 2 :(得分:1)
我意识到这已经过时了,但我一直在寻找解决方案,我认为这是一个很好的解决方案。
在我的基类Controller类(protected / Components / Controller.php)中,我添加了以下函数:
protected function renderJsonDeep($o) {
header('Content-type: application/json');
// if it's an array, call getAttributesDeep for each record
if (is_array($o)) {
$data = array();
foreach ($o as $record) {
array_push($data, $this->getAttributesDeep($record));
}
echo CJSON::encode($data);
} else {
// otherwise just do it on the passed-in object
echo CJSON::encode( $this->getAttributesDeep($o) );
}
// this just prevents any other Yii code from being output
foreach (Yii::app()->log->routes as $route) {
if($route instanceof CWebLogRoute) {
$route->enabled = false; // disable any weblogroutes
}
}
Yii::app()->end();
}
protected function getAttributesDeep($o) {
// get the attributes and relations
$data = $o->attributes;
$relations = $o->relations();
foreach (array_keys($relations) as $r) {
// for each relation, if it has the data and it isn't nul/
if ($o->hasRelated($r) && $o->getRelated($r) != null) {
// add this to the attributes structure, recursively calling
// this function to get any of the child's relations
$data[$r] = $this->getAttributesDeep($o->getRelated($r));
}
}
return $data;
}
现在,在一个对象或对象数组上调用renderJsonDeep将对JSON中的对象进行编码,包括你已经拉过的任何关系,比如将它们添加到DbCriteria中的'with'参数。
如果子对象有任何关系,那么这些关系也将在JSON中设置,因为getAttributesDeep是递归调用的。
希望这有助于某人。