Reputation: 75
I want to get the details of users from the users table using the ids in another table called virtualwh. The result that displays from the users table is not according to the winners id in the virtualwh.
I created this two tables(Virtualwh and users) and did the association linking using belongsTo (virtualwh => users). On the virtualwh table, there is a column(called winners which is connected to the users table using foreignkey) on each row that stores an id of a single user which is randomly selected and included from another table. I am trying to get all the ids in the virtualwh and query the Users table for the details of the user with the ids in order to display all their details in a page. This is what I have done but not working. Any help here?
/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);
users table structure
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;
Upvotes: 0
Views: 869
Reputation: 2625
If you want to belongsto on the fly then use like this
For 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);
For cakephp 2.x
$this->Virtualwarehouse->bindModel([
'belongsTo' => [
'User'=>[
'foreignKey' => 'winner',
'type'=>'INNER'
]
]
]);
$winners = $this->Virtualwarehouse->find('all');
$this->set('winnerslist',$winners);
print_r($winners);
Upvotes: 0
Reputation: 3944
Try the following codes if you are using CakePhp 3.x version
$winners = $this->Virtualwarehouse->find('all')->contain('User')->where(['Virtualwarehouse.winner !=' => 'null']);
For version CakePhp 2.x
$this->Virtualwarehouse->recursive = 1;
$winners = $this->Virtualwarehouse->find('all', array('conditions'=>array('Virtualwarehouse.winner !=' => 'null')));
print_r($winners);
Make sure in Virtualwarehouse model file you have connection with user table with foreign key column name
Upvotes: 0