gpt4 book ai didi

php - HABTM join 不会从 cakephp 中的关联数组产生任何结果

转载 作者:行者123 更新时间:2023-11-29 23:46:15 24 4
gpt4 key购买 nike

我正在尝试将 HABTM 模型与联接链接起来,但我没有从联接表中获取任何记录。
我有导师 id =2 的数据,并且在导师科目的其他表中也有这个 id 2,也是该科目的关键。我不应该在其他表中得到 NULL。我没有收到错误。

我想要导师 id =2 的所有科目。

这里是 Controller 和模型的关系

array(
(int) 0 => array(
'Subject' => array(
'id' => null,
'name' => null
),
'TutorsSubject' => array(
'id' => null,
'tutor_id' => null,
'subject_id' => null
),
'Tutor' => array(
'id' => '2',
'tutor_inactive' => false,
'first_name' => 'fred2',
'last_name' => 'blah',..........

class TutorsController extends AppController {

public function tutordetails() {
$options2['joins'] = array(
array('table' => 'tutors_subjects',
'alias' => 'TutorsSubject',
'type' => 'LEFT',
'conditions' => array(
'Tutor.id = TutorsSubject.tutor_id', //fixed 'Tutor.id = TutorsSubject.id', //
)
),

array('table' => 'subjects',
'alias' => 'Subject',
'type' => 'LEFT',
'conditions' => array(
'TutorsSubject.subject_id=Subject.id',
)
)

));

$options2['fields'] = array('Subject.*','TutorsSubject.*','Tutor.*');

$this->Tutor->recursive = -1;
$options2['conditions'] = array('Tutor.id' => 2);
$subject=$this->Tutor->find('all',$options2);
$this->set('subject', $subject);
debug($subject);


class Subject extends AppModel {
public $hasAndBelongsToMany = array(

'Tutor' => array(
'className' => 'Tutor',
'joinTable' => 'tutors_subjects',
'foreignKey' => 'subject_id',
'associationForeignKey' => 'tutor_id',
'unique' => 'keepExisting',
'conditions' => '',

)
);

}

class Tutor extends AppModel {
..

public $hasAndBelongsToMany = array(
'Subject' => array(
'className' => 'Subject',
'joinTable' => 'tutors_subjects',
'foreignKey' => 'tutor_id',
'associationForeignKey' => 'subject_id',
'unique' => 'keepExisting',
'conditions' => '',

),

);

UPDATE=AgRIZZO solved it and changed made above

最佳答案

Agrizzo 给出了答案:当您正确定义模型关系时使用 JOIN 是一个巨大的错误。使用框架来定义它的用途(并且您需要在没有 JOIN 的情况下得到很好的支持。)。话虽这么说 - 你的问题很可能是这样的:'

Tutor.id = TutorsSubject.id' 在您的第一个 JOIN 定义中。尝试一下

        'Tutor.id = TutorsSubject.tutor_id

关于php - HABTM join 不会从 cakephp 中的关联数组产生任何结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25925853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com