adding data directly to a magento database - magento

I am adding regions of a country to the database in Magento so when a user selects their country a relevant list of regions will be available in a drop down menu. To do this I believe I need to add information to directory_country_region and directory_country_region_name.
The tutorial I've been looking at states that I should add them directly to the database using sql, however I remember reading that you should not place information directly into the database using raw sql when working with Magento.
My questions are:
1- to keep in line with best practices do I need to use some magento functions to add the required information to my database or can they be dropped in using raw sql?
2- if I need to use some Magento functions how do I work out which I need to use (I have heard off and noticed the lack of documentation) or is there some online reference, even if it is limited?
3- if I am not to use a sql query why is it considered bad practice to do so in Magento?

Hello, if you want to add the information just once, you can use raw sql (faster and no drawbacks), also you are right about the 2 tables (if the country is already in directory_country). If you want to make something that will be available for more Magento instalations you have to crate a new Magento module and add the sql using the installer you can read more here http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-6-magento-setup-resources
Magento wiki is a good place to start, also there are lots of blog posts.
It's considered a bad practice because Magento has its own ORM and most of the time for your new tables (entities) you only need to create models that extend magento core models and you will have access to CRUD without any development and everyone that uses your new module will understaind what's going on.
Example for a region you can use the class Mage_Directory_Model_Region or for a collection of regions Mage_Directory_Model_Resource_Region_Collection

Related

Is it advisible to add new table in Joomla?

I need to know if it is advisible to add new table in default database of Joomla. Will it be preserved if I update my Joomla.
If yes, then can anybody describe the steps?
Yes, you can add custom database tables if you wish, it doesn't pose any threats. Just make sure you use Joomla coding standards when getting or adding data from the table. More information on that can be found here:
http://docs.joomla.org/J2.5:Accessing_the_database_using_JDatabase
As for updating Joomla, yes, the table will be preserved.
Hope this helps
As already said, you can add any table you like to your Joomla database. Joomla doesn't care for tables it doesn't know. And those extra tables are not modified by an update.
BUT keep two things in mind:
1. If you make backups of your database with a joomla extension (such as lazyBackup) and you want to backup your extra-tables too, then make sure to include these extra tables by choosing the appropriate options in the backup extension. By default those extra table might not be included in your backup.
2. As you can easily see in phpMyAdmin all joomla tables start with a prefix such as he3ie_. This prefix is different on every joomla installation (if you did not choose your own). It is no good idea to start your extra tables with this prefix. This might collide with joomla-tables which could be created in the future (by joomla or by an extension).
as everybody said adding tables will not be affected by Joomla upgrades unless they decide to name a new table the same as yours. A table "name" could also clash with another extension that you want to install so it would be good advise to spend some time thinking on a name that will make sure is unique to your use or component.
Of course if this is a one-off custom thing then it matters less.
If you are looking for a more in-depth tutorial try this YouTube Video: http://www.youtube.com/watch?v=twT3q7dyVpI
Joomla update doesn't disturb your custom database tables or any other data. Just create the database as you want like phpmyadmin etc & use it without any fear.

Magento adding set of attributes/properties to products

I need to add something similar to the Review system already available on Magento. I mean, I want to add a couple of texts, reviewing capacities and alike. It should have an administration side on the backend to be able to reject/accept/edit those texts. I still do not have all the things clear so I want to ask before I embark into a wrong/closed path.
Should I reuse the existing code by copying core files into local and modifying at will?
Or should I add my own tables and link to EAV model using product ID?
On this SO question the accepted answer posts a code on how to add a new custom attribute to all products. I could use this but I would still need to add the reviews part.
I've searched for a similar functionality but can not find anything.

Joomla 1.5 - Creating SQL tables in components

Joomla 1.5 has JTable, which can be extended to act as an Active Record system (Create/Read/Update/Delete).
However, I can not find any way for this interface to create the table represented by my JTable sub-class.
Every example I have found has started with "manually create the database tables... ", then created a JTable class to work with it. I would like to be able to distribute my plugin and have it create the tables on setup, so that this is not necessary.
Any easy way to do this?
This is usually done in the install.sql of your extension (The link is related to component but I guess for plugins it's the same).

Ability to connect Magento to existing database?

I have an existing database that contains 4 tables: categories, sub-categories, products, prices. I am looking to create an ecom site using Magento leveraging the existing tables. I would rather not import the tables since products will be continually modified. I am new to Magento. What is the best way to handle this? Would I want to query my existing tables and hook into Magento bypassing their product tables? Is there a way to do this? Thanks!
I think you're choosing the wrong e-commerce system if want to use your existing database in place of the one in Magento. Magento doesn't work like this. You can't have the Magento interface and not the guts of it. They are inextricably bound (some would say, tightly coupled).
I would hire a Magento programmer and have him or her create a module for you to import your products into the Magento catalog. This module should be able to handle updates to your existing database. This can be done easily as the Magento catalog model uses the entity-attribute-value (EAV) database model pattern. Then you can continue using your database.

Magento custom attributes failing to install

I followed the tutorial at http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/installing_custom_attributes_with_your_module step by step to make my module install a couple of custom attributes, using Magento CE 1.4. I get to see the custom attributes on the product edit page, but whenever I try to save them I get an SQL error complaining that column "myattrid" does not exist. I know that:
this column is part of the flat product tables and indeed, it is not there
a lot of people give the advice to create by hand, but this bypasses the point of automatic installation
Is the tutorial I followed outdated? If so, what extra steps do I need to take?
An alternative approach is outlined in this blog post. You could try that.
I agree that you should avoid creating the attributes manually, there are many keys, indexes and relationships that Magento needs to be aware of which might be bypassed if you go straight to the database.
HTH,
JD

Resources