How To Take The Data From Another Table?


(Rajkumar000000) #1

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


(Bhavikruparel) #2

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


(Rajkumar000000) #3

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?


(Bhavikruparel) #4

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


(Rajkumar000000) #5

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…


(Bhavikruparel) #6

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/


(Rajkumar000000) #7

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’ .


(Bhavikruparel) #8

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.


(Njmorais) #9

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




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

echo $productindex->pricing->price;




(Rajkumar000000) #10

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?


(Rajkumar000000) #11

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.


(Softark) #12

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. :)