missing FROM-clause entry for table “Grupo” cakephp

前端 未结 2 668
[愿得一人]
[愿得一人] 2020-12-22 06:50

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[42P

2条回答
  •  伪装坚强ぢ
    2020-12-22 07:26

    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:

    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'
    

提交回复
热议问题