doutuobao4004 2016-01-28 20:01
浏览 54
已采纳

php yii框架问题与对象

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 ?

  • 写回答

2条回答 默认 最新

  • douyuepi6485 2016-01-28 21:08
    关注

    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',
      ),
    )); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路