We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20025
    • 6 Posts
    Methods like getCollection, getIterator etc. returns array of xPDO Criteria objects. I thought what work with one of these objects is identical to objects, getted by getObject.. Unfortunally, ->save() method works only in object, getted by getObject - it is a bug or "feature"?

    I expected what my code should be like this (1 select + N update queries):
    $linkpage = $this->xpdo->getIterator('Linkpages', $this->dbQuery);
    foreach ($linkpages as $linkpage) {
     $linkpage->set('content', $this->content);
     $linkpage->save();
    }
    


    But currently works only this solution (1+N select + N update queries):
    $linkpage = $this->xpdo->getIterator('Linkpages', $this->dbQuery);
    foreach ($linkpages as $linkpage) {
     $updatable_linkpage = $this->xpdo->getObject('Linkpages', $linkpage->id_linkpage);
     $updatable_linkpage->set('content', $this->content);
     $updatable_linkpage->save();
    }
    


    So it's very unoptimized. Especially when such situations is usual.
    Is there any other easy solution to "do more with less"?
      UNSIGNED, NOT NULL
      • 22303 MODX Staff
      • 10,725 Posts
      Should work with the objects returned from getIterator() or getCollection(). I don't see any obvious reason why it would not, but if you
      $this->xpdo->setDebug(true);
      you might be able to see more about what is happening to your model.
        • 20025
        • 6 Posts
        $linkpage->set('content', 'test saving content');
        returns 1

        $linkpage->save();
        fails:
        (ERROR @ /p/test.php) Attempt to save lazy object: Array
        (
            [id_linkpage] => 1
            [tstmp] => CURRENT_TIMESTAMP
            [status] => 
            [cr_type] => 
            [cr_option] => 
            [id_location] => 
            [id_location_parent] => 
            [page] => 
            [link] => 
            [content] => test saving content
        )


        why object is lazy? smiley
          UNSIGNED, NOT NULL
          • 22303 MODX Staff
          • 10,725 Posts
          Have you used select() on your xPDOQuery that is being passed as $this->dbQuery? In order to save changes to an object, you must load all the fields from the table that represent the object, otherwise you have what are known as lazy-loaded fields. IOW, if you request data from a lazy field, another query is sent off to retrieve that field's data and populate it on the object. Once that is done, the field is no longer lazy. But as long as any field is lazy, you will not be able to save changes you make to the object.
            • 20025
            • 6 Posts
            thanks! i've used "select('id_linkpage, cr_type, content')", without it save() works.

            but also i tested my previous dbQuery (with that select) on getObject - it returns object only with selected fields, but it's not lazy and save() works too... next i tested saving field, not mentioned in select - it also works...
            weird, right?

            i think what in situation, when we got collection with some selected fields, possibility to save that fields is logically right. hope this will be in future xPDO releases smiley [ed. note: modxor last edited this post 15 years, 1 month ago.]
              UNSIGNED, NOT NULL