I have four tables (Locations, Routes, Routes and Schedules):
Local Table:
id | nombreLocal
--------------------------
1 Local Uno
2 Local Dos
3 Local Tres
Routes Table:
id | id_ruta_origen | id_ruta_destino
--------------------------------------------------
1 1 2
2 2 3
Routes table:
id | id_trayecto_origen | id_trayecto_destino
--------------------------------------------------------
1 1 3
2 3 1
Schedules table:
id | id_rutas | id_trayectos
------------------------------------
1 1 1
2 2 1
I have the following associations:
In the Locals table:
$this->hasMany('Rutas', ['foreignKey' => 'id_ruta_origen']);
$this->hasMany('Rutas', ['foreignKey' => 'id_ruta_destino']);
$this->hasMany('Trayectos', ['foreignKey' => 'id_trayecto_origen']);
$this->hasMany('Trayectos', ['foreignKey' => 'id_trayecto_destino']);
In the Routes table:
$this->belongsTo('Origen', [
'foreignKey' => 'id_ruta_origen',
'className' => 'Locales',
'joinType' => 'INNER'
]);
$this->belongsTo('Destino', [
'foreignKey' => 'id_ruta_destino',
'className' => 'Locales',
'joinType' => 'INNER'
]);
$this->hasMany('Programaciones', [
'foreignKey' => 'id_rutas'
]);
In the Paths table:
$this->belongsTo('Origen', [
'foreignKey' => 'id_trayecto_origen',
'className' => 'Locales',
'joinType' => 'INNER'
]);
$this->belongsTo('Destino', [
'foreignKey' => 'id_trayecto_destino',
'className' => 'Locales',
'joinType' => 'INNER'
]);
$this->hasMany('Programaciones', [
'foreignKey' => 'id_trayectos'
]);
In the Schedules table:
$this->belongsTo('Rutas', [
'foreignKey' => 'id_rutas',
'joinType' => 'INNER'
]);
$this->belongsTo('Trayectos', [
'foreignKey' => 'id_trayectos',
'joinType' => 'INNER'
]);
I'm trying to program a form control (input) to search for locale names both in paths and paths from the Schedules controller's index.ctp; but what I have advanced is to filter it by code:
In the index action of the Schedules controller I have this:
public function index()
{
if ($this->request->is(['patch', 'post', 'put'])) {
if (isset($this->request->data['PrgSearch'])) {
$conditions = $this->request->data['PrgSearch'];
$programaciones = $this->paginate($this->Programaciones->find('all', [
'conditions' => ['Programaciones.id' => $conditions],
'contain' => ['Rutas' => ['Origen', 'Destino'], 'Trayectos' => ['Origen', 'Destino']]
]));
$this->set(compact('programaciones'));
debug($programaciones);
}
}else{
$this->paginate = [
'contain' => ['Rutas' => ['Origen', 'Destino'], 'Trayectos' => ['Origen', 'Destino']]
];
$programaciones = $this->paginate($this->Programaciones);
$this->set(compact('programaciones'));
}
}
And in the index.ctp of the Programming template I have this:
<?= $this->Form->create($programaciones) ?>
<fieldset>
<?php
echo $this->Form->control('PrgSearch', ['label' => 'Buscar', 'placeholder' => 'Buscar Programación']);
?>
<?= $this->Form->button(__('Buscar', ['action' => 'index'])) ?>
</fieldset>
<?= $this->Form->end() ?>
And what I would like to get is something like this:
The only way I can think of to do this is to search for the Routes and Paths separately and then pass it as a condition to the search for the Schedules .
To take advantage of pagination we can send the search text as a GET parameter , for that we pass it as a parameter, like this:
The code in the controller mentioning certain important parts:
Important Links: