Jump to content

Edit History

QuentinJou

QuentinJou

Hello,

I try to add lot of fields in the product and the can change regularly, so I want not set in hard the field but can add with function.

When I do it with overidde in declare all field before it works :

Firs I modify database to add fields :

$sql = [];

$sql[] = 'ALTER TABLE `' . _DB_PREFIX_ . 'product_lang`
    ADD field1 TEXT NULL,
    ADD field2 TEXT NULL,
    ADD field3 TEXT NULL';

$sql[] = 'ALTER TABLE `' . _DB_PREFIX_ . 'product`
    ADD field4 DATE NULL,
    ADD field5 DATE NULL,
    ADD field6 BOOLEAN NULL';

foreach ($sql as $query) {
    if (Db::getInstance()->execute($query) == false) {
        return false;
    }
}

I overrides the product with my new class variable and fields definitions

class Product extends ProductCore
{
	public $field1;
	public $field2;
	public $field3;
	public $field4;
	public $field5;
	public $field6;

	public function __construct(
        $id_product = null,
        $full = false,
        $id_lang = null,
        $id_shop = null,
        Context $context = null
    ) {
        self::$definition['fields']['field1'] = [
            'type' => self::TYPE_STRING,
            'lang' => true,
            'validate' => 'isString',
            'required' => false
        ];
		self::$definition['fields']['field2'] = [
            'type' => self::TYPE_STRING,
            'lang' => true,
            'validate' => 'isString',
            'required' => false
        ];
		self::$definition['fields']['field3'] = [
            'type' => self::TYPE_STRING,
            'lang' => true,
            'validate' => 'isString',
            'required' => false
        ];
        self::$definition['fields']['field4'] = [
            'type' => self::TYPE_DATE,
            'validate' => 'isDateOrNull',
            'required' => false
        ];
        self::$definition['fields']['field5'] = [
            'type' => self::TYPE_DATE,
            'validate' => 'isDateOrNull',
            'required' => false
        ];
        self::$definition['fields']['field6'] = [
            'type' => self::TYPE_BOOL,
            'required' => false
        ];
		parent::__construct($id_product, $full, $id_lang, $id_shop, $context);
	}
}

So I try to add field as things progress in the code whith this function but it don't work he must declare his class variable before in product 

public function insertField($name, $lang = true, $inputType = "text", $dbType = null)
    {
        if (empty($dbType)) {
            if ($inputType == "date") {
                $dbType = 'DATE';
            } elseif ($inputType == "checkbox") {
                $dbType = 'BOOLEAN';
            } elseif ($inputType == "number") {
                $dbType = 'FLOAT';
            } else {
                $dbType = 'TEXT';
            }
        }

        Db::getInstance()->execute(
            "INSERT INTO " . _DB_PREFIX_ . "productextrafields (
                `name`, `lang`, `type`)
            VALUES('" . $name . "'," . (int)$lang . ", '" . $inputType . "')"
        );

        if ($lang) {
            Db::getInstance()->execute(
                'ALTER TABLE `' . _DB_PREFIX_ . 'product_lang`
                ADD '. $name . ' ' . $dbType . ' NULL'
            );
        } else {
            Db::getInstance()->execute(
                'ALTER TABLE `' . _DB_PREFIX_ . 'product`
                ADD '. $name . ' ' . $dbType . ' NULL'
            );
        }
        $prducts = Product::getProducts(
            Configuration::get('PS_LANG_DEFAULT'),
            1,
            1000,
            'id_product',
            'ASC'
        );
        Product::$definition['fields'][$name] = [
            'type' => Product::TYPE_STRING,
            'lang' => $lang,
            'validate' => 'isString',
            'required' => false
        ];
        foreach ($prducts as &$value) {
            $product = new Product($value['id_product']);
            object($product);
            $product->{$name} = null;
            $product->save();
        }
    }

How can I do it ?

