We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Quote from: jkenters at Aug 05, 2010, 11:36 AM

    Nobody knows what’s wrong here?
    Once again, I don’t see you creating your intersection object (nwGroupSubscribers) at all in that code. huh
    • Quote from: OpenGeek at Aug 05, 2010, 05:33 PM

      Once again, I don’t see you creating your intersection object (nwGroupSubscribers) at all in that code. huh

      Ah; "intersection object" got me thinking. smiley I’m used to CakePHP which handles intersection tables automatically. I didn’t realize I needed a special object for that in Modx.

      You mean I should do something like this (works, but doesn’t feel quite right)?

      <?php
      $modx->addPackage('newsletters', MODX_CORE_PATH.'/components/newsletters/model/');  
      
      $subscriber = $modx->newObject('nwSubscriber');
      $subscriber->set('name', 'Subscriber');
      $subscriber->set('email', '[email protected]');
      $subscriber->set('code', 'codet54tg4353wtgw34q');
      $subscriber->set('active', '0');
      $subscriber->save();
      
      for ($i=1;$i<10;$i++) {  
      	$group = $modx->newObject('nwGroup');  
      	$group->set('name', 'Group '.$i);
      	$group->save();
      	
      	$nwGroupSubscribers = $modx->newObject('nwGroupSubscribers');
      	$nwGroupSubscribers->set('group', $group->get('id'));
      	$nwGroupSubscribers->set('subscriber', $subscriber->get('id'));
      	$nwGroupSubscribers->save();
      }
      
      echo 'done';
      exit;
      ?>
      
        Jeroen Kenters

        MODX Professional | MODX Ambassador | Dutch MODX forums moderator

        website | twitter
      • That’s correct.

        You can hide this in your implementation by creating custom methods (or overriding the base methods) so they appear to bypass the intersection table objects and directly get the target collections when you need them, but xPDO doesn’t automatically handle this for you.
          • 24351
          • 15 Posts
          Hi Guys.

          I was following this Example: http://rtfm.modx.com/display/xPDO20/More+Examples+of+xPDO+XML+Schema+Files
          to reproduce the many to many example.

          I couldn’t find an example of addOne and addMany. So apparently it doesn’t automatically populate the "intersection" table with the id’s of the given insert.

          I am trying to make a simple n:m relationship.

          So with the above provided code I can obviously create the relation manually. But I just thought this is done automaticly.

          So why then to I set all these relations between each table in the schema.xml? Only for the getting purpose?

          Maybe someone can clarify this a bit for me or provide a link for further reading on n:m and 1:n relations with xpdo.

          Thanks you very much

          Cheers Ragnar

          This is my first post in this forum and I just want to mention that I am a huge fan of modx! Great work guys laugh
          • You are correct; the intersection table is up to you to manage. We are planning to provide some more example add-on base classes in the future that will abstract the need to manage specific types of relationships that require additional table structures such as many-to-many relations, closure tables for providing single-query access to an entire hierarchy, handling localization of translatable fields, or just about anything else you can design in a schema that you want to automate.