We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • 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 huh 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.

      • 32674
      • 101 Posts
      I was looking though the revo core for a dtd or xsd for the schema xml files a while back too.

      Is there one? In addition to specing all the options available, it would be handy to validate against before turning xPdo loose on the xml files.

      Added later: FYI. I just noticed the JIRA request for a dtd or xsd is here: http://svn.modxcms.com/jira/browse/XPDO-19
      • Exactly... this is a case where a xsd file might come in handy.

        Another question: can we add our own attributes and then access them via the API? I’m thinking along the lines of validation rules, e.g.

        <field key="some_number" dbtype="integer" precision="3" phptype="integer" null="false" default="" index="" validations="integer|min[12]|max[20]"/>


        Or even something like
        <validations>
            <validation key="some_field">integer</validation>
            <validation key="some_field">min["12"]</validation>
            <validation key="some_field">max["20"]</validation>
        </validations>


        I have a fair amount of code that could be adapted to use either approach, I’m just wondering if the API has a way to read an "extended" XML file.
        • You can already specify validation rules like

              <validation>
                  <rule field="payment_first_name"
                      name="required_firstname"
                      type="xPDOValidationRule"
                      rule="xPDOMinLengthValidationRule"
                      value="1"
                      message="Please specify a first name."
                  />
              </validation>
          


          .. havta run, or I’d post some more on this.
            Mike Schell
            Lead Developer, MODX Cloud
            Email: [email protected]
            GitHub: https://github.com/netProphET/
            Twitter: @mkschell
            • 28215
            • 4,149 Posts
            splittingred Reply #5, 14 years ago
            Quote from: netProphET at Apr 28, 2010, 01:54 PM

            You can already specify validation rules like
                <validation>
                    <rule field="payment_first_name"
                        name="required_firstname"
                        type="xPDOValidationRule"
                        rule="xPDOMinLengthValidationRule"
                        value="1"
                        message="Please specify a first name."
                    />
                </validation>
            


            .. havta run, or I’d post some more on this.

            "message" can also be a lexicon entry key.

            Everett, see: http://svn.modxcms.com/docs/display/xPDO20/Object+Validation
              shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com