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

    I’ve got an object related by one->many relationships to several other objects, e.g.:


    If I submit a form which changes the Regions a Voucher is associated with (e.g. so the Voucher goes from being associated with London & Birmingham to London, Birmingham & Manchester), first of all links to any Regions that have been removed are deleted from the database, and then new links are added with the following:

    while($newRegion = array_pop($newRegions))
    	$obj = $this->xpdo->newObject('VoucherRegion');
    	$obj->set('voucher', $this->getPrimaryKey());
    	$obj->set('region', $newRegion);

    A new object is created, populated and saved.

    Later on, when I try to retrieve all of the Voucher’s relationships to Regions (with $regions = $this->getMany(’VoucherRegion’)), and then iterate through the retrieved objects, none of the newly added regions are returned.

    I haven’t set up caching anywhere, and if I query the database directly, it says that the correct number of entries are in the database, so the save() method is being executed correctly.

    If I refresh the page, the Regions are returned correctly.

    Is there any way I can force xPDO to refresh its cache? Or is the call wrong? Perhaps it isn’t a caching issue since if I remove a Region, that one isn’t returned when I call getMany(). It’s just newly added Regions that aren’t returned. I’m not setting a cacheFlag anywhere either.

    Any help would be appreciated.
    • I don’t understand, are you using result-set caching? And where is that you are adding related objects? $this->addMany() doesn’t look valid? What is $this referring to? Shouldn’t it just be some other $parentObj->addMany($obj) or is $this an xPDOObject? Also, I’d do it all at once, with the entire collection of VoucherRegion objects, i.e. $collection[]= $obj;, and then $parentObj->addMany($collection); after the while loop?
        • 24719
        • 194 Posts
        Oh yeah, I’d forgotten I could add a collection of related objects. That’ll be better.

        Yes, the $this refers to an instance of a Voucher, which extends xPDOObject.

        I’m not sure if I’m using result-set caching - I’m not using it intentionally. How would I find out?

        Re-reading the example for related objects, I don’t need to add the primary key of the parent object do I? Shouldn’t it be more like:

        $obj = $this->xpdo->newObject('VoucherRegion');
        $obj->set('region', $newRegion);


        • Correct, the addMany should set the foreign key. To turn on result-set caching, first, make sure you enable the cache by loading the cacheManager before performing any queries:

          $cacheManager= $xpdo->getCacheManager();

          Then you can control caching using the third boolean parameter in $xpdo->getObject(’class’, $criteria, true), and the only parameter of $anXPDOObj->save(true), known as $cacheFlag. In MODx, this is set explicitly to true, but controlled globally via a config setting. xPDO does not automatically cache these calls, so you must explicitly call save(), getObject() or getCollection() with these flags to cache a specific result, or look for it in cache.
            • 24719
            • 194 Posts
            ok thanks. i’m learning a little more every day wink