Jump to content
floriqn

[sOLVED] UPDATE in php

Recommended Posts

Posted (edited)

Hello,

I'm trying to update the ps_product table and more precisely cache_has_attachements (set to 1 ) to display the attached document.

This function is in AdminImportController.php.

In my function in the php file  :

...
Db::getInstance()->execute('
        UPDATE `'._DB_PREFIX_.'product` 
        SET `cache_has_attachments` = 1
        ');
...

 

But it doesn't work.

It works when i write the command in MySQL :

UPDATE ps_product SET cache_has_attachments = 1

 

 

Someone would have any idea ?

PrestaShop : 1.7.6.5

 

Edited by floriqn (see edit history)

Share this post


Link to post
Share on other sites

And how do you know that a query to the database will run in your function?
The problem cannot be identified from this part of the code.

Share this post


Link to post
Share on other sites
2 hours ago, D. Tengler said:

And how do you know that a query to the database will run in your function?
The problem cannot be identified from this part of the code.

Hello D. Tengler,

Precisely I do not know how to test it. In the same function I have another request that works.

The code :

$attachments = get_object_vars($product);

                if (isset($attachments['attachment']) && !empty($attachments['attachment']))
                    foreach (explode($this->multiple_value_separator, $attachments['attachment']) as $single_attachment){
        
                    $tab_attachment = explode('|', $single_attachment);
                    $attachment_filename = isset($tab_attachment[0]) ? $tab_attachment[0] : '';
                    $attachment_name = isset($tab_attachment[1]) ? trim($tab_attachment[1]) : $attachment_filename ;
                    $attachment_description = isset($tab_attachment[2]) ? trim($tab_attachment[2]) : '';

                if(!empty($attachment_filename)) {
                    $id_attachment = (int)Attachment::addAttachmentImport($attachment_filename, $attachment_name, $attachment_description);
                    if (isset($attachments['attachment_id_nums']) && !empty($attachments['attachment_id_nums'])) {
                        $tab_attachment_id_nums = explode(',',$attachments['attachment_id_nums']);
                        foreach ($tab_attachment_id_nums as $id_product) {
                            Attachment::addAttachmentProductImport($id_product, $id_attachment);
                            Db::getInstance()->execute('
            					UPDATE `' . _DB_PREFIX_ . 'product`
            					SET `cache_has_attachments` = 1
        					');
                        }                     
                    }                    
                }
        }

 

Everything works except

Db::getInstance()->execute('
            UPDATE `' . _DB_PREFIX_ . 'product`
            SET `cache_has_attachments` = 1
        ');  

 

Share this post


Link to post
Share on other sites

And did you try to add it to the default value?

obrazek.png.cb9af6f84d44b079d50955604284b8b5.png

Share this post


Link to post
Share on other sites

1.It's maybe stupid, but try to add WHERE params.

Db::getInstance()->execute(' UPDATE `' . _DB_PREFIX_ . 'product` SET `cache_has_attachments` = 1 WHERE `id_product` = $id_product');

2. You can also try next step:

$query = (' UPDATE `' . _DB_PREFIX_ . 'product` SET `cache_has_attachments` = 1 ');

dump($query);

exit();

Than copy/paste the $query values to the sql command prompt and check the errors.

3. Try the ExecuteS instead of Execute

Share this post


Link to post
Share on other sites
On 6/9/2020 at 8:04 AM, D. Tengler said:

And did you try to add it to the default value?

obrazek.png.cb9af6f84d44b079d50955604284b8b5.png

Hi D.Tengler,

Please excuse my late reply but I found a similar solution that works perfectly.

Share this post


Link to post
Share on other sites
On 6/9/2020 at 10:27 AM, Amantha Bill said:

1.It's maybe stupid, but try to add WHERE params.

Db::getInstance()->execute(' UPDATE `' . _DB_PREFIX_ . 'product` SET `cache_has_attachments` = 1 WHERE `id_product` = $id_product');

2. You can also try next step:


$query = (' UPDATE `' . _DB_PREFIX_ . 'product` SET `cache_has_attachments` = 1 ');

dump($query);

exit();

Than copy/paste the $query values to the sql command prompt and check the errors.

3. Try the ExecuteS instead of Execute

Hi Amanda

As for D.Tengler, please excuse my late reply but I found a similar solution that works perfectly. But I keep your solution for another project.

Thanks.

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

×
×
  • Create New...

Important Information

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