2016-01-28 105 views
0

我正在使用yii框架製作一個網站作爲學校項目。我正在使用XAMPP包(特別是Apache和MySql)。我在phpmyadmin中用一些表創建了數據庫,並在它們之間創建了需要的關係。現在我有代碼的admin.php的文件中的以下部分的問題:php yii框架問題與對象

<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model, 
'attributes'=>array(
    //'ID', 
    'profesor_id', 
    'predmet_id', 
    'sala', 
    'od_id', 
    'do_id', 
    'dan_id', 
), 
)); ?> 

現在,這是主力機型PHP文件Raspored.php(admin.php的發現在/視圖/ raspored):

<?php 

/** 
* This is the model class for table "raspored". 
* 
* The followings are the available columns in table 'raspored': 
* @property integer $ID 
* @property integer $profesor_id 
* @property integer $predmet_id 
* @property integer $sala_id 
* @property integer $od_id 
* @property integer $do_id 
* @property integer $dan_id 
* 
* The followings are the available model relations: 
* @property Predmet $predmet 
* @property Dan $dan 
* @property Profesor $profesor 
* @property Sala $sala 
*/ 
class Raspored extends CActiveRecord 
{ 
    /** 
* Returns the static model of the specified AR class. 
* @param string $className active record class name. 
* @return Raspored the static model class 
*/ 
public static function model($className=__CLASS__) 
{ 
    return parent::model($className); 
} 

/** 
* @return string the associated database table name 
*/ 
public function tableName() 
{ 
    return 'raspored'; 
} 

/** 
* @return array validation rules for model attributes. 
*/ 
public function rules() 
{ 
    // NOTE: you should only define rules for those attributes that 
    // will receive user inputs. 
    return array(
     array('profesor_id, predmet_id, sala_id, od_id, do_id, dan_id', 'required'), 
     array('profesor_id, predmet_id, sala_id, od_id, do_id, dan_id', 'numerical', 'integerOnly'=>true), 
     // The following rule is used by search(). 
     // Please remove those attributes that should not be searched. 
     array('ID, profesor_id, predmet_id, sala_id, od_id, do_id, dan_id', 'safe', 'on'=>'search'), 
    ); 
} 

/** 
* @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(
     'predmet' => array(self::BELONGS_TO, 'Predmet', 'predmet_id'), 
     'dan' => array(self::BELONGS_TO, 'Dan', 'dan_id'), 
     'profesor' => array(self::BELONGS_TO, 'Profesor', 'profesor_id'), 
     'sala' => array(self::BELONGS_TO, 'Sala', 'sala_id'), 
     'terminod' => array(self::BELONGS_TO, 'Termin', 'od_id'), 
     'termindo' => array(self::BELONGS_TO, 'Termin', 'do_id'), 
    ); 
} 

/** 
* @return array customized attribute labels (name=>label) 
*/ 
public function attributeLabels() 
{ 
    return array(
     'ID' => 'ID', 
     'profesor_id' => 'Profesor', 
     'predmet_id' => 'Predmet', 
     'sala_id' => 'Sala', 
     'od_id' => 'Od', 
     'do_id' => 'Do', 
     'dan_id' => 'Dan', 
    ); 
} 

/** 
* Retrieves a list of models based on the current search/filter conditions. 
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions. 
*/ 
public function search() 
{ 
    // Warning: Please modify the following code to remove attributes that 
    // should not be searched. 

    $criteria=new CDbCriteria; 

    $criteria->compare('ID',$this->ID); 
    $criteria->compare('profesor_id',$this->profesor_id); 
    $criteria->compare('predmet_id',$this->predmet_id); 
    $criteria->compare('sala_id',$this->sala_id); 
    $criteria->compare('od_id',$this->od_id); 
    $criteria->compare('do_id',$this->do_id); 
    $criteria->compare('dan_id',$this->dan_id); 

    return new CActiveDataProvider($this, array(
     'criteria'=>$criteria, 
    )); 
} 

public function uzmiSpisakDana() 
{ 
    $query = 'select id, naziv from dan'; 
    $rezultat = Yii::app()->db->createCommand($query)->queryAll(); 
    $spisak = CHtml::listData($rezultat, 'id', 'naziv'); //prikazuje naziv, krije ID(valjda radi combo boxa-wombo ?) 

    return $spisak; 
} 

public function uzmiSpisakTermina() 
{ 
    $query = 'select id, naziv from termin'; 
    $rezultat = Yii::app()->db->createCommand($query)->queryAll(); 
    $spisak = CHtml::listData($rezultat, 'id', 'naziv'); //prikazuje naziv, krije ID(valjda radi combo boxa-wombo ?) 

    return $spisak; 
} 

public function uzmiSpisakProfesorPredmet() 
{ 
    $query = 'select a.id, CONCAT(prof.naziv, ", ", pred.naziv) as naziv 
    from angazman a, profesor prof, predmet pred 
    where a.profesor_id = prof.id and a.predmet_id = pred.id'; 
    $rezultat = Yii::app()->db->createCommand($query)->queryAll(); 
    $spisak = CHtml::listData($rezultat, 'id', 'naziv'); //prikazuje naziv, krije ID(valjda radi combo boxa-wombo ?) 

    return $spisak; 
} 
} 

