How To Take The Data From Another Table?

i have 2 table…

  1. tbl_product_index(product_id,product_name,brand) product_id is primary key

and

  1. tbl_pricing(product_id,price) product_id is primary key

here i use the relation:

in Pricing model:




public function relations()

	{

		return array(

			'product' => array(self::BELONGS_TO, 'ProductIndex', 'product_id'),

			

		);

	}

in ProductIndex model:




public function relations()

	{

		return array(

			'pricing' => array(self::HAS_ONE, 'Pricing', 'product_id'),

			);

	}



above code id genereted by the gii…

i want to show (product_id,product_name,brand,price) in CListView on index page of proxuctindex.

how to price data take from pricing table to productindex.

here i use


$productindex = ProductIndex::model()->findByPk(123);

echo $productindex->pricing->price;

it gives the result of price value at primary key 123.

when i use in place of above code this


$productindex = ProductIndex::model()->findByPk($product_id);

echo $productindex->pricing->price;

i found one error:

Undefined variable: product_id

how to solve this error?

i want to show all price value of product_id in CListView.

how to do this?

please help me…

thanks in advance

In the CListView, you have access to the current iteration data through the $data object. Try putting in $data->product_id instead of $product_id and see if it works.

B

thanks BCR…

I put

$data->product_id instead of $product_id

now i found the php notice:

Trying to get property of non-object.

now what i do?it is not getting any object.how to getting the object?

Where are you putting the code? When you call the CListView widget, you need to set it’s view file under the itemView attribute. You need to put that code in that view file.

If that doesn’t solve it, try pasting your code here so that we can have a look.

B

my view attribute file is _view.php.


<?php

/* @var $this ProductIndexController */

/* @var $data ProductIndex */

?>


<div class="view">


	

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

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

	<br />

        

  


	


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

	<?php echo CHtml::encode($data->brand); ?>

	<br />

	

	<b><?php

	

    $productindex = ProductIndex::model()->with('pricing')->findByPk($data='product_id');

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

	<?php echo CHtml::encode($productindex->pricing->price); ?>

	<br />

	

	


</div>

now also getting one error:

Fatal error: Call to a member function getAttributeLabel() on a non-object…

Change this




$productindex = ProductIndex::model()->with('pricing')->findByPk($data='product_id');



to




$productindex = ProductIndex::model()->with('pricing')->findByPk($data->product_id);



Read more about the special $data variable here: http://www.yiiframework.com/wiki/252/special-variables-in-cgridview-and-clistview/

when i use




$productindex = ProductIndex::model()->with('pricing')->findByPk($data->product_id);



then come

PHP notice

Use of undefined constant product_id - assumed ‘product_id’ .

Please confirm whether the error is for the same line. You have already used $data->product_id in your CHtml::link, you’re only using that again in the code I mentioned above. The error you stated might be for some other line in your code.

since i don’t see any CListView in your view file, try this…




$productindex = ProductIndex::model()->with('pricing')->findByPk(123);

echo $productindex->pricing->price;



thanks njasm…

when i use this code




$productindex = ProductIndex::model()->with('pricing')->findByPk(123);

echo $productindex->pricing->price;



then it give the same price for all the product because it use only one product_id value but i have many product_id for many product.

how to select product price for different product at different product_id?

i used $data->product_id in my CHtml::link, here,product_id belongs to productIndex table that cause it take the value very easy.but price belongs to Pricing table.i want to take data from the pricing table and view the productIndex.

how to find exact price for the each product.

when i used




$productindex = ProductIndex::model()->with('pricing')->findByPk(123);

echo $productindex->pricing->price;

it gives same price for all the product because it take only one product_id value.

Try this:




<div class="view">

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

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

	<br />

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

	<?php echo CHtml::encode($data->brand); ?>

	<br />

	<b>Price</b>

	<?php echo CHtml::encode($data->pricing->price); ?>

	<br />

</div>



Or




<div class="view">

	...

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

	<?php echo CHtml::encode($data->pricing->price); ?>

	<br />

</div>



‘$data’ in this partial view refers to an AR object of ProductIndex. And by using ‘$data->pricing’ syntax, you can automatically get an instance of Pricing model that is related to the main model. How convenient, isn’t it? This is why we use the relations. :)