====== addAttribute ====== ~~NOTOC~~ **From Version 5.21.0-beta-2** == See Also == [[v5:dictionary:structure:introduction|Structured Schema Management]]\\ [[v5:dictionary:column_attributes|Column Attributes Reference]]\\ == Syntax == obj addAttribute( mixed $attribute, optional string $platform='', optional integer $priority = -1; ) The method ''addAttribute()'' is part of the **//Structured Schema Management//** suite, and adds an attribute to the current metaObjectStructure object. An attribute can be added to any of the available structure types, table,column,index or index-item. ===== Parameters ===== ==== $attribute ==== The attribute can be provided as a string, a numeric or associative array, and can describe any parameter understood by the DBMS. For example, it might be necessary to add an option to tell the DBMS what type of database engine to use. ==== Platform ==== The platform parameter can be any parameter recognized by the [[v5:dictionary:structure:platform|Platform]] keyword. ==== Priority ==== The priority parameter can optionally force the system to process the attributes in a specific order. If not provided, the attributes are processed in the order provided. ===== Usage ===== In the following example, a table **//employees//** is to be created. The table is to be used in a portable environment, and require 2 platform specific attribute. - Under **MySQL**, an engine type needs to be specified. - Under **IBM DB2**, a tablespace needs to be specified. $t = new metaObjectStructure($dict,'employees'); $t->addAttribute('ENGINE INNODB','mysql'); /* * Add another platform option, for IBM DB2 */ $t->addAttribute(array('TABLESPACE'=>'LARGE1'),'db2'); Alternatively, we can write the definition as: $t = new metaObjectStructure($dict,'employees'); $t->addAttribute('ENGINE INNODB','mysql') ->addAttribute(array('TABLESPACE'=>'LARGE1'),'db2'); ===== Understanding Attributes ===== ==== Portable Attributes ==== Portable attributes are comparable to the options available to the original [[v5:dictionary:column_attributes|Column Attributes]] in **//Simple Schema Management//**. * If a feature is **portable**, no platform designation is generally necessary but can still be used * Portable options may have a **//priority//** automatically assigned, which may control the order of definitions. In particular, the priority will force the NOTNULL and DEFAULT attributes to conform to the ANSI SQL order. ==== Non-Portable (Custom) Attributes ==== A custom attribute is one that must be defined fully in order to be handled correctly by the DBMS. In principle, any option not all ready defined as portable in the [[v5:dictionary:column_attributes|Column Attributes]] list is a custom attribute. In the following example, we create a column, and add 3 attributes to it. 1 attribute is portable and 2 are custom. /* * We define a metaObjectStructure, that represents * the table 'employees' */ $dict = NewDataDictionary($db); $t = new metaObjectStructure($dict,'employees'); /* * We now add an object representing the column, 'somecolumn' of type Character(32) */ $c = $t->addColumnObject('somecolumn','C(32)'); /* * now add a portable attribute DEFAULT * This could also be passed as a string "DEFAULT 'SOMEVALUE'") */ $c->addAttribute(array('DEFAULT'=>'SOMEVALUE'); /* * now add a custom attribute to column, applicable to all platforms */ $c->addAttribute(array('CHARACTER SET'=>'"ascii"')); /* * Now add another attribute, which is only used if the database type is SQL Server */ $c->addAttribute('SPARSE','mssqlnative'); /* * This object is now passed to the addColumnSql method */ $sql = $dict->addColumnSql($t) If ADOdb was attached to a MySQL database, the sql returned would be: ALTER TABLE employees ADD somecolumn VARCHAR(32) DEFAULT 'SOME VALUE' CHARACTER SET "ascii" but if attached to a SQL Server database, it would appear as: ALTER TABLE employees ADD somecolumn VARCHAR(32) DEFAULT "SOME VALUE" CHARACTER SET "ascii" SPARSE