How View Name Instead Of Id Of An Related Model In Gridview (List)?

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.

  1. Product which one of column is an “foreign id” it’s contains an other table ids this is the supplier_id

  2. 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); ?>



I think you need to use CDbCriteria (not CDbCriidteria):


 $criteria=new CDbCriteria;

I don’t understand your question at all. Could you please provide a more detailed description of your problem? If you cannot express it in English, perhaps can you post some pictures (screenshots) to illustrate it? That can be very helpful.

Or you can also post your question in International subforum. Maybe you can use a language that is more familiar to you there. :)

So, here my Product admin:

There is an "Supplier" column in the table. And in the column there are 1 numbers. But I would like to view the name of Suppliers here!

You can see my Supplier admin here:

And in the Product admin I would like to view the "Ret, Globis, Sos…" instead of "1, 2, 3,…" (ids)

And in the Product admin under the Supplier column I would like to replace the search box to a dropdownlist, which contains the Supplier names.

My international subforum is dead… :(

You may want to check this thread.

Hopefully this and this help you to solve your problem.

Thank you very much the help :)

I did it, but I have only one big problem :).

Here you can see the problem, this is given by the yii:

When I use the pager, and click on the "next" button or on the "3" page, I get that popup with that error message.

Would you like to post code of your CGridView and Controller (Product), please? Did you modify your search procedure above?

I also saw you used supplier_id as relation name:




public function relations()

{

   return array(

     'supplier_id' => array(self::BELONGS_TO, 'Supplier', 'id'),

   );

}



If the Product table has already had supplier_id field, I would choose different name for relation:




public function relations()

{

   return array(

     'supplier' => array(self::BELONGS_TO, 'Supplier', 'supplier_id'),

   );

}



iam new in yii framework ,the same problem accured my project also ,i have using two tables in database instead where i change in my grid view admin part please would you tell me