We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1778
    • 659 Posts
    Hi all

    I have to build a xml schema with many tables. Some of them have compound primary keys (keys based on 2 or 3 columns).

    Referring to this post "http://modxcms.com/forums/index.php?topic=50387.0", I have to admit i’m a bit lost...

    Quote from: OpenGeek at Jun 11, 2010, 10:21 AM

    Quote from: Black at Jun 11, 2010, 10:13 AM

    • Is this supported by xPDO?
    • How should I define the composite relationship?
      Would this do the trick?: <composite alias="table1" class="myTable1" local="colid1,colid2" foreign="colid1,colid2" cardinality="many" owner="local" />
    • How should I define the aggregate relationship?
      Would this do the trick?: <aggregate alias="table2" class="myTable2" local="colid1,colid2" foreign="colid1,colid2" cardinality="one" owner="foreign" />

    Though xPDO currently supports tables with compound primary keys, it does not support defining aggregate/composite relationships based on compound keys. For now, you may override xPDOObject methods in these classes to handle the unsupported relation definitions.

    That said, I definitely believe support for this should be added. Can you enter a JIRA feature request for this so I don’t forget to add it to the xPDO roadmap?

    First of all is this feature as been already added to xPDO ? Or should I have to override the XPDOObject methods, or getMany() and getOne() now support multi-column relationship ?
    If yes how to define the relationships in my schema ? Is
    <composite alias="table1" class="myTable1" local="colid1,colid2" foreign="colid1,colid2" cardinality="many" owner="local" /> 
    and
    "<aggregate alias="table2" class="myTable2" local="colid1,colid2" foreign="colid1,colid2" cardinality="one" owner="foreign" />" 
    

    the way to achieve this ?

    A simple and functionnal example would be very great wink
    Thanks in advance
    Cheers
      • 3749
      • 24,544 Posts
      xPDO should handle compound keys (including primary keys) fine. Make sure you have the latest version.

      I would try generating the schema from the existing tables.

      See this: http://bobsguides.com/custom-db-tables.html

      It’s brand new and untested, except by me, but it should get you going.

        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 1778
        • 659 Posts
        Hi Bob,

        I’m running Revo 2.0.4pl from Git, I suppose the xPDO version is the last one...

        Effectively one way is to first create the db in MySQL and then generate schema from the physical tables, but I was looking for a way to do it the other way... Write the schema first and generate physical tables from it...
        I have no problem to do that with simple primary keys but the question is how do I write the composite and aggregate relationships in my schema when the "local" and the "foreign" are composed of 2 or 3 columns, so the physical tables are well made/formed/conformed (not sure of the right word in this case, -i’m not a very fluent english speaker I guess- ) when they are generated, hope you understand what I’m trying to explain...
        Cheers

        EDIT : I forgot to mention that I ask this question, because I have to create a transport package for my 3PC, so I create the physical tables from the schema...
          • 1778
          • 659 Posts
          Around the same subject I have another question...
          Is it possible to (and of course how to...) define a constraint on 2 or 3 (or more columns) in my schema ?

          SQL would be something like that ... but how to define the constraints ?

          CREATE TABLE personne_metier
          (
          	ID_personne_metier INT NOT NULL IDENTITY CONSTRAINT PK_personne_metier PRIMARY KEY
          	, ID_personne INT NOT NULL CONSTRAINT FK_personne_metier__ID_personne FOREIGN KEY (ID_personne) REFERENCES dbo.Liste
          	, ID_metier INT NOT NULL CONSTRAINT FK_personne_metier__ID_metier FOREIGN KEY (ID_metier) REFERENCES dbo.metier
          	, CONSTRAINT UQ_personne_metier__ID_personne__ID_metier UNIQUE (ID_personne, ID_metier)
          )
          


          IOW how do I transform the SQL code above in a xml.schema ?
          Cheers
            • 22303 MODX Staff
            • 10,725 Posts
            There is currently no support for aggregate/composite relationships defined by more than one column, but it is has been requested and should be implemented in a 2.1 release of xPDO.

            For now, you can override addOne/getOne/addMany/getMany in your specific classes that use these relations, and use the relationship alias to identify when you need to override the behavior to handle the multi-column relationships.
              • 1778
              • 659 Posts
              Thanks a lot for your answer... Is there already a planned date for the 2?1 Release of xPDO ?

              I would like too, add a constraint on 2 columns in a table
              With SQL I would do something like this
              CREATE TABLE companies (
                id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
                name VARCHAR(80) NOT NULL,
                office VARCHAR(80) NOT NULL,
                description VARCHAR(255) NULL,
                email VARCHAR(45) NULL,
                website VARCHAR(80) NULL,
                PRIMARY KEY(id),
                UNIQUE(name, office)     <-- HOW CAN I DO THIS IN MY XML SCHEMA ???
              );
              
              


              In my schema I would have something like this

              <object class="Company" table="companies" extends="xPDOSimpleObject">
              		<field key="name" dbtype="varchar" precision="80" phptype="string" null="false" default="" index="index" />
              		<field key="office" dbtype="varchar" precision="80" phptype="string" null="true" default="" index="index" />
              		<field key="description" dbtype="text" phptype="text" null="true" default="" />
              
              		<field key="email" dbtype="varchar" precision="255" phptype="string" null="true" default="" />
              		<field key="website" dbtype="varchar" precision="255" phptype="string" null="true" default="" />
              		
                  </object>
              


              I can’t find how to define that the pair ’name/office’ is unique like "UNIQUE(name,office)" in SQL... Or there’s no way to do that easily, directly in my schema ?

              Cheers
                • 22303 MODX Staff
                • 10,725 Posts
                All that constraint does is add a UNIQUE INDEX on those two columns. You can do this in the current release of xPDO for mysql by simply defining the following attributes in both fields in the schema:
                		<field key="name" dbtype="varchar" precision="80" phptype="string" null="false" default="" index="unique" indexgrp="nameoffice" />
                		<field key="office" dbtype="varchar" precision="80" phptype="string" null="true" default="" index="unique" indexgrp="nameoffice" />
                

                though, just FYI, this is changing in the near future to a new isolated index format that will better support various database engines and custom index types (this is how it works in sqlite implementation already):
                        <index alias="nameoffice" name="nameoffice" primary="false" unique="true">
                            <column key="name" collation="A" null="false" />
                            <column key="office" collation="A" null="false" />
                        </index>
                
                  • 1778
                  • 659 Posts
                  Great and easy smiley thanks a lot...

                  I like the idea of an isolated index format... and love the way it works in the sqlite implementation you gave in example... Would it be the same for MySQL ?

                  Other question, is there somewhere a doc (even a draft) with the list of all attributes properties for a field ?

                  Cheers and thanks again
                    • 22303 MODX Staff
                    • 10,725 Posts
                    Quote from: anso at Oct 18, 2010, 10:20 AM

                    I like the idea of an isolated index format... and love the way it works in the sqlite implementation you gave in example... Would it be the same for MySQL ?
                    It will indeed be the same of all database engines; the implementation for mysql will be modified before 2.0.0-pl very soon.

                    Quote from: anso at Oct 18, 2010, 10:20 AM

                    Other question, is there somewhere a doc (even a draft) with the list of all attributes properties for a field ?
                    Just an incomplete one at http://rtfm.modx.com/display/xPDO20/Defining+the+Database+and+Tables, but this definitely needs to be addressed. In addition, there will be an XML schema definition for the xPDO model schema files in the near future.
                      • 1778
                      • 659 Posts
                      Hey ... finally today is a good day... Good answers and good news for the near future... smiley
                      Jason you put light on my (dark) side of the world ^_^
                      Thanks again
                      Cheers