php yii 框架的对象问题(二) [英] php yii framework issue with objects (2)

查看:18
本文介绍了php yii 框架的对象问题(二)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有点延续我已经发布的问题phpyii 框架问题与对象",但是出现了一个新问题,因此我发布了一个新问题,以便更好地显示代码并解释错误所在.这是代码有问题的部分:

This is somewhat the continuation to the question I already posted "php yii framework issue with objects", but a new problem arised so I am posting a new question for the sake of better code display and explanation of what's wrong. This is the problematic part of the code:

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'predmet-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
    //array(
    //  'name'=>'naziv',
    //  'value'=>$model->studijskiProgram->naziv,
    //),
    'sp_id',
    'naziv',
    'semestar',
    'predavanja',
    'vjezbe',
    //array(
    //  'class'=>'CButtonColumn',
    //),
),
)); ?>

这是关系所在的模型类的代码:

And this is the code of the model class where relations are located:

<?php

/**
* This is the model class for table "predmet".
*
* The followings are the available columns in table 'predmet':
* @property integer $id
* @property integer $sp_id
* @property string $naziv
* @property integer $semestar
* @property integer $predavanja
* @property integer $vjezbe
*/
   class Predmet extends CActiveRecord
   {
     /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Predmet the static model class
     */

const STATUS_O = "O";
const STATUS_I = "I";
const STATUS_VP = "VP";

public static function model($className=__CLASS__)
{
    return parent::model($className);
}

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

/**
 * @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('sp_id, naziv, semestar, predavanja, vjezbe, status', 'required'),
        array('sp_id, semestar, predavanja, vjezbe', 'numerical', 'integerOnly'=>true),
        array('naziv', 'length', 'max'=>100),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('id, sp_id, naziv, semestar, predavanja, vjezbe, status', '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.

    //POVEZUJEMO PREDMET I STUDIJSKI PROGRAM
    //BELONGS_TO ide za vezu jedan u vise(kad na stranu 1 pisemo kome pripada)
    //HASMANY ili HAS_MANY (?) JE ZA VISE U JEDAN

    /*ovo prvo 'studijskiProgram' je kao pokazivac na studijski program s kojim je povezan
    Neka imamo $predmet. da bi pristupili nazivu studijskog programa kome pripada, koristicemo:
    $predmet->studijskiProgram->naziv
    */
    return array(
        'studijskiProgram' => array(self::BELONGS_TO, 'StudijskiProgram', 'sp_id')
    );
}

/**
 * @return array customized attribute labels (name=>label)
 */
public function attributeLabels()
{
    return array(
        'id' => 'ID',
        'sp_id' => 'Studijski program',
        'naziv' => 'Naziv',
        'semestar' => 'Semestar',
        'predavanja' => 'Predavanja',
        'vjezbe' => 'Vjezbe',
        'status' => 'Status'
    );
}

/**
 * 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('sp_id',$this->sp_id);
    $criteria->compare('naziv',$this->naziv,true);
    $criteria->compare('semestar',$this->semestar);
    $criteria->compare('predavanja',$this->predavanja);
    $criteria->compare('vjezbe',$this->vjezbe);

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


//Funkcija za punjenje Combo box-a (Wombo)
public function uzmiStatusPredmeta()
{
    return array(
            self::STATUS_O => 'Obavezni',
            self::STATUS_I => 'Izborni',
            self::STATUS_VP => 'VP'
    );
}

public function uzmiSpisakStudijskihPrograma()
{
    $query = 'select id, naziv from studijski_program';
    $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 vratiProgram()
{
    $query = 'select s.naziv 
    from studijski_program s,predmet p
    where p.sp_id = s.id';
    $rezultat = Yii::app()->db->createCommand($query)->queryScalar();
    //$rezultat1 = implode($rezultat);
    //$rezultat2 = implode($rezultat1);
    return $rezultat;
}
public function vratiProgramId()
{
    $query = 'select s.id 
    from studijski_program s,predmet p
    where p.sp_id = s.id';
    $rezultat = Yii::app()->db->createCommand($query)->queryScalar();
    //$rezultat1 = implode($rezultat);
    //$rezultat2 = implode($rezultat1);
    return $rezultat;
}

}

正如您在此处看到的,此处使用predmet"中的sp_id"外键与studijskiProgram"存在关联.但是,当我尝试从代码上部的studijskiProgram"访问naziv"属性时,如下所示:

As you can see here there exists relation to "studijskiProgram" here using the "sp_id" foreign key in "predmet". However when I am trying to access "naziv" proprety from "studijskiProgram" in the upper part of the code like this:

    array(
        'name'=>'naziv',
        'value'=>$model->studijskiProgram->naziv,
    ),

这是上一个使用 CDatilView 小部件的问题的解决方案.出于某种原因,这里同样的事情不起作用,并报告尝试获取非对象的属性的错误.知道要改变什么才能起作用吗?

which was the solution with the previous question where CDeatilView widget is used. For some reason the same thing here doesn't work and reports error of trying to get a proprety of a non object. Any idea what to alter so it would work?

推荐答案

如下编写代码:-

 // CGridView column definition:
        'columns'=>array(
            array(
                'name'=>'naziv',
                'value'=>'$data->studijskiProgram->naziv',
                'type'=>'text',
            ),

    // Preparing the DataProvider in the model:
    return new CActiveDataProvider(get_class($this),array(
        'sort'=>array(
            'multiSort'=>false,
            'attributes'=>array(
                'naziv'=>array(                
                    'desc'=>'naziv desc',
                ),

这篇关于php yii 框架的对象问题(二)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