Left John of Phpactiverecord using Codeigniter does not work

Asked

Viewed 66 times

1

I’m using Phpactiverecord’s left Join to get data from companies and users, but it’s not working. I use the users and Companies tables as relationship table proposals. I want to get the user data in foreach, but am not getting.

Model class Proposal.php

<?php

class Proposal extends ActiveRecord\Model
{

    static $belongs_to = array(
       array('company'),
       array('user'),
    );

}

Class controller proposals.php

public function index()
{
    $join = 'JOIN users u ON(proposals.vendedor_id = u.id)';
    $proposals = Proposal::all(array('joins' => $join));

    foreach ($proposals as $proposal) {
        echo 'vendedor_id -> ' . $proposal->vendedor_id .
             '<br>company -> ' . $proposal->company->name . 
             '<br>user -> ' . $proposal->user->username . '<br>';
    }

    exit();
}

My answer is:

sellers_id -> 1, company -> new company test, user -> (does not return user data)

I would like to understand the reason for not returning user data.

MER from my Mysql database:

Tabela users Tabela proposals Tabela companies

Source: https://phpactiverecord.xyz/projects/main/wiki/Finders/

  • Welcome to Sopt, I recommend meeting our tour. When creating questions on the site, always try to write all the information in the question, thus avoiding the use of images. Leave them to extremely specific cases.

  • Write the code and the output of it in the question. Always supplementing with Minimum, Complete and Verifiable.

  • I was able to reach the expected result using Function: public Function index() { $proposals = Proposal::find_by_sql('select p.*, u.firstname from proposals p INNER Join users u on p.vendedor_id = u.id'); foreach ($proposals as $Proposal) { echo 'vendedor_id -> ' . $Proposal->vendedor_id . '<br>company -> ' . $Proposal->company->name . '<br>user -> ' . $Proposal->firstname . '<br>'; } Exit(); }

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.