Sujan
Sujan

Reputation: 81

Join in cakephp not showing all the data

I'm trying to have this query

SELECT * FROM `rentprograms` AS `Rentprogram` 
    inner join `vehiclerentprograms` as  `Vehiclerentprogram` on `Vehiclerentprogram`.`rentprogramid` = `Rentprogram`.`id` 
    inner join `vehicles` AS `Vehicle` ON `Vehicle`.`id` =`Vehiclerentprogram`.`vehicleid` WHERE `Vehicle`.`id` = 1

Code in CakePHP

$this->Rentprogram->find('all'), array(
  'fields'=>array('*'),
  'joins' => array(
      array(
        'table' => 'vehiclerentprograms',
                'alias' => 'Vehiclerentprogram',
                'type'=>'inner',
                'conditions' => array(
                    'Vehiclerentprogram.rentprogramid' => 'Rentprogram.id',
                                )
                            ),
                array(
                'table' => 'vehicles',
                'alias' => 'Vehicle',
                'type'=>'inner',
                'conditions' => array(
                    'Vehicle.id' => 'Vehiclerentprogram.vehicleid',
                                )
                            )
                        ),
                    );

But it only display the value of Rentprogram. How can i have all the fields related to Rentprogram, Vehicle, Vehiclerentprogram.

Upvotes: 2

Views: 1335

Answers (2)

user361697
user361697

Reputation: 1131

You can use this method

$this->Rentprogram->find('all'), array(
 'fields' => array('Rentprogram.*', 'Vehicle.*', 'Vehiclerentprogram.*'), ...

Upvotes: 0

Raffaele
Raffaele

Reputation: 20885

There's no value in using an MVC framework and do the dirty joins by hand. You'd better use Cake conventions, which lets you access Cake's libraries and tools which in turn speed up the development process quite a lot. In this case you have to setup models and associations between models (I hope you have heard of has-many, belongs-to, many-to-many and so on).

CakePHP ships with an invaluable DAO layer and a code generator called bake. Once you design the database schema, forget about SQL and think in terms of your business objects. First, create three tables in MySQL (I used a minimal set of fields and deduced the structure from your query):

CREATE TABLE `programs` (
  `id` int(11) AUTO_INCREMENT,
  `start` datetime DEFAULT NULL,
  `end` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
);

CREATE TABLE `vehicles` (
  `id` int(11) AUTO_INCREMENT,
  `model` varchar(128) DEFAULT NULL,
  `plate` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
);

CREATE TABLE `vehicle_programs` (
  `id` int(11) AUTO_INCREMENT,
  `program_id` int(11) DEFAULT NULL,
  `vehicle_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
);

Then run the shell script:

Console/cake bake all

and select one table at a time (remember vehicle_programs must be the last one). This will generate all Models, Controllers and Views files for you. Then you can start filling your database with test data. Point your browser to http://host/vehicle_programs and put some vehicle and program in.

Finally I will show you how to retrieve all of the fields in one query. Suppose that you want to show everything when listing vehicle_programs. In the index() method of VehicleProgramsController you have to set $this->VehicleProgram->recursive to 1, so that it fetches related models fields as well. In the view index.ctp you'll now be able to access fields like

<?php
foreach ($vehiclePrograms as $vehicleProgram) {
  echo $vehicleProgram['Program']['start'];
  echo $vehicleProgram['VehicleProgram']['id'];
  echo $vehicleProgram['Vehicle']['plate'];
}

Note if we hadn't set Model->recursive to 1, Cake wouldn't have fetched fields of related models (Vehicle and Program) for us.


Incidentally, I think not setting the fields key at all should do the trick, since Cake reads everything by default. However, the correct solution is using relationships between model classes - when you run bake it puts the following in Model/Vehicle.php:

class Vehicle extends AppModel {

    public $hasMany = array(
        'VehicleProgram' => array(
            'className' => 'VehicleProgram',
            'foreignKey' => 'vehicle_id',
            'dependent' => false
        )
    );

}

and symmetric associations in Model/VehicleProgram.php

Upvotes: 1

Related Questions