Morning,
I have 2 tables
Countries table
+------+-------------+
| ID | Negara |
+------+-------------+
| 1 | Indonesia |
| 2 | Malaysia |
+------+-------------+
Users Table
+------+-----------------+------------+
| ID | countrie_id | Nama |
+------+-----------------+------------+
| 1 | 1 | Arif |
| 2 | 1 | Jono |
| 3 | 2 | Mahmud |
| 4 | 2 | Abas |
+-------+----------------+------------+
with table method below
Table countries
$this->hasMany('Users', [
'foreignKey' => 'countrie_id'
]);
Table users
$this->belongsTo('Countries', [
'foreignKey' => 'countrie_id'
]);
I want to join 2 tables with type ‘INNER’, below CountriesController
public function testajax()
{
$queri = $this->Countries->find()
->select(['ID', 'Negara'])
->order(['Countries.Negara' => 'ASC'])
->contain(['Users'])
->join([
'u' => [
'table' => 'users',
'type' => 'INNER',
'conditions' => array('u.countrie_id = countries.id')
]
]);
$this->set('queri', $queri);
//dd($queri->sql());
//debug($queri);
}
and inside testajax.ctp
below this
<?php foreach ($queri as $countrie): ?>
<tr>
<td><?= h($countrie->ID)?></td>
<td><?= h($countrie->Negara)?></td>
<td><?= h($countrie->ID)?></td>
<td><?= h($countrie->Nama)?></td>
</tr>
<?php endforeach; ?>
The problem is reference field in table Users <td><?= h($countrie->Nama)?></td>
the result is null or empty.
I hope someone could help me, thanx