# How To Convert LEFT JOIN SQL Statement into CakePHP coding

**URL:** <https://discourse.cakephp.org/t/how-to-convert-left-join-sql-statement-into-cakephp-coding/7479>\
**Category:** Need Help\
**Created:** [March 16, 2020, 5:10pm UTC](https://discourse.cakephp.org/t/how-to-convert-left-join-sql-statement-into-cakephp-coding/7479 "2020-03-16T17:10:44Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![dstudio101](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dstudio101/32/1713_2.png) [@dstudio101](https://discourse.cakephp.org/u/dstudio101)\
**Post date:** [March 16, 2020, 5:10pm UTC](https://discourse.cakephp.org/t/how-to-convert-left-join-sql-statement-into-cakephp-coding/7479/1 "2020-03-16T17:10:44Z")

</div>

This is the statement I would like to use convert using CakePHP

```
SELECT appearances.member_id, members.name, appearances.created
FROM appearances
LEFT JOIN members ON members.mbrid = appearances.member_id

```

In the list of appearances (templates/Appearances/index.php) I can display the name of the members instead of just the member\_id. And of course other fields in the members table if I need to.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 16, 2020, 6:42pm UTC](https://discourse.cakephp.org/t/how-to-convert-left-join-sql-statement-into-cakephp-coding/7479/2 "2020-03-16T18:42:22Z")

</div>

What does your index function look like? If the table associations are all set up correctly, it should be a simple matter of [containing](https://book.cakephp.org/3/en/orm/retrieving-data-and-resultsets.html#eager-loading-associations-via-contain) the Members association, and then your template will have something like `$appearance->member->name` instead of `$appearance->member_id`.

---

<div class="post-metadata">

**Author:** ![dstudio101](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dstudio101/32/1713_2.png) [@dstudio101](https://discourse.cakephp.org/u/dstudio101)\
**Post date:** [March 17, 2020, 12:55pm UTC](https://discourse.cakephp.org/t/how-to-convert-left-join-sql-statement-into-cakephp-coding/7479/3 "2020-03-17T12:55:07Z")

</div>

> [@Zuluru](#):
>
> If the table associations are all set up correctly

This is what my table class looks like. Please note, the foreign key is not members table’s primary key.

```
class AppearancesTable extends Table
{
    public function initialize(array $config): void
    {
        $this->belongsTo('Members', [
            'foreignKey' => 'mbrid'
        ]);
        $this->addBehavior('Timestamp');
    }
}

```

This is what my table association looks like. My index.php resulted in:  
`Trying to get property 'mbr_name' of non-object`_mbr\_name_ is the name in the members table.

This is the code I used in the index.php  
`<?= $appearance->member->mbr_name ?>`

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 17, 2020, 2:25pm UTC](https://discourse.cakephp.org/t/how-to-convert-left-join-sql-statement-into-cakephp-coding/7479/4 "2020-03-17T14:25:14Z")

</div>

Did you add the containment to where you’re reading the list of appearances?

---

<div class="post-metadata">

**Author:** ![dstudio101](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dstudio101/32/1713_2.png) [@dstudio101](https://discourse.cakephp.org/u/dstudio101)\
**Post date:** [March 18, 2020, 4:36am UTC](https://discourse.cakephp.org/t/how-to-convert-left-join-sql-statement-into-cakephp-coding/7479/5 "2020-03-18T04:36:06Z")

</div>

That’s it. done and it works. Great. I added it on the controller file

```
public function index()
  {
      $this->loadComponent('Paginator');
      $appearances = $this->Paginator->paginate($this->Appearances->find('all', [
        'order' => ['Appearances.created' => 'DESC'],
        'contain' => ['Members']
      ]));
      $this->set(compact('appearances'));
  }
```

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 18, 2020, 7:26am UTC](https://discourse.cakephp.org/t/how-to-convert-left-join-sql-statement-into-cakephp-coding/7479/6 "2020-03-18T07:26:57Z")

</div>

That looks exactly right.

---

<div class="post-metadata">

**Author:** ![dstudio101](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dstudio101/32/1713_2.png) [@dstudio101](https://discourse.cakephp.org/u/dstudio101)\
**Post date:** [March 18, 2020, 8:48am UTC](https://discourse.cakephp.org/t/how-to-convert-left-join-sql-statement-into-cakephp-coding/7479/7 "2020-03-18T08:48:36Z")

</div>

```
SELECT appearances.member_id, members.name, appearances.created, batches.grade_id
    FROM appearances 
    LEFT JOIN members ON members.mbrid = appearances.member_id
    LEFT JOIN batches ON members.mbrid = batches.member_id
    WHERE batches.no = 1 AND (batch.subno = 0 OR batch.subno = 1)

```

How do I manage this kind of SQL?

1. For MembersTable: ` $this->hasMany('Batches')->setForeignKey('member_id');`

2. For AppearancesController::index()

3. For index.php: ` <?= $visit->member->batch->grade_id ?>`

But I get this error message: `Notice (8): Trying to get property 'grade_id' of non-object` in the display page.

And I still haven’t add a WHERE clause in the code – not sure where to put this…

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 18, 2020, 4:51pm UTC](https://discourse.cakephp.org/t/how-to-convert-left-join-sql-statement-into-cakephp-coding/7479/8 "2020-03-18T16:51:37Z")

</div>

Since members `hasMany` batches, it’s not `$visit->member->batch`, it’s `$visit->member->batches`, and it’s an array.

To add the `where` clause, I think you may need to convert your query to use the functional mode instead of array parameters, something like this (untested):

```auto
$query = $this->Visits->find()
    ->contain([
        'Members' => ['Batches' => [
            'queryBuilder' => function (Query $q) {
                return $q->where(['batches.no' => 1, 'OR' => ['batch.subno' => 0, 'batch.subno' => 1]]);
            },
        ]],
    ])
    ->order(['Visits.created' => 'DESC']);
$visits = $this->Paginator->paginate($query);

```

Quite possible that this can work with the array method too, I just never do it that way; I find the functional approach more flexible and expressive. Here’s [the documentation](https://book.cakephp.org/3/en/orm/query-builder.html#passing-conditions-to-contain) on passing conditions to contain.

---

<div class="post-metadata">

**Author:** ![dstudio101](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dstudio101/32/1713_2.png) [@dstudio101](https://discourse.cakephp.org/u/dstudio101)\
**Post date:** [March 19, 2020, 8:41am UTC](https://discourse.cakephp.org/t/how-to-convert-left-join-sql-statement-into-cakephp-coding/7479/9 "2020-03-19T08:41:34Z")

</div>

Hi,

I tried the code above. I’m not sure if it’s right but in the debug, I get two (2) SQL statements instead of only one which is this:

```auto
SELECT visits.member_id, members.mbr_name, visits.created, batches.grade_id
FROM visits
LEFT JOIN members ON members.mbrid = visits.member_id
LEFT JOIN batches ON batches.member_id = members.mbrid
WHERE batches.batch = '2020' AND (batches.sub_batch = '0' OR batches.sub_batch = '1')
ORDER BY visits.created DESC

```

DEBUG SQL RESULTS

```auto
SELECT 
  Visits.id AS Visits__id, 
  Visits.member_id AS Visits__member_id, 
  Visits.vst_datetime AS Visits__vst_datetime, 
  Visits.location_id AS Visits__location_id, 
  Visits.created AS Visits__created, 
  Visits.modified AS Visits__modified, 
  Members.id AS Members__id, 
  Members.mbrid AS Members__mbrid, 
  Members.mbr_name AS Members__mbr_name, 
  Members.mbr_type AS Members__mbr_type, 
  Members.created AS Members__created, 
  Members.modified AS Members__modified 
FROM 
  visits Visits 
  LEFT JOIN members Members ON Members.mbrid = (Visits.member_id) 
ORDER BY 
  Visits.created DESC 
LIMIT 
  20 OFFSET 0

```

SECOND STATEMENT

```auto
SELECT 
  Batches.member_id AS Batches__member_id 
FROM 
  batches Batches 
WHERE 
  (
    Batches.member_id in (
      187, 1471, 1470, 1463, 250, 350, 1000, 
      501, 300, 255, 254, 253, 3, 303, 215, 
      305, 202, 201
    ) 
    AND Members.Batches.batch = '0' 
    AND Members.Batches.sub_batch = '0'
  )

```

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 19, 2020, 3:55pm UTC](https://discourse.cakephp.org/t/how-to-convert-left-join-sql-statement-into-cakephp-coding/7479/10 "2020-03-19T15:55:55Z")

</div>

This is as expected for a `hasMany` relationship. Each visit will have a member, each member will have an array of batches.

---

<div class="post-metadata">

**Author:** ![dstudio101](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dstudio101/32/1713_2.png) [@dstudio101](https://discourse.cakephp.org/u/dstudio101)\
**Post date:** [March 19, 2020, 8:02pm UTC](https://discourse.cakephp.org/t/how-to-convert-left-join-sql-statement-into-cakephp-coding/7479/11 "2020-03-19T20:02:59Z")

</div>

I know, but in this case, I just want the result to be:

`visits.member_id, members.name, visits.created,batches.grade_id`  
ID | NAME | DATE OF VISIT | GRADE

For the grade\_id, I got this error message: `Notice (8): Trying to get property 'grade_id' of non-object`  
template-\>index.php  
code to display grade: `<?= $visit->member->batch->grade_id ?>`

Controller-\> find() code used

```auto
$this->Visits->find()
      ->contain([
        'Members' => ['Batches' => [
            'queryBuilder' => function (Query $q) {
                return $q->where(['Batches.batch' => 1, 'OR' => ['Batches.sub_batch' => '0', 'Batches.sub_batch' => '1']]);
            },
        ]],
    ])
    ->order(['Visits.created' => 'DESC']));

```

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 19, 2020, 8:44pm UTC](https://discourse.cakephp.org/t/how-to-convert-left-join-sql-statement-into-cakephp-coding/7479/12 "2020-03-19T20:44:35Z")

</div>

Again, as written, there’s no `batch` property on `member`, it’s `batches`. If you absolutely cannot deal with an array in there, and are instead wanting to get 5 copies of the visit and member data if there’s 5 batches that go with it, then follow the suggestion you got on StackOverflow.

---

<div class="post-metadata">

**Author:** ![dstudio101](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dstudio101/32/1713_2.png) [@dstudio101](https://discourse.cakephp.org/u/dstudio101)\
**Post date:** [March 20, 2020, 8:54am UTC](https://discourse.cakephp.org/t/how-to-convert-left-join-sql-statement-into-cakephp-coding/7479/13 "2020-03-20T08:54:19Z")

</div>

This code seem to get me the right SQL I’m looking for:

```auto
$visits = $this->Paginator->paginate($this->Visits->find()
        ->select(['Members.mbrid', 'Members.mbr_name', 'Visits.created', 'Batches.grade_id'])
        ->leftJoinWith('Members')
        ->leftJoinWith('Members.Batches')
        ->where(['Batches.batch' => '2020', 'Batches.sub_batch' => '0'])
        ->order(['Visits.created' => 'DESC'])
    );

```

What’s the code to use in index.php to access the data:

\<?= $visit-\>member\_id ?\> doesn't seem to work...

`Notice (8): Trying to get property 'mbrid' of non-object [ROOT/templates/Visits/index.php, line 20]` is the error message.

In Debug mode, there is one variable visits and below is the content of one visits row

```auto
1 (App\Model\Entity\Visit)

    created (Cake\I18n\FrozenTime)
    _matchingData (array)
        Members (array)
            mbrid 250
            mbr_name John Doe
        Batches (array)
            grade_id gr7

```

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 20, 2020, 2:44pm UTC](https://discourse.cakephp.org/t/how-to-convert-left-join-sql-statement-into-cakephp-coding/7479/14 "2020-03-20T14:44:56Z")

</div>

I thought `_matchingData` was only present when you use the `->matching()` call in your query. Couldn’t tell you why it’s showing up here.

---

<div class="post-metadata">

**Author:** ![dstudio101](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dstudio101/32/1713_2.png) [@dstudio101](https://discourse.cakephp.org/u/dstudio101)\
**Post date:** [March 20, 2020, 3:13pm UTC](https://discourse.cakephp.org/t/how-to-convert-left-join-sql-statement-into-cakephp-coding/7479/15 "2020-03-20T15:13:00Z")

</div>

The SQL generated is exactly what I needed:

```auto
SELECT 
  Members.mbrid AS Members__mbrid, 
  Members.mbr_name AS Members__mbr_name, 
  Visits.created AS Visits__created, 
  Batches.grade_id AS Batches__grade_id 
FROM 
  visits Visits 
  LEFT JOIN members Members ON Members.mbrid = (Visits.member_id) 
  LEFT JOIN batches Batches ON Members.id = (Batches.member_id) 
WHERE 
  (
    Batches.batch = '2020' 
    AND Batches.sub_batch = '0'
  ) 
ORDER BY 
  Visits.created DESC 

```

I run this query in phpmyadmin and I got the result I need. The problem is I don’t know how to access them…  
`<?= $visit->created->format(DATE_RFC850) ?>` this code is the only code that displays the correct data. The others gave me the error message.  
`Trying to get property 'xx' of non-object`—

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 20, 2020, 4:42pm UTC](https://discourse.cakephp.org/t/how-to-convert-left-join-sql-statement-into-cakephp-coding/7479/16 "2020-03-20T16:42:46Z")

</div>

Looking at the structure of the `$visit` variable you showed, it would seem that `$visit->_matchingData['Members']['mbrid']` would work. I have no idea why it’s coming in that structure.
