0
votes

hi i have aproblem in my code, I want generate a list of user but this have a group and need just a group of user. the error say:

Error: SQLSTATE[42P01]: Undefined table: 7 ERROR: missing FROM-clause entry for table "Grupo"

this is my code:

    public function add()
{
    $this->loadModel('SoyaProveedor');
    $this->loadModel('Soya');

    $this->set('oleaginosas', $this->Soya->find('list', array(
        'fields'=> array('id','username'),
        'conditions' => array('Grupo.categoria' => 'Soya' , 'Grupo.subcategoria' => 'Productor de Oleaginosas')
        )));

    if ($this->request->is('post')) {
    $this->request->data['SoyaProveedor']['nombre'] = strtoupper($this->request->data['SoyaProveedor']['nombre']);
    $this->request->data['SoyaProveedor']['codigo'] = strtoupper($this->request->data['SoyaProveedor']['codigo']);
    if ($this->SoyaProveedor->save($this->request->data)) {
        $this->Session->setFlash(__('La Información fue Guardada.'));
        return $this->redirect(array('action' => 'index'));
        }
    }
}

the sql query of the cake generate it:

SQL Query: SELECT "Soya"."id" AS "Soya__id", "Soya"."username" AS "Soya__username" FROM "public"."users" AS "Soya" WHERE "Grupo"."categoria" = 'Soya' AND "Grupo"."subcategoria" = 'Productor de Oleaginosas'

2
Your problem is that your conditions are on table Grupo while your find is on Soya. - Kai
What is the relation between Soya and Grupo? - Fazal Rasel
Soya is a user and user has a group - CoolLife
Please do not answer questions by editing them - write an answer and (when permitted) accept it. That said - you've found a very verbose/difficult/not-normal solution (and you're joining the users table again?). - AD7six

2 Answers

2
votes

You need the grupos table to be joined in the query, your query in the question has no joins. There are a number of simple solutions.

Define recursive.

Recursive is a very coarse control of what joins and queries are executed, by default find('list') has a recursive value of -1.

-1 means no joins, which is why there is no join in the resultant query. Setting it to a value of 0 adds a join to the main query for all hasOne and belongsTo associations.

Be wary of using/relying on recursive as it's very easy to generate queries with joins you don't need - and/or triggering many subsequent queries for related data (if set to a value larger than 0).

However this find call:

$data = $this->Soya->find('list', array(
    'fields'=> array('Soya.id','Soya.username'),
    'recursive' => 0, // added
    'conditions' => array(
        'Grupo.categoria' => 'Soya' , 
        'Grupo.subcategoria' => 'Productor de Oleaginosas'
    )
));

Should result in this query (If the Soya model has a belongsTo association to Grupo):

SELECT
    "Soya"."id" AS "Soya__id",
    "Soya"."username" AS "Soya__username"
FROM
    "public"."users" as "Soya"
LEFT JOIN
    "public"."Grupos" as "Grupo" on ("Soya"."grupo_id" = "Grupo"."id")
...
Possibly more joins
...
WHERE
   "Grupo"."categoria" = 'Soya' 
    AND 
    "Grupo"."subcategoria" = 'Productor de Oleaginosas'

Or Use containable

The containable behavior allows better control of what queries are executed. Given the info in the question to use it that means:

<?php

class Soya extends AppModel {
    // Assumed from information in the question
    public $useTable = 'users';

    public $belongsTo = array('Grupo');

    // added
    public $actsAs = array('Containable');

}

Will permit you to do the following in your controller:

$data = $this->Soya->find('list', array(
    'fields'=> array('Soya.id','Soya.username'),
    'contain' => array('Grupo'), // added
    'conditions' => array(
        'Grupo.categoria' => 'Soya' , 
        'Grupo.subcategoria' => 'Productor de Oleaginosas'
    )
));

Which will generate the following query (exactly one join):

SELECT
    "Soya"."id" AS "Soya__id",
    "Soya"."username" AS "Soya__username"
FROM
    "public"."users" as "Soya"
LEFT JOIN
    "public"."Grupos" as "Grupo" on ("Soya"."grupo_id" = "Grupo"."id")
WHERE
   "Grupo"."categoria" = 'Soya' 
    AND 
    "Grupo"."subcategoria" = 'Productor de Oleaginosas'
0
votes

Link your models together using associations: CakePHP Associations

Alternatively you can use custom sql-statemens using join e.g.:

$db = $this->getDataSource();
$result = $db->fetchAll(
            "SELECT Soya.id AS Soya__id, Soya.username AS Soya__username FROM public.users AS Soya 
            join Grupo on Grupo.id = Soya.groupo_id
            WHERE Grupo.categoria = ? AND Grupo.subcategoria = ?",
            array('Soya', 'Productor de Oleaginosas')
        );

$this->set('oleaginosas', $result);