Jump to content

[1.6]Problème avec la methode Db::getInstance=>update()


Recommended Posts

Bonjour à tous :)

 

J'ai un problème avec la methode update() de Db. Je n'est aucune modification de ma table celle-ci  :blink:

Je souhaite pourtant faire au plus propre comme expliquer sur https://www.prestashop.com/blog/fr/les-bonnes-pratiques-de-la-classe-db-sur-prestashop-1-5/ (notez que je suis sous 1.6)

 

Voici ma méthode qui ne passe pas:

<?php
    private function setModulePositionIntoHookDisplayTop() {
        //gets the position for this module into the displayTop hook
        $sql = "SELECT `position` FROM `ps_hook_module` WHERE `id_module` = 27 AND `id_hook` = 15;";
        $module_position = Db::getInstance()->executeS($sql, $array = true, $use_cache = false);

        //gets the id for this module into the displayTop hook
        $sql = "SELECT `id_module` FROM `ps_module` WHERE `name` = '" . $this->name . "';";
        $id_module = Db::getInstance()->executeS($sql, $array = true, $use_cache = false);
        
        //gets rows for the displayTop hook where position is upper to the future position of this module
        $sql = "SELECT * FROM `ps_hook_module` WHERE"
            . "`position` >= (SELECT `position` FROM `ps_hook_module` WHERE `id_module` = 27 AND `id_hook` = 15)"
            . " AND `id_hook` = 15"
            . " AND `id_module` NOT IN (SELECT `id_module` FROM `ps_module` WHERE `name` = '" . $this->name . "');";
        $result = Db::getInstance()->executeS($sql, $array = true, $use_cache = false);
        
        //update position of other module then position is upper or equal to this module position
        foreach ($result as $value) {
            $p = $value['position'] + 1;
            $data = array('position' => $p);
            $where = 'id_module = '.$value['id_module'].' AND id_hook = 15';
            Db::getInstance()->update('ps_hook_module', $data, $where);
        }
        
        //finally update the position of this module
        $data = array('position' => $module_position);
        $where_module = 'id_module = '.$id_module.' AND id_hook = 15';
        Db::getInstance()->update('ps_hook_module', $data, $where_module);
        return true;
    }
?>

Si quelqu'un à une idée sur le pourquoi je n'arrive pas à faire fonctionner cette methode qui est appelée en fin de methode install() de mon module ;)

 

Merci d'avance

Edited by BeRoots (see edit history)

Share this post


Link to post
Share on other sites

Oui. Enfin pour le moment je souhaiterai comprendre comment utiliser la méthode update et surtout pourquoi elle ne fonctionne pas ici alors que si je remplace par des execute("UPDATE ...") j'ai bien modification de la table ;)

Share this post


Link to post
Share on other sites

Les points sont là pour la concaténation de chaine ;)

$str = 'une chaine' . 'concaténée';

Sinon Merci pour ton aide mais cela ne résout pas le problème.

 

Ceci ne fonctionne toujours pas:

<?php
private function setModulePositionIntoHookDisplayTop() {
    //gets the future position for this module into the displayTop hook
    $sql = "SELECT `position` FROM `ps_hook_module` WHERE `id_module` = 27 AND `id_hook` = 15;";
    $module_position = Db::getInstance()->getValue($sql, $use_cache = false);

    //gets the id for this module into the displayTop hook
    $sql = "SELECT `id_module` FROM `ps_module` WHERE `name` = '" . $this->name . "';";
    $id_module = Db::getInstance()->getValue($sql, $use_cache = false);

    //gets rows for the displayTop hook where position is upper to the future position of this module
    $sql = "SELECT * FROM `ps_hook_module` WHERE"
        . " `position` >= (SELECT `position` FROM `ps_hook_module` WHERE `id_module` = 27 AND `id_hook` = 15)"
        . " AND `id_hook` = 15"
        . " AND `id_module` NOT IN (SELECT `id_module` FROM `ps_module` WHERE `name` = '" . $this->name . "');";
    $result = Db::getInstance()->executeS($sql, $array = true, $use_cache = false);

    //update position of other module then position is upper or equal to this module position
    foreach ($result as $value) {
        $p = $value['position'] + 1;
        $data = array('position' => $p);
        $where = "`id_module` = '".$value['id_module']."' AND `id_hook` = '15'";
        Db::getInstance()->update('ps_hook_module', $data, $where);
    }

    //finally update the position of this module
    $data = array('position' => $module_position);
    $where_module = "`id_module` = '".$id_module."' AND `id_hook` = '15'";
    Db::getInstance()->update('ps_hook_module', $data, $where_module);

    return true;
}
?>

Par contre si je le fait sans utiliser la methode update() comme ceci cela fonctionne nickel :huh:

<?php
private function setModulePositionIntoHookDisplayTop() {
    //gets the future position for this module into the displayTop hook
    $sql = "SELECT `position` FROM `ps_hook_module` WHERE `id_module` = 27 AND `id_hook` = 15;";
    $module_position = Db::getInstance()->getValue($sql, $use_cache = false);

    //gets the id for this module into the displayTop hook
    $sql = "SELECT `id_module` FROM `ps_module` WHERE `name` = '" . $this->name . "';";
    $id_module = Db::getInstance()->getValue($sql, $use_cache = false);

    //gets rows for the displayTop hook where position is upper to the future position of this module
    $sql = "SELECT * FROM `ps_hook_module` WHERE"
        . " `position` >= (SELECT `position` FROM `ps_hook_module` WHERE `id_module` = 27 AND `id_hook` = 15)"
        . " AND `id_hook` = 15"
        . " AND `id_module` NOT IN (SELECT `id_module` FROM `ps_module` WHERE `name` = '" . $this->name . "');";
    $result = Db::getInstance()->executeS($sql, $array = true, $use_cache = false);

    //update position of other module then position is upper or equal to this module position
    foreach ($result as $value) {
        $p = $value['position'] + 1;
        $sql = "UPDATE `ps_hook_module` SET `position` = '" . $p
            . "' WHERE `id_module` = '" . $value['id_module']
            . "' AND `id_hook` = '15';";
        Db::getInstance()->execute($sql);
    }

    //finally update the position of this module
    $sql = "UPDATE `ps_hook_module` SET `position` = '" . $module_position
        . "' WHERE `id_module` = '" . $id_module . "' AND `id_hook` = '15';";
    Db::getInstance()->execute($sql);

    return true;
}
?>

Si quelqu'un serai me dire ce que j'ai raté avec la méthode update() ;)

Merci d'avance.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More