php yii 框架对象问题

标签 php mysql yii

我正在使用 yii 框架制作一个网站作为学校项目。我正在使用 XAMPP bundle (特别是 Apache 和 MySql)。我在 phpmyadmin 中创建了带有一些表的数据库,并在它们之间创建了所需的关系。现在我对 admin.php 文件中的以下代码部分有疑问:

<?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 位于/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;
}
}

正如您所看到的,此文件中存在与教授数据库的关系,我正在尝试将上面代码中小部件中的professor_id替换为教授表中的实际名称。在代码的其他一些部分,例如:

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

它工作得很好,但是在这个小部件中,当我尝试这样的事情时:

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

弹出以下错误“属性必须以“名称:类型:标签”的格式指定”。我在这里尝试了一些其他方法来访问此属性,但收效甚微,所以我一无所知。有什么办法可以做到这一点吗?

最佳答案

似乎需要这种格式

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

关于php yii 框架对象问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35070908/

相关文章:

html - Yii框架解释

自动完成文本字段 CJuiAutoComplete 小部件 yii 存储隐藏字段 id 和显示名称

mysql - yii中关系型mysql无主键表搜索(调用非整数字段)

php - 为什么 xpath 会删除 html 特殊字符?

php - 使用PHP邮件发送图像标签

php - 从 mysql 查询中获取 1 个字符串

mysql - 更新表时查询变慢

php - Laravel 辅助函数 base_path() 失败

php - 将帖子加载到 div 中

mysql - 从总分中获得排名