对象的 php yii 框架问题 [英] php yii framework issue with objects
问题描述
我正在使用 yii 框架制作一个网站作为学校项目.我正在使用 XAMPP 包(特别是 Apache 和 MySql).我在 phpmyadmin 中用一些表创建了数据库,并在它们之间创建了所需的关系.现在我在 admin.php 文件中的以下部分代码有问题:
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',
),
)); ?>
现在这是主要的模型php文件Raspored.php(admin.php在/views/raspored中):
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;
}
}
如您所见,此文件中存在与教授数据库的关系,我试图用教授表中的实际名称替换上层代码中小部件中的professional_id.在代码的其他部分,像这样:
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 ?
推荐答案
似乎需要这种格式
<?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 框架问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!