gpt4 book ai didi

php - 如何在 PHALCON FRAMEWORK 中连接多个表

转载 作者:可可西里 更新时间:2023-11-01 08:16:53 29 4
gpt4 key购买 nike

我正在尝试在 Phalcon 框架 中连接两个表 (MYSQL)。

”名称:

user
contact_details

模型”名称:

User
ContactDetails

初始化:

In "User" model ----->$this->hasMany("id", "ContactDetails", "user_id"); 
In "ContactDetails" model----->$this->belongsTo("user_id", "User", "id");

我的代码:

public function userDetails(){             
$phql = "SELECT User.*, ContactDetails.* FROM User LEFT JOIN ContactDetails";
$user = $this->modelsManager->executeQuery($phql);
-------
-------Remaining code---------
--------------
}
Thanks in advance.

最佳答案

感谢您的快速回复。

我实际上需要运行 MySQL 查询而不是使用内置函数。

这是我最终得到的修复:

$query = "SELECT u.*, c.* FROM user u "
. "LEFT JOIN ContactDetails c "
. "ON c.user_id = u.id ORDER BY u.id ";
$cars = $this->modelsManager->executeQuery($query);
$user = array();
foreach ($cars as $car) {
//print_r($car);exit;
$user[$car->u->id]['firstname'] = $car->u->firstname;
$user[$car->u->id]['lastname'] = $car->u->lastname;
$user[$car->u->id]['username'] = $car->u->username;
$user[$car->u->id]['emailid'] = $car->u->emailid;
$user[$car->u->id]['address'] = $car->c->address;
$user[$car->u->id]['street'] = $car->c->street;
$user[$car->u->id]['city'] = $car->c->city;
}

关于php - 如何在 PHALCON FRAMEWORK 中连接多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23739405/

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