Thank you in advance.

QuentinJou

QuentinJou

Hello,

I try to add lot of fields in the product and the can change regularly, so I want not set in hard the field but can add with function.

Actually how I do :

Firs I modify database to add fields :

$sql = [];

$sql[] = 'ALTER TABLE `' . _DB_PREFIX_ . 'product_lang`
    ADD field1 TEXT NULL,
    ADD field2 TEXT NULL,
    ADD field3 TEXT NULL';

$sql[] = 'ALTER TABLE `' . _DB_PREFIX_ . 'product`
    ADD field4 DATE NULL,
    ADD field5 DATE NULL,
    ADD field6 BOOLEAN NULL';

foreach ($sql as $query) {
    if (Db::getInstance()->execute($query) == false) {
        return false;
    }
}

I overrides the product with my new class variable and fields definitions

class Product extends ProductCore
{
	public $field1;
	public $field2;
	public $field3;
	public $field4;
	public $field5;
	public $field6;

	public function __construct(
        $id_product = null,
        $full = false,
        $id_lang = null,
        $id_shop = null,
        Context $context = null
    ) {
        self::$definition['fields']['field1'] = [
            'type' => self::TYPE_STRING,
            'lang' => true,
            'validate' => 'isString',
            'required' => false
        ];
		self::$definition['fields']['field2'] = [
            'type' => self::TYPE_STRING,
            'lang' => true,
            'validate' => 'isString',
            'required' => false
        ];
		self::$definition['fields']['field3'] = [
            'type' => self::TYPE_STRING,
            'lang' => true,
            'validate' => 'isString',
            'required' => false
        ];
        self::$definition['fields']['field4'] = [
            'type' => self::TYPE_DATE,
            'validate' => 'isDateOrNull',
            'required' => false
        ];
        self::$definition['fields']['field5'] = [
            'type' => self::TYPE_DATE,
            'validate' => 'isDateOrNull',
            'required' => false
        ];
        self::$definition['fields']['field6'] = [
            'type' => self::TYPE_BOOL,
            'required' => false
        ];
		parent::__construct($id_product, $full, $id_lang, $id_shop, $context);
	}
}

So I try to add field as things progress in the code whith this function but it don't work he must declare his class variable before in product 

public function insertField($name, $lang = true, $inputType = "text", $dbType = null)
    {
        if (empty($dbType)) {
            if ($inputType == "date") {
                $dbType = 'DATE';
            } elseif ($inputType == "checkbox") {
                $dbType = 'BOOLEAN';
            } elseif ($inputType == "number") {
                $dbType = 'FLOAT';
            } else {
                $dbType = 'TEXT';
            }
        }

        Db::getInstance()->execute(
            "INSERT INTO " . _DB_PREFIX_ . "productextrafields (
                `name`, `lang`, `type`)
            VALUES('" . $name . "'," . (int)$lang . ", '" . $inputType . "')"
        );

        if ($lang) {
            Db::getInstance()->execute(
                'ALTER TABLE `' . _DB_PREFIX_ . 'product_lang`
                ADD '. $name . ' ' . $dbType . ' NULL'
            );
        } else {
            Db::getInstance()->execute(
                'ALTER TABLE `' . _DB_PREFIX_ . 'product`
                ADD '. $name . ' ' . $dbType . ' NULL'
            );
        }
        $prducts = Product::getProducts(
            Configuration::get('PS_LANG_DEFAULT'),
            1,
            1000,
            'id_product',
            'ASC'
        );
        Product::$definition['fields'][$name] = [
            'type' => Product::TYPE_STRING,
            'lang' => $lang,
            'validate' => 'isString',
            'required' => false
        ];
        foreach ($prducts as &$value) {
            $product = new Product($value['id_product']);
            object($product);
            $product->{$name} = null;
            $product->save();
        }
    }

How can I do it ?

Thank you in advance.

×
×
  • Create New...