Valentin Tanasescu
Valentin Tanasescu

Reputation: 106

Fatal error: Uncaught Error: Class 'Db' not found in prestashop

Sorry for my english.

I am a beginner in prestashop and I am still trying to learn more about it.

I created a module (/module3) in /modules directory. This has next files:

-module3.php (the main php file of this module)
-/views/templates/hook/module3.tpl (the template for module3.php)

-/controllers/front/products.php (the controller of this module)
-/views/templates/front/products.tpl (the template for my controller)
-and also /controllers/front/take_product.php (which will be called by products.tpl by ajax).

My /views/templates/front/products.tpl has next ajax:

`   function print_product (id_product) {
        var form_data = new FormData();
        form_data.append('id_product', id_product);

        take(function(rezultat) {
            $("#product_div").html(rezultat);
        });

        function take(rezultat) {
            $.ajax({
                url: "http://127.0.0.1/prestashop/modules/module3/controllers/front/take_product.php",
                dataType: 'text',
                cache: false,
                contentType: false,
                processData: false,
                data: form_data,                         
                type: 'post',
            }).done(function(output) {
                rezultat(output);
            });
        }
    }`

My /controllers/front/take_product.php has next code:

`
    @ini_set('display_errors', 'on');

    $sql = 'SELECT * FROM ps_product WHERE id_product="'.(int)$_POST["id_product"].'"';
    $result = Db::getInstance()->execute($sql);
    echo $result;
`

That's what /views/templates/front/products.tpl receives and prints:

`
Fatal error: Uncaught Error:
Class 'Db' not found in
/var/www/html/prestashop/modules/module3/controllers/front/take_product.php:6
Stack trace: #0 {main} thrown in
/var/www/html/prestashop/modules/module3/controllers/front/take_product.php
on line 6 `

Maybe you think I need a class-controller in /controllers/front/take_product.php.
That's why I also changed my /controllers/front/take_product.php with next code:

`
    @ini_set('display_errors', 'on');

    class Module3take_productsModuleFrontController extends 
    ModuleFrontController
    {
        public function initContent()
        {
            parent::initContent();

            $sql = 'SELECT * FROM ps_product WHERE id_product="'.(int)$_POST["id_product"].'"';
            $result = Db::getInstance()->execute($sql);
            echo $result;
        }
    }
`

But now /views/templates/front/products.tpl receives and prints another error:

`
Fatal error: 
Class 'ModuleFrontController' not found in
/var/www/html/prestashop/modules/module3/controllers/front/take_product.php
on line 6 `

So... Do you have any idea?

Upvotes: 1

Views: 2517

Answers (1)

sadlyblue
sadlyblue

Reputation: 2987

You are trying to access the take_product.php directly in your ajax. Instead of hard coding the url, use the Link object in your tpl:

url: "{$link->getModuleLink('Module3', 'take_products', [], true)|escape:'html':'UTF-8'}",

This way, it will have the correct url to load the Prestashop and use the modulecontroller.

Just a reminder of another solution, but not so correct. You don't need it to extend the ModuleFrontController (as you tried before), just to "load" prestashop using in the beggining of the file:

require_once(dirname(__FILE__) .'/../../../../config/config.inc.php'); 

cron_currency_rates.php uses this instead of being a controller. But, again, this is not the best way. Just wanted to point out the difference.

Upvotes: 0

Related Questions