Downloading models using INNER JOIN

Say I have Phalcon\Mvc\Modelone that I upload using ::findFirst($id).

How can I swap a custom query that will load a model row and do an INNER JOIN in another table?

Thanks!

+4
source share
3 answers

I'm sure you can use the query builder for simple joins, for example:

<?php

//Getting a whole set
$robots = $this->modelsManager->createBuilder()
    ->from('Robots')
    ->join('RobotsParts')
    ->orderBy('Robots.name')
    ->getQuery()
    ->execute();

//Getting the first row
$robots = $this->modelsManager->createBuilder()
    ->from('Robots')
    ->join('RobotsParts')
    ->orderBy('Robots.name')
    ->getQuery()
    ->getSingleResult();

Or a PHQL example from the documentation:

<?php

$phql = "SELECT Robots.*
    FROM Robots JOIN RobotsParts p
    ORDER BY Robots.name LIMIT 20";
$result = $manager->executeQuery($phql);

The default is INNER JOIN. However, you can specify the JOIN type in the request.

Link: http://docs.phalconphp.com/en/latest/reference/phql.html#creating-queries-using-the-query-builder

findFirst() , .

+4

, Model, Phalcon MVC Model.

    $followingUsers = Users::query()
        ->leftJoin('Common\WideZike\Models\UsersFollowers', 'Common\WideZike\Models\Users.id = Common\WideZike\Models\UsersFollowers.followingId')
        ->where('Common\WideZike\Models\UsersFollowers.followerId = :userId:', array('userId' => $user->getId()))
        ->orderBy('Common\WideZike\Models\UsersFollowers.addedDate DESC')
        ->execute();

, !

+2
$activations = UserActivations::query()
    ->columns("UserActivations.id")
    ->leftJoin("Deals", "d.id = UserActivations.dealId", "d") 
    ->where("UserActivations.state = 1")
    ->execute();
0
source

Source: https://habr.com/ru/post/1526449/


All Articles