How can i display data from different model

Table :: product

product_id

product_name

product_price

product_category (FK with another table)

Table :: productcategory

productcategory_id

productcategory_name

productcategory_description

and i have product default view which created by gii and it display some result that doesn’t match my functionality

View :: product

product_id :: 1 (CHtml::link)

product_name :: Silver Ring

product_price :: 4000 $

productcategory_id :: 1 (in this case i assumed product category 1 equal to accessories) and link to Productcategory model to show productcategory description when click on the link

the problem is how can i show productcategory_id to productcategory_name

sorry for my noob question , i actually wanna know about the correctly way to solved this problem by MVC solution

this is my code that i 've ever try out

In my Product :: Model




public function getProductCategoryName($_productcategoryid)

	{

		$productcategoryname=Productcategoryname::model()->findByPk($_productcategoryid);

		echo $temp=$productcategoryname->productcategoryname;

		return $temp;

	}



and this in my Product :: view




<?php echo CHtml::encode($data->getAttributeLabel('productcategoryid')); ?>:</b>

<?php echo CHtml::link(CHtml::encode($model->getProductCategoryName($data->productcategoryid)), array('view', 'id'=>$data->productcategoryid)); ?>



and i got a horrible defects

sorry again for my newbie yii framework

You need to relate the models:

Table::product

On you model class (Product.php I guess)




public function relations(){


    return array('category'=>array(self::BELONGS_TO, 'ProductCategory','product_category'));

}



Now on your view you can call $data->category->productcategory_name;

Check on relations my friend: http://www.yiiframework.com/doc/guide/1.1/en/database.arr#declaring-relationship

Thank you Antonio it works now !!

but i still have 1 question left

after i already show productcategory_name on my product view,

the second question is how can make link on " product_category " to display detail of product_category

from below it just only display product detail and i don’t know how to call different view

Product :: view




array('view', 'id'=>$data->productcategoryid));



I dont really understand what you mean, but if you wish to get the products under a category you should:

set a relationship on your productcategory model:




public function relations(){


    return array('products'=>array(self::MAS_MANY, 'Product','product_category'));

}



Then on the categories product, for view, you do get the id passed by the parameter and load a model productcategory: $model = $this->loadModel($_REQUEST[‘id’]); and pass the resulting model to the view:




$this->render('view', array('model'=>$model));



On the view you can loop through the products if any to display the products under that category

Cheers


foreach($model->products as $product) {

echo $product->what_ever;

}

1 Like

everything solved thank you for suggestion :D

Thanks, this also solved my problem.

Thanks sir

thank you so mjch sir