Funzione MySQL nello script di installazione
-
13-12-2019 - |
Domanda
È possibile creare una funzione da uno script di Installer Magento?Attualmente ho il seguente codice:
.
$installer = $this;
$installer->startSetup();
$installer->run("
DROP function IF EXISTS {$this->getTable('getLatestActivity')};
CREATE FUNCTION {$this->getTable('getLatestActivity')} (activityid int) RETURNS int(11)
BEGIN
set @rank = 0;
set @matchingId := (select id from (
SELECT @rank := @rank+1 AS rank, t_act.id, t_act.date
FROM activityupdates ut_act
LEFT JOIN activityupdates AS t_act ON t_act.ticked = 1
AND t_act.date <= ut_act.date AND t_act.customer_id = ut_act.customer_id AND t_act.type = ut_act.type
WHERE ut_act.id = activityid
ORDER BY t_act.date DESC
)ranked
where ranked.rank = 1);
return @matchingId;
END;
");
Ma ottengo un'eccezione lanciata SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4
- non molto utile.
Per ottenere questo funzionamento in phpmyadmin, ho dovuto aggiungere linee relative ai delimitatori, ma penso che sia un inconveniente della GUI piuttosto che la funzione che richiede loro.Se li includo, ho solo un'eccezione sulla definizione del delimitatore.
$installer->run("
DROP function IF EXISTS `{$this->getTable('getLatestActivity')}`;
DELIMITER $$
CREATE FUNCTION `{$this->getTable('getLatestActivity')}` (activityid int) RETURNS int(11)
BEGIN
set @rank = 0;
set @matchingId := (select id from (
SELECT @rank := @rank+1 AS rank, t_act.id, t_act.date
FROM activityupdates ut_act
LEFT JOIN activityupdates AS t_act ON t_act.ticked = 1
AND t_act.date <= ut_act.date AND t_act.customer_id = ut_act.customer_id AND t_act.type = ut_act.type
WHERE ut_act.id = activityid
ORDER BY t_act.date DESC
)ranked
where ranked.rank = 1);
return @matchingId;
END$$
DELIMITER ;
");
.
ERRORE: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER $$ CREATE FUNCTION getLatestActivity (activityid int)
Soluzione
Mage_Core_Model_Resource_Setup::run()
accetta più query, dividendo l'argomento specificato (Dichiarazione SQL) a punto e virgola. Ciò porta al corpo funzione diviso in più chiamate separate. La dichiarazione della funzione è quindi incompleta e la creazione della funzione non riesce con il messaggio di eccezione specificato.
Inoltre, al contrario di utilizzare direttamente il client MySQL CLI (o PhpMyAdmin), definendo un delimitatore conduce a un PDOException
. NILS Preuß ha ricevuto una soluzione da zend_db mailing list qualche anno fa :
..
- Non preoccuparti del delimitatore, non ne hai bisogno.
- È necessario rilasciare e creare in due dichiarazioni separate.
- Bypassare il metodo di query ZF predefinito.
Per riassumere, il seguente snippet porterà alla funzione creato:
$this->getConnection()->query("DROP function IF EXISTS `{$this->getTable('getLatestActivity')}`");
$this->getConnection()->query("
CREATE FUNCTION `{$this->getTable('getLatestActivity')}` (activityid int) RETURNS int(11)
BEGIN
set @rank = 0;
set @matchingId := (select id from (
SELECT @rank := @rank+1 AS rank, t_act.id, t_act.date
FROM activityupdates ut_act
LEFT JOIN activityupdates AS t_act ON t_act.ticked = 1
AND t_act.date <= ut_act.date
AND t_act.customer_id = ut_act.customer_id
AND t_act.type = ut_act.type
WHERE ut_act.id = activityid
ORDER BY t_act.date DESC
) ranked
where ranked.rank = 1);
return @matchingId;
END;
");
.