php yii framework issue with objects

2019-09-11 09:28发布

问题:

I am making a website as a school project using yii framework. I am using XAMPP bundle (specifically Apache and MySql). I've created database in phpmyadmin with some tables, created needed relations between them as well. Now I have an issue with the following part of the code in admin.php file:

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

Now this is the main model php file Raspored.php (admin.php is found in /views/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;
}
}

As you see there is a relation to professor database in this file, and I am trying to replace professor_id in widget in the upper code with its actual name from the professor table. In some other parts of the code, like this one:

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

it works perfectly, but in this widget when I try something like this:

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

the following error pops up "The attribute must be specified in the format of "Name:Type:Label"". I tried some other ways of accessing this attribute here but with little success so I am left clueless. Is there any way to do this ?

回答1:

seems is required this format

<?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',
  ),
)); ?>


回答2:

<?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',
),
)); ?>


标签: php mysql yii