Jump to content

Creating database in Object Model class ? Is it possible


Recommended Posts

Hello

 

i created db and tables in the main modules class and now i am looking to create objectmodel to use database functions,

 

my question is that, can we create a database and table within objectmodel class in our module.

 

or we can just create database functions

 

thanks,

Link to comment
Share on other sites

Hi...

First you need to create a database table in your module install method..Like as
 

public function install()
{
if(!Db::getInstance()->execute('CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'table_name` ('
. ' `id_table_name` INT(9) NOT NULL AUTO_INCREMENT,'
. ' `your_table_fileds` VARCHAR(50) NOT NULL,'
. ' PRIMARY KEY (`id_table_name`)'
. ' ) ENGINE=' . _MYSQL_ENGINE_ . ' default CHARSET=utf8;'))
return false;} 

An then after you need put your class is "yourmodulename/classes/yourmoduleclass.php"

see the here 
 

class yourmoduleclass extends ObjectModel
{
public $id_table_name;
    public $your_table_fileds;


/**
     * @see ObjectModel::$definition
     */
    public static $definition = array(
        'table' => 'table_name',
        'primary' => 'id_table_name',
        'multilang' => false,
        'fields' => array(
            'your_table_fileds' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
        ),
    );
}

See other core classes of prestashop for your reference 

Thanks

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...