我正在尝试在 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.
答案 0 :(得分:2)
感谢您的快速回复。
我实际上需要运行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;
}
答案 1 :(得分:1)
这没有经过测试,但它可能会给你一个想法,你实际上并不需要使用$ phql,因为模型应该能够容纳你需要的一切。
$users = User::find();
foreach($users as $user) {
// whatever you want
echo $user->name;
foreach($user->contactdetails as $contactDetails) {
echo $contactDetails->telephone;
}
}
您可以从文档中获取所有这些信息 - http://docs.phalconphp.com/en/latest/reference/models.html