Hi,
I am newbie in Yii, I have a problem. I have a GridView / list in the Admin, and I would like to view the name of the related model instead of its id number, and I would like to search for it.
I have 2 table.
-
Product which one of column is an “foreign id” it’s contains an other table ids this is the supplier_id
-
Supplier table is where there are the ‘id’ and the ‘name’ column.
I read some articles, but I think, I use them badly in my project.
This is the part code of my Product, at firstly I tired only implement the search proccess by this tutorial. But I have got this error message:
include(CDbCriidteria.php): failed to open stream: No such file or directory
class Product extends CActiveRecord
{
/**
* Related models
*/
public $supplier_name;
...
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(
'supplier_id' => array(self::BELONGS_TO, 'Supplier', 'id'),
);
}
...
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('prod_name, sdu, sdu_supplier, prodgroup_id, vtsz, vat, production_neprice, production_brprice, margin, seller_neprice, seller_brprice, act_stock, min_stock, quantity, supplier_id, modify_date', 'required'),
array('prodgroup_id, vat, margin, act_stock, min_stock', 'numerical', 'integerOnly'=>true),
array('production_neprice, production_brprice, seller_neprice, seller_brprice', 'numerical'),
array('prod_name', 'length', 'max'=>255),
array('sdu, sdu_supplier', 'length', 'max'=>100),
array('vtsz, supplier_id', 'length', 'max'=>11),
array('quantity', 'length', 'max'=>50),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, prod_name, sdu, sdu_supplier, prodgroup_id, vtsz, vat, production_neprice, production_brprice, margin, seller_neprice, seller_brprice, act_stock, min_stock, quantity, supplier_id, supplier_name, modify_date', 'safe', 'on'=>'search'),
);
}
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriidteria;
$criteria->with = array('supplier');
$criteria->compare('id',$this->id,true);
$criteria->compare('prod_name',$this->prod_name,true);
$criteria->compare('sdu',$this->sdu,true);
$criteria->compare('sdu_supplier',$this->sdu_supplier,true);
$criteria->compare('prodgroup_id',$this->prodgroup_id);
$criteria->compare('vtsz',$this->vtsz,true);
$criteria->compare('vat',$this->vat);
$criteria->compare('production_neprice',$this->production_neprice);
$criteria->compare('production_brprice',$this->production_brprice);
$criteria->compare('margin',$this->margin);
$criteria->compare('seller_neprice',$this->seller_neprice);
$criteria->compare('seller_brprice',$this->seller_brprice);
$criteria->compare('act_stock',$this->act_stock);
$criteria->compare('min_stock',$this->min_stock);
$criteria->compare('quantity',$this->quantity,true);
$criteria->compare('supplier_id',$this->supplier_id,true);
$criteria->compare('modify_date',$this->modify_date,true);
$criteria->compare( 'supplier.name', $this->supplier_name, true );
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array(
'attributes'=>array(
'supplier_name'=>array(
'asc'=>'supplier.name',
'desc'=>'supplier.name DESC',
),
'*',
),
),
));
}
public function getSupplierOptions()
{
$supplierssArray = CHtml::listData($this->supplier_id, 'id', 'name');
return $supplierssArray;
}
And I have question about what is the best way of implement that in GridView view the Supplier’s name instead of their id?
Or maybe Can give me links, tutorials, how I do this myself? (Sorry but I don’t know how I have to search in Google my problem… I am not from a country where the native language is English, and it is my problem, how can I exactly search on my problem in Google.
Thanks the help
EDIT:
I would like to in ‘view/admin’ and ‘view/view’ use that I view the name of Supplier instead of id.
In ‘view/_form’ I can use this code:
$list = CHtml::listData(Supplier::model()->findAll(array('order' => 'name')), 'id', 'name');
<?php echo $form->dropDownList($model,'supplier_id',$list); ?>