Query Magento database object oriented

Naturally you can query Magento database directly as described in my earlier post about querying Magento database. But a much smarter way is it, to use exiting Magento objects and constructs like that:

This is a simplified version of a function (better to say shell script class) that sets attribute value positions for a certain attribute code.

The bare query version of the code above would look like that:

But that’s not state of the art. It is strongly recommended to use the build in constructs to query Magento database whenever possible (there are reasonable exceptions). The abstraction helps to prevent errors and makes the code better maintainable. Another enhancement is, that through the abstraction of database interactions, it is possible to change the database type without the need of a complete code refactoring (in best case 😉 ).

By the way. If you want to know, how I got the queries out of the the objects in the first code snippet, you can read this elderly blog post.

For those who want to see the complete position change shell script, should have a look at my blog post about Option value position (select value sort order).

More “Query Magento database” examples

Here are a few more examples on how to query Magento database the right way:

  • insert multiple items to a custom table:
  • delete multiple items:

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.