Sorry for the long post with questions sprinkled throughout. I’ve been wading through the docs, but I haven’t seen a definition of all available attributes used in the XML schema files. Is there something out there that explains what each attribute is used for and how it correlates to the database structure and/or to code variables? I’m happy to include this in the formal docs, but I want to make sure I’m understanding it correctly, so please, weigh in.
For example, take one of the keys from the schema referenced here:
http://svn.modxcms.com/docs/display/revolution/Using+Custom+Database+Tables+in+your+3rd+Party+Components
you might have something like this:
<field key="name" dbtype="varchar" precision="100" phptype="string" null="false" default="" index="index" />
- key="myfield" : corresponds to a field name of `myfield`
- dbtype="varchar" : data type used by the database (e.g. int, tinyint, varchar, text) -- these are dependent on the database used.
- precision="100" : number of characters visible in that field. E.g. tinyint(1) has a precision of 1 character, so it can store 0 - 9, but not a two-digit number like 10 or 11.
- phptype="string": php data type. Valid data types are string? Integer?
- null="false" : corresponds to a NOT NULL in the field definition
- default="" : if there is any DEFAULT value set at the database level.
- index="index": possible values are "" (blank), "index" (ensures that the field is indexed for faster searching), or "unique";
Are there other attributes that I’m missing here?
Is it correct to say that certain schemas will therefore NOT be database agnostic? E.g. if you define a certain dbtype for a field that one database supports but not another (e.g. Oracle boolean vs. MySQL tinyint(1) )?
Also, what if I don’t want to use the default ’id’ name for my primary key? I’d prefer to use an explicit name, e.g. ’book_id’. How is that handled?
The same definition list is really required to explain the stuff that appears in the other XML tags, including
- model: package, baseClass, platform, defaultEngine, phpdoc-package, phpdoc-subpackage
- object: class, extends
- aggregate: alias, class, local, foreign, owner
- composite: alias, class, local, foreign, owner
For example, compare the
core/model/schema/modx.mysql.schema.xml to the database schema:
<object class="modAccessPolicy" table="access_policies" extends="xPDOSimpleObject">
<field key="name" dbtype="varchar" precision="255" phptype="string" null="false" index="unique" />
<field key="description" dbtype="mediumtext" phptype="string" />
<field key="parent" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" index="index" />
<field key="class" dbtype="varchar" precision="255" phptype="string" null="false" default="" index="index" />
<field key="data" dbtype="text" phptype="json" default="{}" />
<aggregate alias="Parent" class="modAccessPolicy" local="parent" foreign="id" owner="foreign" cardinality="one" />
<composite alias="Children" class="modAccessPolicy" local="id" foreign="parent" owner="local" cardinality="many" />
<composite alias="Permissions" class="modAccessPermission" local="id" foreign="policy" owner="local" cardinality="many" />
</object>
And the corresponding database CREATE TABLE statement:
CREATE TABLE `modx_access_policies` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`description` mediumtext,
`parent` int(10) unsigned NOT NULL default '0',
`class` varchar(255) NOT NULL default '',
`data` text,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`),
KEY `parent` (`parent`),
KEY `class` (`class`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1
All I’m trying to do for a start is to take an existing CREATE TABLE statement that I have (and that I understand) and get an XML schema put together so I can interface with this table. Here’s a simple example: I’ve downloaded a list of ZIP codes from a gov’t web site, and I want to have a simple snippet that will return the city and state by using the $xpdo->getObject() function.