-2

Found this lovely piece of sql query code to excluded multiple images on magento.

UPDATE catalog_product_entity_media_gallery_value SET disabled = 0; 
UPDATE catalog_product_entity_media_gallery_value AS mgv,
(SELECT entity_id, COUNT(*) as image_count, MAX(value_id) AS value_id
FROM catalog_product_entity_media_gallery AS mg
GROUP BY entity_id
HAVING image_count = 2) AS mg
SET mgv.disabled = 1
WHERE mgv.value_id = mg.value_id 

I am looking to have this run as a cron job instead of direct with the database but have no idea how to write it up as one.

Can this be made into a cron job?

Point me in the right direction please.

  • There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow down the answer set or to isolate an issue that can be answered in a few paragraphs. – Daan Jun 01 '15 at 09:43
  • 1
    _"And if so can someone do it for me please?"_ No, SO isn't a free coding service, hire a free lancer. – Epodax Jun 01 '15 at 09:47
  • they wanted me to be direct, all i really wanted was a yes it can be written in a cron job, here is an tutorial in writing cron jobs that access database. Or look at this and to explains writing cron jobs. – always magento issues Jun 01 '15 at 09:49
  • _" all i really wanted was a yes it can be written in a cron job, here is an tutorial in writing cron jobs that access database. Or look at this and to explains writing cron jobs"_ That's just not now SO works, give [how-to-ask](http://stackoverflow.com/help/how-to-ask) and [on-topic](http://stackoverflow.com/help/on-topic) a read – Epodax Jun 01 '15 at 10:09

1 Answers1

0

Using the command line tool that comes with most databases you could wrap these statements in a script, like this, using mysql

/path/to/mysql --whateverparametersyouneed yourdb << HERE
  UPDATE catalog_product_entity_media_gallery_value SET disabled = 0; 
   UPDATE catalog_product_entity_media_gallery_value AS mgv,
  (SELECT entity_id, COUNT(*) as image_count, MAX(value_id) AS value_id
  FROM catalog_product_entity_media_gallery AS mg
  GROUP BY entity_id
  HAVING image_count = 2) AS mg
  SET mgv.disabled = 1
  WHERE mgv.value_id = mg.value_id; 
HERE

This is called a here-document, see How can I write a here doc to a file in Bash script? for more information.

Then, just call that script from cron.

Community
  • 1
  • 1
fvu
  • 32,488
  • 6
  • 61
  • 79