-->

Using Magento API v/s plain MySQL

2019-07-24 22:22发布

问题:

I've been tasked with connecting a phpbb forum to an existing Magento database.

The way this will work is:

  1. New users register on the magento-based site and choose a forum username as well as a website username (could be the same). That way, they can use the same credentials in both places.
  2. Some registration logic needs to populate other database fields based on the user info.
  3. While authenticating an existing phpbb user, Magento's DB is used.

I used Magento's API for populating the table in 1, and plain MySQL queries/updates in 3. I'm stuck at 2 though.

For 2, the fields accessed by phpbb will be updated only by the phpbb forum (well, apart from the forum username), and never by the Magento site. Using Magento's API seems like an awfully tedious option and to my naive mind, unnecessary for my situation.

So, should I use the Magento API or plain MySQL inserts, and why?

回答1:

Pick the Magento API. Not only do you not have to emulate generating the whole hash (they use a 2 character salt and append it to an MD5 hash), you also won't have to worry when/if they decide to update the database schema or hashing mechanism.

Changing a user password is pretty simple;

require_once 'app/Mage.php';

Mage::init('admin');

$customer = Mage::getModel('customer/customer')->load(123); // whatever the user ID is
$customer->changePassword('whatever_new_password');


回答2:

I Would suggest you to use magento API at all, meaning no SQL. To use magento outside Magento scope you could use:

require_once('app/Mage.php');
Mage::app();

Moreover, if I understood correctly I would suggest for

Some registration logic needs to populate other database fields based on the user info

Create separate table with additional fields, and make link with customers table. TO achieve load, you simply need to create Magento's model and understand how to work with models propery.

Basically, Why I vote for raw-sql:

  1. Speed. Work far quicker than API models handling
  2. Simplicity. Very simple, as you work directly with DB.

For Magento API:

  1. Simplicity. As for me it is far more comfortable to work with API models, than SQL queries. So we don't write sql at all.
  2. Integrity. You may break something with sql, while you don't feel about things quite well. Using Models API you won't break anything. Framework will handle everything buy itself.
  3. Less code. Magento API has many-many-many useful methods, so I think you won't need to write much code, if you use api, not sql, because magento has everything you need: db, validation, format functions, etc.

So my vote is for magento.



回答3:

In alomst every case developer should use native Magento classes in order to perform CRUD functions on data.

Using native classes/methods allow Magento to use build in event handling or cascading operations on related objects, for ex.: when you delete customer, all customer_id field in order entities would be set to NULL.

Beside that there are lot of other reason why you should chose Magento API, @Jevgeni Smirnov posted some of them.