On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • Hello community,

    i try to create new ressource und then get the id:

    this is my snippet:
      
    // Part 1  
        $pagetitle = "my ressource";
        $parent = "0";
        $alias = "myalias";
    
    $doc = $modx->newObject('modDocument');
        $doc->set('parent',$parent);
        $doc->set('pagetitle',$pagetitle);
        $doc->set('alias',$alias);
        $doc->save();
      
    // Part
        $newdoc = $modx->getObject('modDocument', array('pagetitle'=>$pagetitle));
        $id = $newdoc->get('id');
    


    problem: the second part get the id from ressource with pagetitle "my ressource". If i have many ressource with the same pagetitle, then it doesn't work. Know anybody better solution?

    This question has been answered by Bruno17. See the first response.

    • discuss.answer
      you can get the id directly from the saved object.
      $doc->save();
      $id = $doc->get('id');
      

        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
      • Wow, its so easy!

        Thank you Bruno for this fast solution!
        • We need to overwrite default saveNewObject method to get rid of the unsetting pre-specified ID.

          $bind = $this->_prepareDataForSave($object);
          if ($this->_isPkAutoIncrement) {
          unset($bind[$this->getIdFieldName()]);
          }
          $this->getConnection()->insert($this->getMainTable(), $bind); [ed. note: nazim13 last edited this post 1 year, 4 months ago.]
          • Hello community,

            i try to create new ressource und then get the id:

            this is my snippet:
              
            // Part 1  
                $pagetitle = "my ressource";
                $parent = "0";
                $alias = "myalias";
            
            $doc = $modx->newObject('modDocument');
                $doc->set('parent',$parent);
                $doc->set('pagetitle',$pagetitle);
                $doc->set('alias',$alias);
                $doc->save();
              
            // Part
                $newdoc = $modx->getObject('modDocument', array('pagetitle'=>$pagetitle));
                $id = $newdoc->get('id');
            


            problem: the second part get the id from ressource with pagetitle "my ressource". If i have many ressource with the same pagetitle, then it doesn't work. Know anybody better solution?
            • If you know the resource IDs, you can used those for your search criteria since they will be unique.

              If the resources are in different contexts, you can also specify the context_key in your search criteria.

              You can also get all the resources with the same pagetitle by using getCollection() instead of getObject().

              If the resources have different aliases, you can search for them by alias.

              Or you could just stop using duplicate pagetitles.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
              • W. Shawn Wilkerson Reply #7, 1 year, 3 months ago
                Quote from: nazim13 at Apr 17, 2018, 11:04 AM
                We need to overwrite default saveNewObject method to get rid of the unsetting pre-specified ID.

                $bind = $this->_prepareDataForSave($object);
                if ($this->_isPkAutoIncrement) {
                unset($bind[$this->getIdFieldName()]);
                }
                $this->getConnection()->insert($this->getMainTable(), $bind);

                If I understand what you are doing here, maybe you should have extended xPDOObject and created your own PK instead of going through all of that. It is all in how you define it. I personally stick to the xPDOSimpleObject more often than not and just work with it.
                  Get your copy of MODX Revolution Building the Web Your Way http://www.sanitypress.com/books/modx-revolution-building-the-web-your-way.html

                  Check out my MODX || xPDO resources here: http://www.shawnwilkerson.com