Hi!
So I have this table structure:
Tickets -> hasOne
=>TicketsFacturacion
=>TicketsPostventas -> hasOne
=> TicketsPostventasBajas
=> TicketsPostventasCambioDatos
=> TicketsPostventasCambioTarifa
=> TicketsPostventasCambioTitular
=> TicketsPostventasCuentaBancaria
=> TicketsPostventasLecturas
=> TicketsPosventasTipos
Something like this:
and I’m trying to paginate records from all the TicketsPostventas associations as well as TicketsFacturacion throught Tickets with a query like this.
'limit' => 25,
'conditions' => array_merge(
$condiciones_generales,
$this->getCondicionesPostventa($sesion_tickets),
$this->getCondicionesFacturacion($sesion_tickets)
),
'contain' => [
'TicketsFacturacion',
'TicketsPostventas' => [
'TicketsPostventasBajas',
'TicketsPostventasCambioDatos',
'TicketsPostventasCambioTarifa',
'TicketsPostventasCambioTitular',
'TicketsPostventasCuentaBancaria',
'TicketsPostventasLecturas',
'TicketsPosventasTipos' => [
'joinType' => 'LEFT',
],
],
],
];```
TicketsFacturacion and the diferent hasOne associations of TicketsPostventas have common fields like CUPS, so conditions that arrive may end up been something like this examples
```[
"Tickets.estado_id !=" => 8,
"TicketsFacturacion.tipo_facturacion_id" => "3",
0 => [
"TicketsPostventas.id IN" => [
11 => 11,
],
]
1 => [
"TicketsFacturacion.cups LIKE" => "ES0219090021035542LL",
],
]```
```[
"Tickets.estado_id !=" => 8,
0 => [
"TicketsPostventas.id IN" => [
11 => 11,
],
]
1 => [
"TicketsFacturacion.cups LIKE" => "ES0219090021035542LL",
],
]```
```[
"Tickets.estado_id !=" => 8,
"TicketsFacturacion.tipo_postventa_id" => "3",
1 => [
"TicketsFacturacion.cups LIKE" => "ES0219090021035542LL",
],
]```
In the case of TicketsPostventas I'm searching by its ID's by doing something like this
```if (!empty($sesion['cups'])) {
$ids_postventa = [];
foreach ($this->Tickets->TicketsPostventas->associations()->getByType('HasOne') as $tabla) {
if ($this->Tickets->TicketsPostventas->{$tabla->name()}->hasField('cups')) {
$ids_postventa += $this->Tickets->TicketsPostventas->{$tabla->name()}->find('list', [
'conditions' => [
$tabla->name() . '.cups LIKE' => '%' . trim($sesion['cups']) . '%',
],
'keyField' => 'postventa_id',
'valueField' => 'postventa_id',
])->toArray();
}
}
$condiciones[] = [
'TicketsPostventas.id IN' => $ids_postventa ?: [0],
];
}```
If the search is only by one of the unique fields (tipo_facturacion_id or tipo_postventa_id), it filters correctly but whenever the seach has common fields in one of the TicketsPostventas hasone tables and TicketsFacturacion, it doesnt bring anything. I guess it's forcing the conditions in all of the tables at the same time.
I don't even know if what I want to achieve is posible like this.
I've tried to separate it in two different queries and merge the results paginating directly in the view with JS but Im pretty sure it will become very slow once the tables start to have a lot of records in them.
I'll appreciate some help! :D