2012-07-26 45 views
1

问:如何显示BootDetailView。yii如何从另一个模型显示BootDetailView的数据

Users 

name : aa 
company : test 
department : dep1 
section : sec1.1 
team : team1.1.1 

我已经2个tbls(部门和用户)

这部门TBL结构

department 
id | name  | p_id | company_id 
1 | dep1  | 0 | 1 
2 | dep2  | 0 | 1 
3 | sec1.1 | 1 | 1 
4 | sec2.1 | 2 | 1 
5 | team1.1.1 | 3 | 1 
6 | team1.1.2 | 3 | 1 
7 | team2.1.1 | 4 | 1 

这是用户TBL结构

user 
id | name | company_id | team_id 
1 | aa | 1   | 5 
2 | bb | 1   | 5 
3 | cc | 1   | 7 
4 | dd | 1   | 6 
5 | ee | 1   | 6 

我加入关系用户型号

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(
      'ranks' => array(self::BELONGS_TO, 'Rank', 'rank_id'), 
      'companies' => array(self::BELONGS_TO, 'Company', 'company_id'), 
      'departments' => array(self::BELONGS_TO, 'Department', 'team_id'), 
     ); 
    } 

该视图(CGridView使用在的index.php(视图))

<?php $this->widget('bootstrap.widgets.BootDetailView', array(
      'data'=>$model, 
      'attributes'=>array(
       //array('name'=>'id', 'label'=>'ID'), 
       array('name'=>'login_name', 'label'=>'Name'), 
       array('name'=>'first_name', 'label'=>'First Name'), 
       array('name'=>'last_name', 'label'=>'last Name'), 
       array('name'=>'email', 'label'=>'Email'), 
       array('name'=>'created', 'label'=>'Created'), 
       array('name'=>'ranks.name', 'label'=>'Rank'), 
       array('name'=>'companies.name', 'label'=>'Company'), 
       array('name'=>'departments.name', 'label'=>'Team'), 
      ), 
     )); ?> 

这是控制器

public function actionView($id) 
{ 
    $model = $this->loadModel($id); 
    $sql = 'SELECT id, name FROM rank r WHERE r.id = '. $model->rank_id; 
    $rank = Yii::app()->db->createCommand($sql)->queryAll(); 

    $sql = 'SELECT id, name FROM company c WHERE c.id = '. $model->company_id; 
    $company = Yii::app()->db->createCommand($sql)->queryAll();   

    $dst = $this->getDST($model->team_id); 

    $this->render('view',array(
     'model'=>$model, 
     'rank'=>$rank[0]['name'], 
     'company'=>$company[0]['name'], 
     'department'=>$dst['department'], 
     'section'=>$dst['section'], 
     'team'=>$dst['team'], 
    )); 
} 

public function getDST($team_id) // Getting the Department, Section and Team 
{ 
    $records = Department::model()->find('id=:id', array(':id'=>$team_id)); 
    if($records->p_id == 0) { 
    $dst['department'] = $model->team_id; 
     $dst['section'] = NULL; 
     $dst['team'] = NULL; 
    } else { 
     $records = Department::model()->find('id=:id', array(':id'=>$records->p_id)); 
     if($records->p_id == 0) { 
      $dst['department'] = $records->id; 
      $dst['section'] = $model->team_id; 
      $dst['team'] = NULL; 
     } else { 
      $dst['section'] = $records->id; 
      $dst['team'] = NULL; 
      $records = Department::model()->find('id=:id', array(':id'=>$records->p_id)); 
      if($records->p_id == 0) { 
       $dst['department'] = $records->id; 
       $dst['team'] = $model->team_id; 
      } 
     } 
    } 
    return $dst; 
} 

回答

1

在Yii的关系是简单的:

// User model 
public function relations() 
{ 
    return array(
     'rank' => array(self::BELONGS_TO, 'Rank', 'rank_id'), 
     'company' => array(self::BELONGS_TO, 'Company', 'company_id'), 
     'department' => array(self::BELONGS_TO, 'Department', 'team_id'), 
    ); 
} // User have only one rank, company and department 

// Department model 
public function relations() 
{ 
    return array(
     'parent' => array(self::BELONGS_TO, 'Department', 'p_id'), 
     'children' => array(self::HAS_MANY, 'Department', 'p_id'), 
     'company' => array(self::BELONGS_TO, 'Company', 'company_id'), 
     'users' => array(self::HAS_MANY, 'User', 'team_id'), 
    ); 
} 

因为: class BootDetailView扩展CDetailView
就够了:

// usercontroller 
function actionView($id) 
{ 
    $this->render('view', array('model' => $this->loadModel($id)); 
    // or for optimize sql query 
    $this->render('view', array('model' => User::model()->with(array('rank', 'company', 'department'))->findByPk($id)); 
} 

// view.php 
$details = array(
     'login_name', 
     'first_name', 
     'last_name', 
     'email', 
     'rank.name', 
     'company.name', 
    ); 
$departments = array(); 
$d = $model->department; 
$departments[] = 'department.name'; 
$s = 'parent.'; 
while ($d->parent != null) { 
    $departments[] = 'department.'.$s.'name'; 
    $s .= 'parent.'; 
    $d = $d->parent; 
} 

$this->widget('bootstrap.widgets.BootDetailView', array(
    'data'=>$model, 
    'attributes'=> array_merge($details, $departments), 
)); 
+0

非常感谢。非常有趣的关系。真的非常感谢。 – 2012-07-26 06:03:52

+0

注意'department.parent.parent.name', - 它是'硬编码'。尝试迭代每个父项并将其收集到$ attributes的数组中以便在detailview中使用 – Sergey 2012-07-26 06:12:42

+0

对不起,我无法捕捉到您的。请解释更多细节。 – 2012-07-26 06:21:06

相关问题