Many_Many Relation - No Results Found Problem

Hello,

i’ve got 2 tables “City” and “Car”. I want to click on a linked result from “City”-ListView to get to all the “Car”'s that could be found within the choosen “City”.

That’s my code:

Relation from "Car" Model:


	 

	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(

            'cities' => array(self::MANY_MANY, 'City', 'city_has_car(car_id, city_id)'),

			'posts' => array(self::HAS_MANY, 'Post', 'car_id'),

		);

	}

Relation from "City" Model:


	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(

			'state' => array(self::BELONGS_TO, 'State', 'state_id'),

			'cars' => array(self::MANY_MANY, 'Car', 'city_has_car(city_id, car_id)'),

		);

	}

Car controller function for actionIndex:


	public function actionIndex()

	{


        $q = $_GET['city'];


        $dataProvider =new CActiveDataProvider('Car',array(

                     'criteria'=>array(

                       'with'=>array('cities'),

                       'condition'=>"cities.id='$q'",

                       'together'=>true,

                     )

                   ));


         $this->render('index',array(

           'dataProvider'=>$dataProvider,

         ));

	}



_view from City (where $_GET[‘city’] comes from):


<div class="view">




    <?php echo CHtml::beginForm(array('car/index'), 'get'); ?>

      <?php echo CHtml::hiddenField('city', $data->id); ?>

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

      <?php echo CHtml::endForm(); ?>


</div>



car/index (Output):


<h1>Cars in <?php $city= City::model()->findByPk($_GET['city']);


    echo $city->name;


    ?></h1>


<?php $this->widget('zii.widgets.CListView', array(

    'id' => 'car-list',

    'dataProvider' => $dataProvider,

    'sortableAttributes' => array(

        'id',

        'name'

    ),

    'itemView' => '_view',

));

?>

I have three tables (all InnoDB): car, city, city_has_car

On car/index i only get the “No results found” message, but there are a lot of results on the database, anyone got a clue what’s going wrong here?

The log file put out the following query:


Querying SQL: SELECT `t`.`id` AS `t0_c0`, `t`.`name` AS `t0_c1`,

`cities`.`id` AS `t1_c0`, `cities`.`name` AS `t1_c1`, `cities`.`state_id`

AS `t1_c2`, `cars`.`id` AS `t2_c0`, `cars`.`name` AS `t2_c1` FROM

`car` `t`  LEFT OUTER JOIN `city_has_car` `cities_cities` ON

(`t`.`id`=`cities_cities`.`car_id`) LEFT OUTER JOIN `city` `cities` ON

(`cities`.`id`=`cities_cities`.`city_id`)  LEFT OUTER JOIN

`city_has_car` `cars_cars` ON

(`cities`.`id`=`cars_cars`.`city_id`) LEFT OUTER JOIN `car`

`cars` ON (`cars`.`id`=`cars_cars`.`car_id`)  WHERE

(cities.id='1') LIMIT 10

As a MySQL beginner, this query is a bit too complex for me to understand correctly what it does. Where comes "cities_cities" and "cars_cars" from?

Querytom

Dear Friend

The following may not be the solution.

But please try this.

Car controller function for actionIndex:




public function actionIndex()

{

        $q = $_GET['city'];       


        $cars=City::model()->with('cars')->findByPk($q)->cars;


        $dataProvider =new CArrayDataProvider($cars);   //Not CActiveDataProvider


        $this->render('index',array(

              'dataProvider'=>$dataProvider,

                                   ));

}



Thanks for your reply seenivasan!

Unfortunatly, your solution does’nt solve the problem. It is showing the same result as before: “No results found”.

The given array is entirely empty on cars/index.

Dear Friend

I am really sorry for that not being helpful.

Kindly test the following code. check what you get.




$city=City::model()->find(); //or findByPk(some value)

foreach($city->cars as $car)

    echo $car->name;