gpt4 book ai didi

mysql - 从与 CakePhp 中的外键关联的两个表中检索数据

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

我有两个名为 login 和 userDetail 的表

                        Login

login_id
uname
pswd
userdetail_id

                       userdetails

userdetail_id
name
address
email

登录表在 userDetail 表中包含 userdetails_id。我想从登录表和 userDetail 表中获取所有数据并将其保存到变量中如果有人知道,请回答我......

最佳答案

首先你的表结构必须如下所示。

logins Table.
Id auto_increment
username
password
userDetails Table.
Id auto_increment
user_id
name
address
etc...

现在每个表的模型都是。

登录

<?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' => ''
)
}
?>

用户详细信息

<?php
class UserDetail extends AppModel
{
var $name = 'UserDetail';

var $belongsTo = array
(
'User' => array
(
'className' => 'User',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => ''
)
}
?>

最后在需要获取登录详细信息的 Controller 中。

$login_detail = $this->Login->find('all');

您将在结果 $login_detail 中看到 userDetail 表记录。使用

pr($login_detail);
在 Controller 中查看它的运行情况。

干杯。欢迎提问。

关于mysql - 从与 CakePhp 中的外键关联的两个表中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14517134/

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