表“Grupo”的缺失FROM子句条目。 cakephp [英] missing FROM-clause entry for table "Grupo" cakephp

查看:250
本文介绍了表“Grupo”的缺失FROM子句条目。 cakephp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:


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

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

这是我的代码:

    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查询:SELECTSoya。idASSoya__id,Soya。 b $ bSoya__usernameFROMpublic。usersASSoyaWHERE
Grupo。categoria='Soya'ANDGrupo。subcategoria='Productor
de Oleaginosas'

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'


推荐答案

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

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.

递归是一个非常粗略的控制连接并执行查询,默认 find('list') 的递归值为-1

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

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

-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.

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

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).

但是,这会找到调用:

$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模型有belongsTo关联到Grupo):

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

}

将允许您在控制器中执行以下操作: / p>

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'

这篇关于表“Grupo”的缺失FROM子句条目。 cakephp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