I have three tables (Locals, 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
Programming table:
id | id_rutas
---------------------
1 1
2 2
I have the following associations:
In the Locales 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'
]);
i need to solve
In the edit.ctp of Programming, something like this should appear:
In the Schedules controller, I have the following in the Edit action:
public function edit($id = null)
{
$programacione = $this->Programaciones->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$programacione = $this->Programaciones->patchEntity($programacione, $this->request->getData());
if ($this->Programaciones->save($programacione)) {
$this->Flash->success(__('The programacione has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The programacione could not be saved. Please, try again.'));
}
$this->set(compact('programacione'));
// Enviando rutas
$tabla_rutas = TableRegistry::get('Rutas');
$query = $tabla_rutas->find('list', [
'keyField' => 'id_rutas',
'valueField' => ['id_local_origen','id_local_destino']
]);
$rutas = $query->toArray();
$this->set(compact('rutas', $rutas));
debug($rutas);
}
In debug I get this:
[
(int) 1 => '1;2',
(int) 2 => '2;3'
]
For this case we can declare one
virtual property
in the Entity.For this we add the function
_getRutaName()
inside the Route classRoute.php
In the Table we must tell it that its displayField is the new virtual property that we have just declared, following the CakePHP conventions it will be the name with "underscore"
In RoutesTable.php
And finally in the controller we call the array that we will display in our view, it is important that through the magic of the CakePHP query builder we call the contain with Origin and Destination so that they exist in
$this->_properties
the Entity.We
find
are going to pass as arguments to the parameters the'keyField' => 'id'
with the id of the route to be saved and with the'valueField' => 'ruta_name'
with our new virtualFieldSchedulesController.php
Finally in the view we call normally as we did before with CakePHP's FormHelper magic, I suggest removing the parameter
'empty' => true
since if it is chosen it will add asid
0 producing unexpected resultsThis will give us something similar to this: