Yii Framework Forum: How To Take The Data From Another Table? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Take The Data From Another Table? Rate Topic: -----

#1 User is offline   rajkumar000000 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 88
  • Joined: 18-September 12

Posted 17 January 2013 - 02:48 AM

i have 2 table..
1. tbl_product_index(product_id,product_name,brand) product_id is primary key
and
2. 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
0

#2 User is offline   BCR 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 12-December 12

Posted 17 January 2013 - 03:49 AM

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
0

#3 User is offline   rajkumar000000 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 88
  • Joined: 18-September 12

Posted 17 January 2013 - 04:09 AM

View PostBCR, on 17 January 2013 - 03:49 AM, said:

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?
0

#4 User is offline   BCR 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 12-December 12

Posted 17 January 2013 - 04:13 AM

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
0

#5 User is offline   rajkumar000000 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 88
  • Joined: 18-September 12

Posted 17 January 2013 - 04:21 AM

View PostBCR, on 17 January 2013 - 04:13 AM, said:

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

#6 User is offline   BCR 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 12-December 12

Posted 17 January 2013 - 04:30 AM

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.yiiframew...-and-clistview/
0

#7 User is offline   rajkumar000000 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 88
  • Joined: 18-September 12

Posted 17 January 2013 - 04:46 AM

View PostBCR, on 17 January 2013 - 04:30 AM, said:

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.yiiframew...-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' .
0

#8 User is offline   BCR 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 12-December 12

Posted 17 January 2013 - 05:40 AM

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

#9 User is offline   njasm 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 09-November 11

Posted 17 January 2013 - 05:49 AM

since i don't see any CListView in your view file, try this...

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

0

#10 User is offline   rajkumar000000 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 88
  • Joined: 18-September 12

Posted 17 January 2013 - 06:11 AM

View Postnjasm, on 17 January 2013 - 05:49 AM, said:

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?
0

#11 User is offline   rajkumar000000 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 88
  • Joined: 18-September 12

Posted 17 January 2013 - 06:29 AM

View PostBCR, on 17 January 2013 - 05:40 AM, said:

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.



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

#12 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,071
  • Joined: 16-February 11
  • Location:Japan

Posted 17 January 2013 - 09:20 AM

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users