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

    I need help to create schema for tags relationships for a site. Please see objects below:
    <object class="tags" table="tags" extends="xPDOSimpleObject">
    	<field key="name" dbtype="varchar" precision="100" phptype="string" null="false" default="" />
    	<field key="used" dbtype="int" precision="6" phptype="integer" null="false" default="0" />
    	<composite alias="tagsRelationships" class="tagsRelationships" local="id" foreign="tag" cardinality="many" owner="local" />
    </object>
    
    <object class="tagsRelationships" table="tag_relationships" extends="xPDOSimpleObject">
    	<field key="object_key" dbtype="varchar" precision="100" phptype="string" null="false" default="" />
    	<field key="tag" dbtype="int" precision="10" phptype="integer" null="false" default="0" index="index" />
    	<field key="object" dbtype="int" precision="10" phptype="integer" null="false" default="0" index="index" />
    	<aggregate alias="tags" class="tags" local="tag" foreign="id" cardinality="one" owner="foreign" />
    </object>

    and the tag_relationships table contains tags for several resources such as blogs, galleries and ect. tag columns is for tag id and object column is for ID of resource e.g. ID of a post or a image in gallery.

       object_key   |       tag      |     object
    ----------------------------------------------------     
        userBlog    |        5       |        2       
        gallery     |        5       |        2       
        userBlog    |        5       |        3       
    

    As you see
    object#2 which its type is userBlog has tag#5
    object#2 which its type is gallery has tag#5
    object#3 which its type is userBlog has tag#5

    Now see userBlog schema and last composite:
    <object class="userBlog" table="user_blog" extends="xPDOSimpleObject">
    	<field key="subject" dbtype="varchar" precision="255" phptype="string" null="false" default="[بدون موضوع]" />
    	<field key="message" dbtype="text" phptype="string" null="false" default="" />
    	<field key="createdon" dbtype="int" precision="20" phptype="timestamp" null="false" default="0" />
    	..........
    	<aggregate alias="modWebUser" class="modWebUser" local="author" foreign="id" cardinality="one" owner="foreign" />
    	<composite alias="userComment" class="userComment" local="id" foreign="parent" cardinality="many" owner="local" />
    	<composite alias="tagsRelationships" class="tagsRelationships" local="id" foreign="object" cardinality="many" owner="local" />
    </object>

    So when userBlog#2 (object#2 which its type is userBlog in above table) is deleted all rows that have object#2 will be deleted but as you can see there are differences in types for repeated IDs.


    So do you have any suggestion to solve it?
    The only solution i found was creating blog_tag_relationships and gallery_tag_relationships and etc....




    And another related to schema question: Is below object correct?
    <object class="userRelationships" table="user_relationships" extends="xPDOSimpleObject">
    	<field key="user1" dbtype="int" precision="10" phptype="integer" null="false" default="0" index="index" />
    	<field key="user2" dbtype="int" precision="10" phptype="integer" null="false" default="0" index="index" />
    	<field key="joint" dbtype="tinyint" precision="1" phptype="boolean" null="false" default="0" />
    	<field key="time" dbtype="int" precision="20" phptype="timestamp" null="false" default="0" />
    	<aggregate alias="modWebUser1" class="modWebUser" local="user1" foreign="id" cardinality="one" owner="foreign" />
    	<aggregate alias="modWebUser2" class="modWebUser" local="user2" foreign="id" cardinality="one" owner="foreign" />
    </object>




    Regards,
    AHHP
      God loves me. 【ツ】


      MODX.ir (Persian Support)

      Boplo.ir/modx/ (Persian)
      • 22295
      • 153 Posts
      As you know, the issue is embedded in the design, ’object’ is not a unique id. xpdo simply follows your defined composite relationship:
      <composite alias="tagsRelationships" class="tagsRelationships" local="id" foreign="object" cardinality="many" owner="local" />
      I assume you’ll get into the same issues later on, for example when you search for tagRelationships based on userBlog, you will not be able to use the getObjectGraph /getCollectionGraph methods. although that’ll be easier to bypass, better solve it in the xpdo design.
      AFAIK, you can not use compound relationships with xpdo, see:
      http://modxcms.com/forums/index.php/topic,35293.msg228101.html#msg228101

      In case it is so, one solution could be to add a GUID to your userBlog and gallery objects, then use that instead of the object id. Then your xpdo model would be valid.

      Another approach could be to avoid this composite relationship. then you’d just have to take care of the tagsRelationships remove in your blog/gallery delete code. it’ll have a $modx->getObject(’tagsRelationships’,array(’object_key’=>’userBlog’,’object’=>’2’));
      and remove the match.