We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How make "inner join" with Phalcon?

Hello! After so much analysis and to have used other frameworks I have had a real challenge and I have decided to use Phalcon and it has blown my head! Is so efficient that it leaves behind any other framework.

Well there is something that is not working for me and it is that I need to do "inner join" in my querys and I do not find the return. I have this structure: App / App / controllers / IndexController.php App / controllers / TUserController.php App / models / TUser.php

In the document https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Model_Manager.html Explains ModelsManager but I can not locate the code in the controller

To do this query: "select * from t_user"; Code fragments: <? Php

Use Phalcon \ Mvc \ Model \ Criteria;

Use Phalcon \ Paginator \ Adapter \ Model as Paginator;

Use Phalcon \ Di;

Use Phalcon \ Mvc \ Model \ Manager as ModelsManager;

Class TUserController extends ControllerBase { Public function indexAction () { $ Di = new Di ();

$ Di-> set ( "ModelsManager", Function () { Return new ModelsManager (); } ); $ TUser = new TUser ($ di);

$ Query = new Phalcon \ Mvc \ Model \ Query ("SELECT * FROM t_user", $ di); $ TUsers = $ query-> execute (); $ This-> persistent-> parameters = null; }

Well I seem to have responded by myself, but I swear I've done this before.

<?php

use LoginForm as FormLogin; use TUser as TUser; use Phalcon\Mvc\Model\Query; use Phalcon\Di; use Phalcon\Mvc\Model\Manager as ModelsManager;

class IndexController extends ControllerBase {

public function indexAction() {
    $email = $this->request->getPost('email', array('striptags', 'trim')); //, 'email'
    $password = $this->request->getPost('password', array('striptags', 'trim'));
    //OPTION 1 OK 
    // Instantiate the Query
    $query = new Query(
        "SELECT * FROM TUser", $this->getDI()
    );

// // Execute the query returning a result if any $TUser = $query->execute();

    //OPTION 2 OK
    $di = new Di();

    $di->set(
            "modelsManager", function() {
        return new ModelsManager();
    }
    );

// Executing a simple query $query = $this->modelsManager->createQuery("SELECT * FROM TUser"); $cars = $query->execute(); var_dump($cars); die;

edited Mar '17

Hi, to join SQL queries I use:


 $db = \Phalcon\DI::getDefault()->getShared('db');
 $query = $db->convertBoundParams('SELECT DISTINCT COUNT(table.id) AS ilosc from table JOIN table2 ON table.klient = :id: AND table2.id = :id:', ['id' => $id]);
  return $db->fetchAll($query['sql'], \Phalcon\Db::FETCH_OBJ, $query['params']);

It binds paramter and returns an object