编程语言
首页 > 编程语言> > php-如何在PHALCON FRAMEWORK中联接多个表

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

作者:互联网

我正在尝试在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;
      }

标签:phalcon,mysql,php
来源: https://codeday.me/bug/20191121/2053919.html