ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:dictionary:structure:addattribute

addAttribute

From Version 5.21.0-beta-2

See Also

Structured Schema Management
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 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.

  1. Under MySQL, an engine type needs to be specified.
  2. 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 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 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
v5/dictionary/structure/addattribute.txt · Last modified: 2016/04/10 02:41 by mnewnham