Hi,
I am new to Yii and I am trying to write my first application. I have been browsing this forum for a while but still didn’t manage to find the right answer for my 'problem" and I would very much appreciate your help.
I don’t really think it is a problem, unless you ar totally new to Yii.
So, I have three tables; masina (PK:id, numar etc), sofer (PK:id, nume, etc) and masinasofer (PK:id, FK:masina_id, FK:sofer_id) and I want to display the rows from masinsofer with complete information.
Here is model MasinaSofer:
[sub]<?php
/**
-
This is the model class for table "masina_sofer".
-
The followings are the available columns in table ‘masina_sofer’:
-
@property string $id
-
@property string $masina_id
-
@property string $sofer_id
-
@property double $kmparcursi
-
@property double $pretcombustibil
-
@property string $data
*/
class MasinaSofer extends CActiveRecord
{
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'masina_sofer';
}
/**
* @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('id, masina_id, sofer_id, kmparcursi, pretcombustibil, data', 'required'),
array('kmparcursi, pretcombustibil', 'numerical'),
array('id, masina_id, sofer_id', 'length', 'max'=>128),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, masina_id, sofer_id, kmparcursi, pretcombustibil, data', '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(
'masini'=>array(self::HAS_MANY, 'Masina', 'masina_id'),
'soferi'=>array(self::HAS_MANY, 'Sofer', 'sofer_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'masina_id' => 'Masina',
'sofer_id' => 'Sofer',
'kmparcursi' => 'Kmparcursi',
'pretcombustibil' => 'Pretcombustibil',
'data' => 'Data',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id,true);
$criteria->compare('masina_id',$this->masina_id,true);
$criteria->compare('sofer_id',$this->sofer_id,true);
$criteria->compare('kmparcursi',$this->kmparcursi);
$criteria->compare('pretcombustibil',$this->pretcombustibil);
$criteria->compare('data',$this->data,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return MasinaSofer the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
[/sub]Here is relations() from Masina:
[sub]return array(
'soferi'=>array(self::MANY_MANY, 'Sofer', 'masina_sofer(masina_id, sofer_id)'),
);
[/sub]Here is relations() from Sofer:
[sub]return array(
'soferi'=>array(self::MANY_MANY, 'Sofer', 'masina_sofer(masina_id, sofer_id)'),
);
[/sub]here is the code from masinasofer/view.php:
[sub]<h1>View MasinaSofer #<?php echo $model->id; ?></h1>
<?php $this->widget(‘zii.widgets.CDetailView’, array(
'data'=>$model,
'attributes'=>array(
'id',
'masina_id',
'sofer_id',
'kmparcursi',
'pretcombustibil',
'data',
),
)); ?>
[/sub]
Instead of masina_id I want to display masina.numar
Instead of sofer_id I want to display sofer.nume