1

I am writing an uncomplicated install script

$table->addColumn(
    'name',
    Varien_Db_Ddl_Table::TYPE_VARCHAR,
    255,
    array(
        'nullable' => false,
    )
);

i need this column to only have unique values (it is not the tables primary key), also is there a way to only allow lowercase and no special characters (only a-z)

  • Is this a core table? Is this a custom table? If so could we see the schema of the table (SHOW CREATE [table]) ? Could you show us the entire installer script? – philwinkle Jun 28 '16 at 22:38
  • a custom table $this->startSetup(); $table = new Varien_Db_Ddl_Table(); $table->setName($this->getTable('namespace_affiliates/affiliates')); – Yehuda Schwartz Jun 29 '16 at 12:47
1

Without knowing all of the information I would suggest there are a few ways you can approach this.

Unique strings

This can be enforced by MySQL by adding a UNIQUE key constraint.

In SQL:

ALTER TABLE user ADD UNIQUE (email)

or expressed as Varien DDL:

$table->addIndex(
    $installer->getIdxName('youmodule/modelalias', array('email'), Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE),
    array('email'), array('type' => Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE)

Alpha entry only

  • To enforce the schema you could create a Backend Model (read more here: https://magento.stackexchange.com/a/6423/336) to validate the input
  • You could also use a backend model to strip non-alpha characters out instead of validating
  • You could use a MySQL TRIGGER to enforce the schema as well on INSERT or UPDATE

However, to my knowledge, there is no built-in column type in MySQL that will enforce this schema for you.

  • thank you what is the varibal $installer? – Yehuda Schwartz Jun 29 '16 at 12:50
  • also what is youmodule/modelalias i assume youmodule is my module's name bu what is modelalias ? – Yehuda Schwartz Jun 29 '16 at 12:54
  • also is there any documentation on creating and applying a Backend Model? – Yehuda Schwartz Jun 29 '16 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.