gpt4 book ai didi

php - Cakephp从多个表中获取数据

转载 作者:行者123 更新时间:2023-11-29 18:31:36 25 4
gpt4 key购买 nike

我是 cakephp 的新手,并尽力在 cakephp 中实现查询。我有这个查询

SELECT * FROM question join question_topic on question.question_id=question_topic.question_id join topic on topic.topic_id=question_topic.topic_id join user_topic on user_topic.topic_id=topic.topic_id where user_topic .user_id=10

我需要在 cakephp 中使用这个。

我尝试过的是这个

$this->Behaviors->attach('Containable');
return $this->find('all', array(

'contain' => array(
'User',
'UserInfo.UserTopic'=>array(
'conditions'=>array('UserTopic.user_id'=>10)
),
'QuestionAndTopic.Topic','UpVoteQuestion','Answer','Answer.UserInfo'

),


'order' => 'rand()',
'recursive' => 0
));

但是查询没有给我正确的结果。可以请您看一下吗?

问题.php

class Question extends AppModel
{
public $useTable = 'question';
public $primaryKey = 'question_id';


public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'fields' => array('User.user_id','User.email','User.active')

),
'UserInfo' => array(
'className' => 'UserInfo',
'foreignKey' => 'user_id',


)
);
public $hasMany = array(
'Answer' => array(
'className' => 'Answer',
'foreignKey' => 'question_id',


),

'QuestionAndTopic' => array(
'className' => 'QuestionAndTopic',
'foreignKey' => 'question_id',
)
);

public function latestQuestions(){
$this->Behaviors->attach('Containable');
return $this->find('all', array(

'contain' => array(
'User',
'UserInfo.UserTopic'=>array(
'conditions'=>array('UserTopic.user_id'=>10)
),
'QuestionAndTopic.Topic','UpVoteQuestion','Answer','Answer.UserInfo'

),


'order' => 'rand()',
'recursive' => 0
));
}
}

最佳答案

你可以使用Mysql查询,它的工作方式就像cakephp查询

$this->Behaviors->query('SELECT * FROM question join question_topic on question.question_id=question_topic.question_id join topic on topic.topic_id=question_topic.topic_id join user_topic on user_topic.topic_id=topic.topic_id where user_topic .user_id=10');

关于php - Cakephp从多个表中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45640884/

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