Error: SQLSTATE[42P01]: Undefined table: "Missing FROM-claouse entry for table

Error: SQLSTATE[42P01]: Undefined table: "Missing FROM-claouse entry for table

我使用 cakephp find 创建了一个连接查询,如下所示:

$test = $this->DiagnosisBind->find(
                'all',
                [
                    'table' => 'diagnosis_binds',
                    'alias' => 'dbinds',
                    'order' => 'dbinds.set_num ASC',
                    //'recursive'  => 0,
                    'conditions' => [
                        'dbinds.deleted IS NULL',
                        'dbinds.diagnosis_id ='.$diagnosisData[0][0]['id']
                    ],
                    'joins' => [
                        [
                            'type'  => 'INNER',
                            'table' => 'diagnosis_treats',
                            'alias' => 'dtreats',
                            'conditions' => [
                                'dtreats.diagnosis_bind_id = dbinds.id'                             
                            ]
                        ],
                        [
                            'type'  => 'INNER',
                            'table' => 'medical_masters',
                            'alias' => 'medmas',
                            'conditions' => [
                                'medmas.deleted IS NULL'
                            ]
                        ]
                    ],  
                    'fields' => [
                        'dtreats.id',
                        'dtreats.medical_name',
                        'dtreats.amount',
                        'dbinds.days'
                    ]
                ]
            );

执行查询时,我收到此错误:

我读过这个 post missing FROM-clause entry for table "Grupo" cakephp 解决方案是创建一个递归(我试过但失败了)并使用 containable(我不应该创建 containable)。那么我上面的 find 有什么问题。谢谢

注:

query builder finders 没有 tablealias 选项,它们仅适用于联接,因此您的主 table 将尽可能使用默认别名在错误消息的查询中看到,别名是 DiagnosisBind,因此使用 dbinds 将导致错误。

长话短说,使用 DiagnosisBind 而不是 dbinds

旁注,永远不要将数据注入单值条件(或在 key => value 条件的键中):

'dbinds.diagnosis_id ='.$diagnosisData[0][0]['id']

这可能是 SQL 注入漏洞!请始终使用 key => value 语法或绑定:

'DiagnosisBind.diagnosis_id' => $diagnosisData[0][0]['id']