0
votes

I have the following model in a CakePHP 1.2 app:

class Equipment extends AppModel {

      var $name = 'Equipment';
       var $belongsTo=array("City");
   ///
}

And my City model looks like this:

class City extends AppModel {

     var $name = 'City';
     var $belongsTo=array("State");
  ///
}

I'm not doing anything special in the EquipmentController -- just getting all the equipment with a Paginator.

So when I try to implement the pagination in the view like this:

  <th><?php echo $paginator->sort('Equipment',"Equipment.name");?></th>
  <th><?php echo $paginator->sort('City',"City.name");?></th>
  <th><?php echo $paginator->sort('State',"State.name");?></th>  

I can sort by Equipment name and City name, but sorting by State name gives me a sorted list by the City.id -- not the State.name. I've tried using City.State.name, but that gave me the same result. I've change the "recursive" property on both models to "2", with the same result.

               );

Why does this not work? Can anyone give me a work-around or show me what I am doing wrong?

EDIT I forgot to say that I tried Containable, which did not work either. This is how I set it up:

   $this->paginate = array(
  "conditions" => array("Equipment.expire_time >"=>date("Y-m-d H:i:s"),
        "Equipment.is_active"=>true
                        ),
  'limit' => 25,        
  'order' => array(
         'Equipment.available_time' => 'asc',

       ),
      'contain'=>array(
             "City"=>array(
               "fields"=>array("id","name"),
               "State"=>array("fields"=>"name")),

              )

And I see the proper queries running, but still can sort by State

1

1 Answers

0
votes

Cake does not join tables all the time like some SQL would normally be done, instead it does a select and then foreach's the rows from one query and the does a find for each iteration of the loop.

Install something like debug kit so that you can see what SQL is being done. If this is the case have a search on google for "cakephp deep finding", there is a few methods for doing this, one of which is forcing hasOne joins on the relations.

Cake might be loading an AppModel instance of the related model. You can check this with the pho method get_class iirc. Make sure nothing is app model, it should all be the actual class that you have created.

If there is not many queries you will need to investigate the relations you have created. (doubt this though, other 2 are more likely the cause)

Edit... How do I filter deep associations in CakePHP