我想使用另一个名为virtualwh的表中的id来从users表中获取用户的详细信息。从users表显示的结果不是根据virtualwh中的获胜者ID。
我创建了这两个表(Virtualwh和用户)并使用belongsTo(virtualwh => users)进行关联链接。在virtualwh表上,在每一行上都有一列(使用foreignkey连接到users表的winners),该列存储从另一个表中随机选择并包含的单个用户的id。我试图获取virtualwh中的所有ID并查询Users表以获取具有id的用户的详细信息,以便在页面中显示其所有详细信息。这就是我所做的,但没有奏效。这里有什么帮助吗?
/get winnerslist
$winners = $this->Virtualwarehouse->find('all', array('conditions'=>array('Virtualwarehouse.winner !=' => 'null')));
$winneroptions = array('conditions'=>array('User.id'=>$winners['Virtualwarehouse']['winner']), array('User.email','User.fname', 'User.path', 'User.pix'));
$winnersrecord = $this->Virtualwarehouse->User->find('all',$winneroptions);
$this->set('winnerslist',$winners);
print_r($winners);
用户表结构
DROP TABLE IF EXISTS `users`;
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL DEFAULT '0',
`path` varchar(20) DEFAULT NULL,
`pix` varchar(200) DEFAULT NULL,
`activation` varchar(32) NOT NULL,
`pass_token` varchar(45) DEFAULT NULL,
`status` int(2) DEFAULT NULL,
`role_id` int(20) DEFAULT NULL,
`fname` varchar(100) DEFAULT NULL,
`lname` varchar(100) DEFAULT NULL,
`gender` enum('Female','Male','Others','') DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`phone_number` varchar(15) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
`address` varchar(150) DEFAULT NULL,
`state` varchar(20) DEFAULT NULL,
`birthday` varchar(50) DEFAULT NULL,
`referer` varchar(200) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
virtualwh table
DROP TABLE IF EXISTS `virtualwh`;
CREATE TABLE IF NOT EXISTS `virtualwh` (
`id` int(11) NOT NULL DEFAULT '0',
`ad_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`alignment` tinyint(4) NOT NULL COMMENT '1=top, 2=bottom',
`image` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`path` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`points` varchar(5) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '20',
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1 = Active, 0 = Inactive',
`winner` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
答案 0 :(得分:0)
如果您使用CakePhp 3.x版
,请尝试以下代码$winners = $this->Virtualwarehouse->find('all')->contain('User')->where(['Virtualwarehouse.winner !=' => 'null']);
对于CakePhp 2.x版本
$this->Virtualwarehouse->recursive = 1;
$winners = $this->Virtualwarehouse->find('all', array('conditions'=>array('Virtualwarehouse.winner !=' => 'null')));
print_r($winners);
确保在 Virtualwarehouse 模型文件中与用户表连接,并使用外键列名称
答案 1 :(得分:0)
如果你想在飞行中属于那么就像这样使用
对于cakephp 3.x
$this->Virtualwarehouse->belongsTo('User',[
'foreignKey' => 'winner',
'joinType' => 'INNER'
]);
$winners = $this->Virtualwarehouse->find('all', [
'contain'=>['User']
]);
$this->set('winnerslist',$winners);
print_r($winners);
对于cakephp 2.x
$this->Virtualwarehouse->bindModel([
'belongsTo' => [
'User'=>[
'foreignKey' => 'winner',
'type'=>'INNER'
]
]
]);
$winners = $this->Virtualwarehouse->find('all');
$this->set('winnerslist',$winners);
print_r($winners);