We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24719
    • 194 Posts
    hi,

    I’m trying to model some address information for a web site user. i’ve created a linked list of countries->regions->cities, and want to be able to store the city and region of the web user by referencing a foreign key from the regions table.

    how would I do this? I’m not quite clear on the composites array.

    below is the map for the region object:

    $xpdo_meta_map['Region']['table']= 'region';
    $xpdo_meta_map['Region']['fields']= array (
    	'name' => '',									// region name
    	'parent' => 0									// id of this region's parent
    );
    $xpdo_meta_map['Region']['fieldMeta']= array (
    	'name' => array('dbtype' => 'VARCHAR', 'precision' => '200', 'phptype' => 'string', 'null' => false, ),
    	'parent' => array('dbtype' => 'UNSIGNED SMALLINT', 'phptype' => 'integer', 'null' => false, )
    );
    $xpdo_meta_map['Region']['composites']= array (
       'user' => array('id' => array('local' => 'id', 'foreign' => 'city', 'cardinality' => 'one', ), ),
    );
    $xpdo_meta_map['Region']['aggregates']= array ();
    $xpdo_meta_map['Region']= & $xpdo_meta_map['Region'];
    


    what does the key of the composites array (’id’ above) signify? could i just change this so that I have the following:

    $xpdo_meta_map['Region']['composites']= array (
       'user' => array('city' => array('local' => 'id', 'foreign' => 'city', 'cardinality' => 'one'),  
                              'region' => array('local' => 'id', 'foreign' => 'region', 'cardinality' => 'one')
                             )
    );
    


    also, what are aggregates and how are they different from composties?

    as an aside, can i declare fields with a dbtype of ’UNSIGNED INT’ as with the parent above?

    thanks for your help
    • redman:

      First, composites and aggregates...

      Composites and aggregates both represent relationships from the class defined to another class. The outer array of composites (or aggregates) uses the key of a related classname and can have multiple relations to the same class. This is why there is an embedded array with another key (in case there are multiple relations to the same table, to uniquely identify each relationship), that contains the local column name, the foreign column name, and a cardinality (one or many).

      The difference between composites and aggregates is that aggregates simple identify aggregate information that is independent of the class it is defined on, while composites relationships are dependent ones. For instance, if a user was deleted, all composite related tables would have any rows related to that user deleted as well, while no cascading delete effect would occur with aggregate relations.

      You can declare unsigned by adding ’attributes’ => ’unsigned’ to the fieldMeta element for the parent field. You can also model self-referential relations (i.e. parent) via aggregate or composite relations, in both directions...

      Give me some more time today and I will be posting information on reverse-engineering XML schemas from existing databases, modeling relationships, and forward-engineering the resulting model definition from XML to PHP classes and maps automatically. This hand-coding of the classes/maps is easy enough, but the real power of XPDO comes from maintaining the XML schema and using the code generation features to automatically update your maps as you model new relations, add new tables, etc.
        • 24719
        • 194 Posts
        brilliant, thanks for your quick reply. i look forward to reading that info. i didn’t think that the code generation was complete, but it would be much easier to simply create the tables i need and get xpdo to parse it ready for use.
          • 24719
          • 194 Posts
          wow. i’ve just tried the generator, and it’s so useful. that’s going to save me about a whole day’s work!

          is there any way to restrict which tables xpdo creates a schema for? i’ve got tables with prefixes modx_ and site_ in one table, and only want ones with site_
          • Quote from: redman at Jan 29, 2007, 05:51 PM

            wow. i’ve just tried the generator, and it’s so useful. that’s going to save me about a whole day’s work!

            is there any way to restrict which tables xpdo creates a schema for? i’ve got tables with prefixes modx_ and site_ in one table, and only want ones with site_
            Hmmm, no, but I’ve been meaning to add that feature. Shouldn’t take but a few minutes to add another parameter to the writeSchema() function to restrict operation to the specified table prefix. I’ll try and sneak that in this afternoon.
            • Quote from: OpenGeek at Jan 29, 2007, 07:13 PM

              Quote from: redman at Jan 29, 2007, 05:51 PM

              wow. i’ve just tried the generator, and it’s so useful. that’s going to save me about a whole day’s work!

              is there any way to restrict which tables xpdo creates a schema for? i’ve got tables with prefixes modx_ and site_ in one table, and only want ones with site_
              Hmmm, no, but I’ve been meaning to add that feature. Shouldn’t take but a few minutes to add another parameter to the writeSchema() function to restrict operation to the specified table prefix. I’ll try and sneak that in this afternoon.

              Ok, I added the new restrictPrefix parameter changes. You can view this changeset via FishEye at http://xpdo.org:8080/changelog/xpdo/?cs=18
                • 1294
                • 16 Posts
                aggregates and composites can be thought of as follows (from http://www.oracle.com/technology/products/ias/toplink/doc/10131/main/_html/descun002.htm#CHEIIIBJ):

                Two objects–a source (parent or owning) object and a target (child or owned) object–are related by aggregation if there is a strict one-to-one relationship between them, and all the attributes of the target object can be retrieved from the same data source representation as the source object. This means that if the source object exists, then the target object must also exist, and if the source object is destroyed, then the target object is also destroyed.

                conversely a composite relationship exists where this isn’t the case.
                • Note: this topic is old, and xPDO has changed a lot since, but I don’t think that the core concepts in question here have changed.

                  This last definition from al_b, by way of Oracle, seems to contradict OpenGeek’s explanation, doesn’t it?
                  Given 2 objects A and B, with B having a relationship with A:

                  • OpenGeek says that if B’s relationship to A is Composite, then B is deleted when A is deleted
                  • al_b says that if B’s relationship to A is Aggregate, then B is deleted when A is deleted
                  I’m also puzzled by the notion that "related by aggregation" implies a one-to-one relationship.
                    Mike Schell
                    Lead Developer, MODX Cloud
                    Email: [email protected]
                    GitHub: https://github.com/netProphET/
                    Twitter: @mkschell
                  • To be honest, I had never looked at that link, nor am I familiar with Oracle TopLink, but you are correct netProphET, those "Descriptors" do not describe anything related to what I mean by composition and aggregation in xPDO.

                    My definitions are based on the ideas of object composition.

                    AGGREGATE RELATIONS
                    When an object describing an aggregate relationship to another has no control over the creation and destruction of the other object, this is known as simple object aggregation.

                    COMPOSITE RELATIONS
                    When an object describing an aggregate relationship to another has total control over the creation and destruction of the other object, this is known as composite object aggregation.