正如你所看到的,這個文件中存在與教授數據庫的關係,並且我試圖用教授表中的實際名稱代替上面代碼中的部件中的professor_id。在代碼的其他一些地方,像這樣的:

<?php echo CHtml::encode($data->profesor->naziv); ?> 

它完美的作品,但在這個小工具,當我嘗試這樣:

'profesor_id'=>$model->profesor->naziv, 

以下錯誤彈出的」屬性必須以「名稱:類型:標籤」的格式指定。我嘗試了其他一些訪問此屬性的方法,但收效甚微,因此我一無所知。有沒有辦法做到這一點?

回答

0

似乎需要這種格式

<?php $this->widget('zii.widgets.CDetailView', array(
    'data'=>$model, 
    'attributes'=>array(
     //'ID', 
     array('name'=>'profesor_id', 
      'value' => $model->profesor->naziv,), // or $data->profesor->naziv 
     'predmet_id', 
     'sala', 
     'od_id', 
     'do_id', 
     'dan_id', 
), 
)); ?> 
+0

感謝的人有很大的幫助,是解決它 –

+0

所以我ecountered幾乎同樣的問題與代碼的以下部分: '插件('籽億。 widgets.grid.CGridView '陣列( \t 'ID'=> 'raspored柵', \t '的dataProvider'=> $模型 - >搜索(), \t '過濾'=> $模型, \t'列'=>數組( \t \t // 'ID', \t \t陣列( '名稱'=> 'naziv', \t \t \t '值'=> $模型 - > profesor-> naziv, ) \t \t // 'profesor_id', \t \t 'predmet_id', \t \t 'sala_id', \t \t 'OD_ID', \t \t 'do_id', \t \t 'dan_id', \t \t陣列( \t \t \t '類'=> 'CButtonColumn', \t \t) \t) )); ?>' 現在,這似乎幾乎是相同的,除了它是某種gridview小工具,它說我想獲得一個非對象的proprety ... –

+0

@waltdisney請發佈最後一個問題作爲一個新的問題,並正確格式化我可以看到它在正確的方式沒有混淆的第一部分已經解決.. – scaisEdge

0
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model, 
'attributes'=>array(
    //'ID', 
    array(
      'name'=>'naziv', 
      'header'=>$model->getAttributeLabel('naziv'), 
      'value'=>'($model->profesor ? $model->profesor->naziv : "empty")', 
     ), 
    'predmet_id', 
    'sala', 
    'od_id', 
    'do_id', 
    'dan_id', 
), 
)); ?> 
+0

謝謝這也解決了這個問題,只是一定的編輯。該行'value'=>'($ model-> profesor?$ model-> profesor-> naziv:「空」)'沒有這樣工作,我刪除了第二對單引號,這很好 –