从与CakePp中的forign键关联的两个表中检索数据 [英] Retrieving Data From two tables that are associated with a forign key in CakePhp
问题描述
我有两个名为login和userDetail的表
登录
login_id
uname
pswd
userdetail_id
和
userdetails
userdetail_id
name
address
email
登录表在userDetail表中包含userdetails_id。我想从Login表和userDetail表中获取所有数据并将其保存到变量
如果有人知道,请回答我......
首先,您的表结构必须如下所示。
Id auto_increment
username
password
userDetails表。
Id auto_increment
user_id
name
address
etc ...
$ b b现在每个表格的模型都是。
登录
<?php
class登录extends AppModel
{
var $ name ='User';
var $ hasMany = array
(
'UserDetail'=> array
(
'className'=>'UserDetail',
'foreignKey'=>'user_id',
'dependent'=> false,
'conditions'=>'',
'fields'=&
'order'=>'',
'limit'=>'',
'offset'=>',
'exclusive'=> ',
'finderQuery'=>'',
'counterQuery'=>''
)
}
?>UserDetail
<?php
class UserDetail extends AppModel
{
var $ name ='UserDetail';
var $ belongsTo = array
$ b'User'=> array
(
'className'=>'User',
'foreignKey'=>'user_id',
'dependent' > false,
'conditions'=> '',
'fields'=> '',
'order'=> '',
'limit'=> ''
)
}
?>需要获取登录详细信息。
$ login_detail = $ this-> Login-> find('all');
您将在结果
$ login_detail
中看到userDetail表记录。
在控制器中使用pr($ login_detail);
b
$ b干杯。
随意问。I have two tables named login and userDetail
Login login_id uname pswd userdetail_id
and
userdetails userdetail_id name address email
the login table contain userdetails_id in the userDetail table. i want to get all data from Login table and userDetail table and save it to a variable if anyone knows, please answer me......
解决方案First of all your table structure must be as below.
logins Table. Id auto_increment username password
userDetails Table. Id auto_increment user_id name address etc...
Now model for each table would be.
Login
<?php class Login extends AppModel { var $name = 'User'; var $hasMany = array ( 'UserDetail' => array ( 'className' => 'UserDetail', 'foreignKey' => 'user_id', 'dependent' => false, 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'exclusive' => '', 'finderQuery' => '', 'counterQuery' => '' ) } ?>
UserDetail
<?php class UserDetail extends AppModel { var $name = 'UserDetail'; var $belongsTo = array ( 'User' => array ( 'className' => 'User', 'foreignKey' => 'user_id', 'dependent' => false, 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '' ) } ?>
And finally in controller where you need to fetch login detail.
$login_detail = $this->Login->find('all');
You will see userDetail table records in resulting
$login_detail
. usepr($login_detail);
in controller to see it in action.
Cheers. Feel Free to ask.
这篇关于从与CakePp中的forign键关联的两个表中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!