Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with multiple joins to the same model #1803

Closed
karlisl opened this issue Jan 13, 2014 · 1 comment
Closed

Problem with multiple joins to the same model #1803

karlisl opened this issue Jan 13, 2014 · 1 comment
Labels
bug A bug report status: medium Medium

Comments

@karlisl
Copy link

karlisl commented Jan 13, 2014

QueryBuilder maps and returns data only from the last join:

$builder = $app->modelsManager->createBuilder()
        ->columns(array(
            'Jobs.*',
            'MasterRobot.*',
            'PeasantRobot.*'
        ))
        ->from('Jobs')
        ->leftJoin('Robots', 'MasterRobot.id = Jobs.master_robot_id', 'MasterRobot')
        ->leftJoin('Robots', 'PeasantRobot.id = Jobs.peasant_robot_id', 'PeasantRobot')
        ->getQuery()
        ->execute();

Generated query:

SELECT `jobs`.`id` AS `_jobs_id`, `jobs`.`master_robot_id` AS `_jobs_master_robot_id`, `jobs`.`peasant_robot_id` AS `_jobs_peasant_robot_id`, `jobs`.`title` AS `_jobs_title`, `PeasantRobot`.`id` AS `_PeasantRobot_id`, `PeasantRobot`.`name` AS `_PeasantRobot_name` FROM `jobs` LEFT JOIN `robots` AS `MasterRobot` ON `MasterRobot`.`id` = `jobs`.`master_robot_id`  LEFT JOIN `robots` AS `PeasantRobot` ON `PeasantRobot`.`id` = `jobs`.`peasant_robot_id`

You can see that MasterRobot columns are missing from SELECT block.

Returned data contains only PeasentRobot values:

...
[PeasantRobot] => Robots Object
        (
            [id] => 2
            [name] => Just Peasant
...

Also if you switch "MasterRobot" and "PeasantRobot" in createBuilder columns array, then returned data under the key "PeasentRobot" containts MasterRobot values:

...
[PeasantRobot] => Robots Object
        (
            [id] => 1
            [name] => Mister Chief
...

Robots model:

class Robots extends Model
{
    public $id;
    public $name;

    public function initialize() {
        $this->hasMany('id', 'Jobs', 'master_robot_id');
        $this->hasMany('id', 'Jobs', 'peasant_robot_id');
    }
}

Jobs model:

class Jobs extends Model
{
    public $id;
    public $master_robot_id;
    public $peasant_robot_id;

    public function initialize() {
        $this->belongsTo('master_robot_id', 'Robots', 'id', array('alias' => 'MasterRobot'));
        $this->belongsTo('peasant_robot_id', 'Robots', 'id', array('alias' => 'PeasantRobot'));
    }
}

Problem persists either relations are or aren't defined. Same thing happens when model aliases are present in relation definitions.

Tried in versions 1.2.4 and 1.3.0.

Update
Same thing with this:

$di = \Phalcon\DI::getDefault();
$mgr  = $di->get('modelsManager');
$phql = "
    SELECT Jobs.*, MasterRobot.*, PeasantRobot.*
        FROM Jobs AS Jobs
            LEFT JOIN Robots AS MasterRobot ON MasterRobot.id = Jobs.master_robot_id
            LEFT JOIN Robots AS PeasantRobot ON PeasantRobot.id = Jobs.peasant_robot_id
";

$mgr->executeQuery($phql);

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@maxgalbu
Copy link
Contributor

This was already reported, see #2113

@niden niden added bug A bug report status: medium Medium and removed Bug - Medium labels Dec 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug report status: medium Medium
Projects
None yet
Development

No branches or pull requests

4 participants