1
votes

I have two models, Zones and Regions. I cannot figure out why Cake's find will not allow me to specify fields from the related tables.

These conditions, only requesting fields from the first table work fine.

$conditions = array('fields' => array('Zone.id','Zone.title','Zone.color'));

These conditions cause problems because... reasons?

$conditions = array('fields' => 
array('Zone.id','Zone.title','Zone.color','Region.id','Region.title'));

Basic query, this is how I'm using the conditions.

$result = $this->Zone->find('all',$conditions);

The SQL Cake is generating:

SELECT "Zone"."id" AS "Zone__id", "Zone"."title" AS "Zone__title", "Zone"."color" 
AS "Zone__color", "Region"."id" AS "Region__id", "Region"."title" AS "Region__title" 
FROM "public"."zones" AS "Zone" WHERE 1 = 1

Why is Cake assuming that the Region.fields are part of the Zones model? Why isn't this working as expected?

1
How are your models related? In your Zone model, is your Region added as a belongsTo, hasOne, or hasMany?Derek

1 Answers

2
votes

When you're doing this, you need to include both models in the find. Here is the working example I settled on:

$conditions = array('fields' => 
array('Zone.id','Zone.title','Zone.color','Region.id','Region.title'));

$this->Zone->Region->find('all',$conditions);