Untitled
unknown
plain_text
3 years ago
3.9 kB
39
Indexable
Buongiorno, ho un problema con una query sul DB, riesco a farla funzionare solamente per product_reference esclusivamente numerici, se contengono Lettere o caratteri diversi (esempio il "-") non viene eseguita, restituisce un errore. Vediamo se qualcuno può aiutarmi:
L'obiettivo è quello di passare da una ricerca per product_id ad una per product_reference nell'elenco ordini.
Questa parte prende il valore dal campo di ricerca del modulo (Chiamato ID ma in realtà modificato per cercare nei reference) , lo esplode usando la virgola come carattere di concatenazione e prepara l'input per la ricerca.
public function getContent()
{
if (true === ((bool) Tools::isSubmit('CF_IDS'))) {
$this->postProcess();
}
$input = \preg_replace('/\s+/', '', Configuration::get('CF_IDS'));
$input = \preg_replace('/,,+/', ',', $input);
if (',' === Tools::substr($input, -1)) {
$input = Tools::substr($input, 0, -1);
}
$input = \implode(',', \array_unique(\explode(',', $input)));
Configuration::updateValue('CF_IDS', $input);
if (empty(Configuration::get('CF_IDS'))) {
$out = $this->displayWarning($this->l('Please enter ID\'s'));
} else {
if ($this->validateInput(Configuration::get('CF_IDS'))) {
$out = $this->findCustomers() . $this->findOrders();
} else {
$out = $this->displayWarning($this->l('Only ID\'s are allowed'));
}
}
return $this->renderForm() . $out;
}
Quà controllo l'ipnut (ho rimosso il controllo che accettava solo caratteri numerici)
public function validateInput($link)
{
return \preg_match('/^(.*)+$/', $link);
/*return \preg_match('/^[,0-9 ]+$/', $link);*/
}
Quì gestisco l'array di output
public function uniqueMultidimArray($array, $key)
{
$temp_array = [];
$i = 0;
$key_array = [];
foreach ($array as $val) {
if (!\in_array($val[$key], $key_array, true)) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
++$i;
}
return $temp_array;
}
Questa la funzione di ricerca
public function findCustomers()
{
$sql = new DbQuery();
$sql->select('C.id_customer, C.firstname, C.lastname, C.email');
$sql->from('customer', 'C');
$sql->innerJoin('orders', 'O', 'C.id_customer = O.id_customer');
$sql->innerJoin('order_detail', 'OD', 'O.id_order = OD.id_order');
$sql->where('OD.product_reference IN (' . Configuration::get('CF_IDS') . ')');
$out = Db::getInstance()->executeS($sql);
if (empty($out)) {
return $this->displayWarning($this->l('No customers was found for ID\'s: "')
. Configuration::get('CF_IDS') . '"' . ' ' . $sql);
}
$result = $this->uniqueMultidimArray($out, 'id_customer');
La query che non restituisce risultati per esempio viene stampata così:
SELECT C.id_customer, C.firstname, C.lastname, C.email FROM `ps_customer` C INNER JOIN `ps_orders` `O` ON C.id_customer = O.id_customer INNER JOIN `ps_order_detail` `OD` ON O.id_order = OD.id_order WHERE (OD.product_reference IN (PRN013100-02))
La query che restituisce risultati (usando un reference solo numerico) esce così
SELECT C.id_customer, C.firstname, C.lastname, C.email FROM `ps_customer` C INNER JOIN `ps_orders` `O` ON C.id_customer = O.id_customer INNER JOIN `ps_order_detail` `OD` ON O.id_order = OD.id_order WHERE (OD.product_reference IN (40505006))
Qualche idea? Grazie
Editor is loading...