Displaying Data From Two Models

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); ?>

               [b] with[/b]

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

How do this ans me thank you ???

What does the $data variable holds?

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.com.au/hosting/support/CreatingAQuickMySQLRelationalDatabase

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>";



  1. 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()


{


	&#036;dataProvider=new CActiveDataProvider('OfferDetail',array(


'criteria'=&gt;array(


    'with'=&gt;array('data'),


),

));

	&#036;this-&gt;render('index',array(


		'dataProvider'=&gt;&#036;dataProvider,


	));


}