Jaquarh
Jaquarh

Reputation: 6673

Db Insert returns false in Prestashop 1.7 during install Module

I am trying to insert a row into my newly built table but it returns false. Looking at the Prestashop Dev Docs, I can see that Db::getInstance returns an object with a function to insert.

I can also see that the pSQL() method is used in this example which I am assuming strips the input before its injected into the SQL query. (Strange why they are not just using prepared statements but anyway).

My current code looks like this:

public function install()
{
    if(!\Db::getInstance()->execute(
        'CREATE TABLE IF NOT EXISTS `' . self::DB_PREFIX . 'iezon_portfolio` ('
        . ' `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,'
        . ' `img_link` VARCHAR (120) NOT NULL,'
        . ' `title` VARCHAR (80) NOT NULL,'
        . ' `description` VARCHAR(1024) NOT NULL,'
        . ' `created_on` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,'
        . ' `company_name` VARCHAR(30) NOT NULL,'
        . ' `company_url` VARCHAR(80) NOT NULL,'
        . ' `testimonial` VARCHAR(255) NOT NULL,'
        . ' PRIMARY KEY(`id`)'
        . ' ) ENGINE=' . self::SQL_ENGINE . ' DEFAULT CHARSET="utf8";'
    )) {
        # Table gets inserted fine
        return false;
    }

    if(!$this->create(
        '/img/prestashop-logo-1578759072.jpg',
        'My First Testimonial',
        'Lorem ipsum dolor sit amet conse ctetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim.',
        'Lorem Ipsum',
        '#',
        'Lorem ipsum dolor sit amet conse ctetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim.'
    )) {
        return false; # This returns false, no row was created
    };

    return parent::install();
}

public function create( $img, $title, $description, $company, $company_url, $testimonial )
{
    return \Db::getInstance()->insert(self::DB_PREFIX . 'iezon_portfolio', array(
        'img_link' => pSQL($img),
        'title' => pSQL($title),
        'description' => pSQL($description),
        'company_name' => pSQL($company),
        'company_url' => pSQL($company_url),
        'testimonial' => pSQL($testimonial)
    ));
}

Seems that the insert() is failing for some reason. Can anyone point me in the right direction? I'm new to using Prestashop.

Upvotes: 0

Views: 827

Answers (1)

Krystian Podemski
Krystian Podemski

Reputation: 1491

You don't need to use DB_PREFIX with insert method, PrestaShop prefix is added automatically

Upvotes: 1

Related Questions