We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17528
    • 0 Posts
    Hi guys..is there a way to relate the existing user table with a new table as a foreign key relation? That is to use the primary key of the user table as a foreign key into a new relation?

    Thanks in advance
      • 33968
      • 863 Posts
      Have you built a schema for your new table?

      I think you’ll find this chapter from the docs useful:
      http://rtfm.modx.com/display/xPDO20/Defining+Relationships
        • 17528
        • 0 Posts
        hey mate thanks for the answer..i ve read the docs..my question is about the existing user table that modx uses..which contains a username and a password field..I am asking if there is a way to associate that table to custom based tables with fk relation; that is new custom tables referring to it as fk.

        Thanks
          • 33968
          • 863 Posts
          I don’t know the structure of your new table and what relationship it has with the ’users’ table, but for example you could define an aggregate type relationship like this:
          <object class="CustomClass" table="custom_table" extends="xPDOSimpleObject">
           <field key="customKey" dbtype="int" precision="10" phptype="integer" null="false" default="" />
            . . . 
          
           <aggregate alias="User" class="modUser" local="customKey" foreign="id" cardinality="one" owner="foreign" />
          </object>
          

          You’ll need to run xPDOGenerator to create php classes and maps for the schema you created, so that you can use them within modx.

          You’ll then be able to use something like the following to grab, for example, a username related to a particular field in your custom table:
          <?php
          $custom = $modx->getObject('CustomClass',1);     // where '1' is the value of 'customKey'
          $userid = $custom->getOne('User');
          return $userid->get('username');
          


          You can look in core/model/schema/modx.mysql.schema.xml to see how existing relationships (and classes) are defined for modx tables.

          I can’t go into the level of detail in the documentation, but if you’re still unsure about this feel free to post more info about your custom table and which user field you want to use as a foreign key.
            • 6228
            • 249 Posts
            If you want the capacity to fetch related records as [tt]$modx->user->getOne(’CustomClass’)[/tt], I believe you will need to dynamically alter the MODx Users schema/model, which I *think* is technically possible. How to go about doing it is a different story - I’d be interested to know how.
              lo9on.com

              MODx Evolution/Revolution | Remote Desktop Training | Development
              • 17528
              • 0 Posts
              thanks all for your answers..

              My custom table is going to be used as an extended form of the current existing user table in modx. Therefore, it going to have a primary key which is a foreign key also to the id of the user table. My question is that, from my understanding through the documentation in order to relate two tables you need to define in both of them an aggregate/composite relationship. So based on this fact i need to modify the existing xml schema provided by modx; which i think is not the best thing to do..Any suggestions are most welcomed.

              Thanks
                • 33968
                • 863 Posts
                i need to modify the existing xml schema provided by modx; which i think is not the best thing to do.
                Agreed smiley

                Have a look at this tutorial on Extending the modUser Class. By implementing this I think you can override the default modUser schema with your own. I think at this stage you can also do the same for modResource, but not sure about the others.