Aggiunta seconda clausola per JOIN nelle API

This commit is contained in:
Beppe 2024-07-25 14:50:08 +02:00
parent 1edf80a491
commit 75e140d7ea
4 changed files with 70 additions and 30 deletions

View File

@ -30,41 +30,55 @@ class Anagrafiche extends Resource implements RetrieveInterface, CreateInterface
{
public function retrieve($request)
{
$table = '`an_anagrafiche`';
$table = 'an_anagrafiche';
$select = [
'`an_anagrafiche`.*',
'`an_nazioni_lang`.`title` AS nazione',
'an_anagrafiche.*',
'an_nazioni_lang.title AS nazione',
];
$joins[] = [
'an_nazioni_lang' => '`an_nazioni_lang`.`id_record` = `an_nazioni`.`id` AND `an_nazioni_lang`.`id_lang` = '.\Models\Locale::getDefault()->id,
'an_nazioni',
'an_nazioni.id',
'an_anagrafiche.id_nazione',
];
$where[] = ['`an_anagrafiche`.`deleted_at`', '=', null];
$joins[] = [
'an_nazioni_lang',
'an_nazioni_lang.id_record',
'an_nazioni.id',
'an_nazioni_lang.id_lang',
\Models\Locale::getDefault()->id,
];
$order['`an_anagrafiche`.`ragione_sociale`'] = 'ASC';
$where[] = ['an_anagrafiche.deleted_at', '=', null];
$order['an_anagrafiche.ragione_sociale'] = 'ASC';
if ($request['resource'] != 'anagrafiche') {
$type = 'Cliente';
$joins[] = [
'`an_tipianagrafiche_anagrafiche`',
'`an_anagrafiche`.`idanagrafica`',
'`an_tipianagrafiche_anagrafiche`.`idanagrafica`',
'an_tipianagrafiche_anagrafiche',
'an_anagrafiche.idanagrafica',
'an_tipianagrafiche_anagrafiche.idanagrafica',
];
$joins[] = [
'`an_tipianagrafiche`',
'`an_tipianagrafiche_anagrafiche`.`idtipoanagrafica`',
'`an_tipianagrafiche`.`id`',
'an_tipianagrafiche',
'an_tipianagrafiche_anagrafiche.idtipoanagrafica',
'an_tipianagrafiche.id',
];
$joins[] = [
'an_tipianagrafiche_lang' => '`an_tipianagrafiche_lang`.`idrecord` = `an_tipianagrafiche`.`id` AND `an_tipianagrafiche_lang`.`idlang` = '.\Models\Locale::getDefault()->id,
'an_tipianagrafiche_lang',
'an_tipianagrafiche_lang.id_record',
'an_tipianagrafiche.id',
'an_tipianagrafiche_lang.id_lang',
\Models\Locale::getDefault()->id,
'an_tipianagrafiche_lang.title',
''.$type.'',
];
$where[] = ['`an_tipianagrafiche_lang`.`title`', '=', $type];
}
return [

View File

@ -33,39 +33,51 @@ class Articoli extends Resource implements RetrieveInterface, UpdateInterface, C
$table = 'mg_articoli';
$select = [
'mg_articoli.*',
'`categorie`.`nome` AS categoria',
'`sottocategorie`.`nome` AS sottocategoria',
'categorie_lang.title AS categoria',
'sottocategorie_lang.title AS sottocategoria',
];
$joins[] = [
'mg_articoli_lang' => 'mg_articoli_lang.id_record = mg_articoli.id AND mg_articoli_lang.id_lang = '.\Models\Locale::getDefault()->id,
'mg_articoli_lang',
'mg_articoli_lang.id_record',
'mg_articoli.id',
'mg_articoli_lang.id_lang',
\Models\Locale::getDefault()->id,
];
$joins[] = [
'`mg_categorie` AS categorie',
'`mg_articoli`.`id_categoria`',
'`categorie`.`id`',
'mg_categorie AS categorie',
'mg_articoli.id_categoria',
'categorie.id',
];
$joins[] = [
'mg_categorie_lang AS categorie_lang' => '`mg_categorie_lang`.`id_record` = `categorie`.`id` AND `mg_categorie_lang`.`id_lang` = '.\Models\Locale::getDefault()->id,
'mg_categorie_lang AS categorie_lang',
'categorie_lang.id_record',
'categorie.id',
'categorie_lang.id_lang',
\Models\Locale::getDefault()->id,
];
$joins[] = [
'`mg_categorie` AS sottocategorie',
'`mg_articoli`.`id_sottocategoria`',
'`sottocategorie`.`id`',
'mg_categorie AS sottocategorie',
'mg_articoli.id_sottocategoria',
'sottocategorie.id',
];
$joins[] = [
'`mg_categorie_lang` AS sottocategorie_lang' => '`mg_categorie_lang`.`id_record` = `sottocategorie`.`id` AND `mg_categorie_lang`.`id_lang` = '.\Models\Locale::getDefault()->id,
'mg_categorie_lang AS sottocategorie_lang',
'sottocategorie_lang.id_record',
'sottocategorie.id',
'sottocategorie_lang.id_lang',
\Models\Locale::getDefault()->id,
];
$where[] = ['`mg_articoli`.`deleted_at`', '=', null];
$where[] = ['mg_articoli.deleted_at', '=', null];
$whereraw = [];
$order['`mg_articoli`.`id`'] = 'ASC';
$order['mg_articoli.id'] = 'ASC';
return [
'table' => $table,

View File

@ -51,7 +51,11 @@ class Interventi extends Resource implements RetrieveInterface, CreateInterface,
];
$joins[] = [
'in_statiintervento_lang' => 'in_statiintervento_lang.id_record = in_statiintervento.id AND in_statiintervento_lang.id_lang = '.\Models\Locale::getDefault()->id,
'in_statiintervento_lang',
'in_statiintervento_lang.id_record',
'in_statiintervento.id',
'in_statiintervento_lang.id_lang',
\Models\Locale::getDefault()->id,
];
$joins[] = [

View File

@ -180,7 +180,17 @@ class Manager
}
foreach ($joins as $join) {
$query->leftJoin($join[0], $join[1], $join[2]);
if (count($join) >= 3) {
$query->leftJoin($join[0], function($joinClause) use ($join) {
$joinClause->on($join[1], $join[2]);
// Aggiungi condizioni aggiuntive se ci sono abbastanza elementi in $join
if (isset($join[3])) {
$joinClause->whereRaw($join[3] . ' = ?', [$join[4]]);
}
});
}
}
if (!empty($where)) {