I have three tables (Locations, Routes and Schedules):
Local Table:
id | nombreLocal
--------------------------
1 Local Uno
2 Local Dos
3 Local Tres
Routes Table:
id | id_local_origen | id_local_destino
--------------------------------------------------
1 1 2
2 2 3
Schedules table:
id | id_rutas
---------------------
1 1
2 2
I have the following associations:
In the Locals table:
$this->hasMany('Rutas', [
'foreignKey' => 'id_local_origen'
]);
$this->hasMany('Rutas', [
'foreignKey' => 'id_local_destino'
]);
In the Routes table:
$this->belongsTo('Origen', [
'foreignKey' => 'id_local_origen',
'className' => 'Locales',
'joinType' => 'INNER'
]);
$this->belongsTo('Destino', [
'foreignKey' => 'id_local_destino',
'className' => 'Locales',
'joinType' => 'INNER'
]);
$this->hasMany('Programaciones', [
'foreignKey' => 'id_rutas'
]);
In the Schedules table:
$this->belongsTo('Rutas', [
'foreignKey' => 'id_rutas',
'joinType' => 'INNER'
]);
What I need for now friends please, is to solve using the CakePHP 3.6 ORM the following in the view.ctp
In the index.ctp of Programming something like this should come out:
id | Origen | Destino
------------------------------------
1 Local Uno Local Dos
2 Local Dos Local Tres
============== In the Schedules controller I have this:
public function view($id = null)
{
$programacione = $this->Programaciones->get($id, [
'contain' => ['Rutas']
]);
$this->set('programacione', $programacione);
}
**
In other words, I have tables A, B, C A is already related to B and vice versa. B is already related to C and vice versa. What I would like to know please is how can I get fields from table A, being in table C?
**
CakePHP allows us to do nesting in contains, and since you've already done most of the work declaring the relationships, you can call them like this:
In your SettingsController
And in the view however you have it, you can use them like this to get the result you want
In your index.ctp
Which will print something similar to this: