表格#34; Grupo"缺少FROM子句条目CakePHP的

时间:2014-04-22 20:55:07

标签: postgresql cakephp

嗨我在我的代码中有问题,我想生成一个用户列表,但这有一个组,只需要一组用户。 错误说:

  

错误:SQLSTATE [42P01]:未定义的表:7错误:缺少表“Grupo”的FROM子句条目

这是我的代码:

    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'));
        }
    }
}

蛋糕的sql查询生成它:

  

SQL查询:SELECT“Soya”。“id”AS“Soya__id”,“Soya”。“username”AS   “Soya__username”FROM“public”。“users”AS“Soya”WHERE   “Grupo”。“categoria”=“Soya”和“Grupo”。“subcategoria”='制作人   de Oleaginosas'

2 个答案:

答案 0 :(得分:1)

您需要在查询中加入grupos表,您在问题中的查询没有连接。有许多简单的解决方案。

定义递归。

递归是对连接和查询执行的非常粗略的控制,默认情况下为find('list') has a recursive value of -1

-1表示没有连接,这就是结果查询中没有连接的原因。将其设置为值0会为所有hasOne和belongsTo关联的主查询添加联接。

警惕使用/依赖递归,因为使用您不需要的连接生成查询非常容易 - 和/或触发相关数据的许多后续查询(如果设置为大于0的值)。

然而,这个发现电话:

$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'
    )
));

应该导致此查询(如果 Soya模型与Grupo有belongsTo关联):

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'

或使用可包含的

containable behavior可以更好地控制执行的查询。鉴于问题中的信息使用它意味着:

<?php

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

    public $belongsTo = array('Grupo');

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

}

允许您在控制器中执行以下操作:

$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'
    )
));

这将生成以下查询(恰好一个连接):

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'

答案 1 :(得分:0)

使用关联将您的模型链接在一起:CakePHP Associations

或者,您可以使用加号来使用custom sql-statemens

$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);