Yii Framework Forum: Displaying Data From Two Models - Yii Framework Forum

Jump to content

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

Displaying Data From Two Models Rate Topic: -----

#1 User is offline   channasmcs 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 87
  • Joined: 06-March 13
  • Location:Srilanka

Posted 14 August 2013 - 12:44 PM

i have 2 table
1 Offer_detail

CREATE TABLE IF NOT EXISTS `offer_detail` (
`id` int(60) NOT NULL AUTO_INCREMENT,
`company_id` int(60) NOT NULL,
`offertype_id` int(60) NOT NULL,
`category_id` int(60) NOT NULL);

2 offer_company

CREATE TABLE IF NOT EXISTS `offer_company` (
`id` int(60) NOT NULL AUTO_INCREMENT,
`company_name` varchar(150) NOT NULL,
`contact_detail` text NOT NULL,

i want to show company name & contact detail from Offer company table when list data in my app

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

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

with

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


How do this ans me thank you ???
0

#2 User is offline   nineinchnick 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 622
  • Joined: 12-September 11
  • Location:Bialystok, Poland

Posted 14 August 2013 - 02:13 PM

What does the $data variable holds?
Don't be a dick.
0

#3 User is offline   Scott Deagan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 70
  • Joined: 04-February 10

Posted 14 August 2013 - 05:05 PM

Hi channasmcs,

I'm not clear about what it is you're trying to achieve. The first thing I would suggest is learning a little about using relationships:

http://www.anchor.co...ationalDatabase

Given this, you should create relationships in your DB. Gii will then generate models with the appropriate "relations". So your tables would be created something like the following:

CREATE TABLE IF NOT EXISTS `offer_company` (
`id` int(60) PRIMARY KEY NOT NULL AUTO_INCREMENT,
`company_name` varchar(150) NOT NULL,
`contact_detail` text NOT NULL
);


CREATE TABLE `offer_detail` (
  `id` int(60) PRIMARY KEY NOT NULL AUTO_INCREMENT,
  `company_id` int(60) NOT NULL,
  `offertype_id` int(60) NOT NULL,
  `category_id` int(60) NOT NULL,
  KEY `FK_OFFER_COMPANY` (`company_id`),
  CONSTRAINT `FK_OFFER_COMPANY` FOREIGN KEY (`company_id`) REFERENCES `offer_company` (`id`)
);


In the above, I'm assuming you want offer_detail.company_id to reference offer_company.id.

If you then use Gii to generate your models and you leave the "build relations" checkbox ticked, Yii will automatically generate something like the following in your OfferCompany model:

	/**
	 * @return array relational rules.
	 */
	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(
			'offerDetails' => array(self::HAS_MANY, 'OfferDetail', 'company_id'),
		);
	}


and something like the following in your OfferDetail model:

	/**
	 * @return array relational rules.
	 */
	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(
			'company' => array(self::BELONGS_TO, 'OfferCompany', 'company_id'),
		);
	}


You can then use these relations in the following manner:

1. What offers belong to company with id = 1?

    $offerCompany = OfferCompany::model()->findByPk(1);
    echo "Company: {$offerCompany->company_name} {$offerCompany->contact_detail}<br>";
    echo "Number of offers: " . count($offerCompany->offerDetails) . "<br><br>";
    
    echo "The offers_details for {$offerCompany->company_name}:<br>";
    foreach ($offerCompany->offerDetails as $rec)
      echo "offertype_id: {$rec->offertype_id} category_id: {$rec->category_id}<br>";
    echo "<br><br>";


2. What company does the offer with id = 5 belong to?

    $offerDetail = OfferDetail::model()->findByPk(5);
    echo "The company that offer_detail 5 belongs to is: {$offerDetail->company->company_name}<br>";


So long as you have set up your relations correctly, you would then be able to display the company_name and contact_detail from a CActiveDataProvider using the following:

<?php echo CHtml::encode($data->company->company_name); ?>
<?php echo CHtml::encode($data->company->contact_detail); ?>


Hope that this helps.
0

#4 User is offline   channasmcs 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 87
  • Joined: 06-March 13
  • Location:Srilanka

Posted 14 August 2013 - 10:13 PM

View PostScott Deagan, on 14 August 2013 - 05:05 PM, said:

Hi channasmcs,

I'm not clear about what it is you're trying to achieve. The first thing I would suggest is learning a little about using relationships:

http://www.anchor.co...ationalDatabase

Given this, you should create relationships in your DB. Gii will then generate models with the appropriate "relations". So your tables would be created something like the following:

CREATE TABLE IF NOT EXISTS `offer_company` (
`id` int(60) PRIMARY KEY NOT NULL AUTO_INCREMENT,
`company_name` varchar(150) NOT NULL,
`contact_detail` text NOT NULL
);


CREATE TABLE `offer_detail` (
  `id` int(60) PRIMARY KEY NOT NULL AUTO_INCREMENT,
  `company_id` int(60) NOT NULL,
  `offertype_id` int(60) NOT NULL,
  `category_id` int(60) NOT NULL,
  KEY `FK_OFFER_COMPANY` (`company_id`),
  CONSTRAINT `FK_OFFER_COMPANY` FOREIGN KEY (`company_id`) REFERENCES `offer_company` (`id`)
);


In the above, I'm assuming you want offer_detail.company_id to reference offer_company.id.

If you then use Gii to generate your models and you leave the "build relations" checkbox ticked, Yii will automatically generate something like the following in your OfferCompany model:

	/**
	 * @return array relational rules.
	 */
	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(
			'offerDetails' => array(self::HAS_MANY, 'OfferDetail', 'company_id'),
		);
	}


and something like the following in your OfferDetail model:

	/**
	 * @return array relational rules.
	 */
	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(
			'company' => array(self::BELONGS_TO, 'OfferCompany', 'company_id'),
		);
	}


You can then use these relations in the following manner:

1. What offers belong to company with id = 1?

    $offerCompany = OfferCompany::model()->findByPk(1);
    echo "Company: {$offerCompany->company_name} {$offerCompany->contact_detail}<br>";
    echo "Number of offers: " . count($offerCompany->offerDetails) . "<br><br>";
    
    echo "The offers_details for {$offerCompany->company_name}:<br>";
    foreach ($offerCompany->offerDetails as $rec)
      echo "offertype_id: {$rec->offertype_id} category_id: {$rec->category_id}<br>";
    echo "<br><br>";


2. What company does the offer with id = 5 belong to?

    $offerDetail = OfferDetail::model()->findByPk(5);
    echo "The company that offer_detail 5 belongs to is: {$offerDetail->company->company_name}<br>";


So long as you have set up your relations correctly, you would then be able to display the company_name and contact_detail from a CActiveDataProvider using the following:

<?php echo CHtml::encode($data->company->company_name); ?>
<?php echo CHtml::encode($data->company->contact_detail); ?>


Hope that this helps.



should need change actio index like this in offer_detail

public function actionIndex()
{
$dataProvider=new CActiveDataProvider('OfferDetail',array(
'criteria'=>array(
'with'=>array('data'),
),
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
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