Jump to content

Add a new table to your PrestaShop database


Recommended Posts

I'm working through the tutorial about how to create a PrestaShop module, when it says (here):

 

Follow these steps:

1.

Add a new table to your PrestaShop database, named ps_test.

 

 

However, for the life of me I can't find the instructions on how to

"Add a new table to your PrestaShop database".

 

 

I know it's probably simple but I can't find it. Can anyone help?

Link to comment
Share on other sites

  • 2 months later...

note: when you create a real module, you will create the table using php code from within the modules install function, so that users of the module would not have to manually create the table

 

Ok, I don't see that in the tutorial. Is that a Db::getInstance()->Execute() command or which command do you use to create the table?

Link to comment
Share on other sites

you can use php code like this code:

$sql= "CREATE TABLE IF NOT EXISTS `"._DB_PREFIX_."tablename`(
    `id_tablename` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `title` VARCHAR(256) NOT NULL )";

    if(!$result=Db::getInstance()->Execute($sql))
    return false;

look this code :

1. there is better to use id_tablename instead of id in your table this is use in adminTab module

2.you use Execute when you wnat to write in database like create delete update and...

and use ExecuteS when you want to read database like select

and when you use ExecuteS use _PS_USE_SQL_SLAVE_ like this :

Db;;getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql)

for this section you go to this link for know how to work the sql in prestashop

good practice

  • Like 4
Link to comment
Share on other sites

  • 2 years later...

Perfect, thanks. I've experimented with phpAdmin commands so I can see how this fits right in, but in the php code.

 you can use this in php code type 'module'

public function installDB()
	{
		return Db::getInstance()->execute('
		CREATE TABLE `'._DB_PREFIX_.'test` 
			`id_test` INT UNSIGNED NOT NULL AUTO_INCREMENT,
			`name` VARCHAR(255) NOT NULL,
			PRIMARY KEY (`id_test`)
			) DEFAULT CHARSET=utf8;');
	}
Link to comment
Share on other sites

  • 2 years later...
On 1/12/2015 at 8:59 AM, sancoLgates said:

 you can use this in php code type 'module'


public function installDB()
	{
		return Db::getInstance()->execute('
		CREATE TABLE `'._DB_PREFIX_.'test` 
			`id_test` INT UNSIGNED NOT NULL AUTO_INCREMENT,
			`name` VARCHAR(255) NOT NULL,
			PRIMARY KEY (`id_test`)
			) DEFAULT CHARSET=utf8;');
	}

I know this thread is very old but I just saw it and there's a question I want to ask. What is _DB_PREFIX_? Is this the prestashop database?

Link to comment
Share on other sites

6 hours ago, beginner1 said:

What is _DB_PREFIX_? Is this the prestashop database?

This is the prefix to the name of your database.  The value is typically ps_

The prefix allows you to share a single database across multiple stores, where each store will have a different prefix (ps1_, ps2_, ps3_ etc...)

 

Link to comment
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...