<![CDATA[ Create ressource and get id - My Forums]]> https://forums.modx.com/thread/?thread=103221 <![CDATA[Create ressource and get id]]> https://forums.modx.com/thread/103221/create-ressource-and-get-id#dis-post-555446
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?]]>
joe-petts Nov 24, 2017, 02:54 PM https://forums.modx.com/thread/103221/create-ressource-and-get-id#dis-post-555446
<![CDATA[Re: Create ressource and get id]]> https://forums.modx.com/thread/103221/create-ressource-and-get-id#dis-post-558384 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.]]>
wshawn May 05, 2018, 03:35 PM https://forums.modx.com/thread/103221/create-ressource-and-get-id#dis-post-558384
<![CDATA[Re: Create ressource and get id]]> https://forums.modx.com/thread/103221/create-ressource-and-get-id#dis-post-558100
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.
]]>
BobRay Apr 18, 2018, 05:03 PM https://forums.modx.com/thread/103221/create-ressource-and-get-id#dis-post-558100
<![CDATA[Re: Create ressource and get id]]> https://forums.modx.com/thread/103221/create-ressource-and-get-id#dis-post-558094
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?]]>
nazim13 Apr 18, 2018, 03:42 PM https://forums.modx.com/thread/103221/create-ressource-and-get-id#dis-post-558094
<![CDATA[Re: Create ressource and get id]]> https://forums.modx.com/thread/103221/create-ressource-and-get-id#dis-post-558065
$bind = $this->_prepareDataForSave($object);
if ($this->_isPkAutoIncrement) {
unset($bind[$this->getIdFieldName()]);
}
$this->getConnection()->insert($this->getMainTable(), $bind);]]>
nazim13 Apr 17, 2018, 11:04 AM https://forums.modx.com/thread/103221/create-ressource-and-get-id#dis-post-558065
<![CDATA[Re: Create ressource and get id]]> https://forums.modx.com/thread/103221/create-ressource-and-get-id#dis-post-555465
Thank you Bruno for this fast solution!]]>
joe-petts Nov 27, 2017, 08:50 AM https://forums.modx.com/thread/103221/create-ressource-and-get-id#dis-post-555465
<![CDATA[Re: Create ressource and get id (Best Answer)]]> https://forums.modx.com/thread/103221/create-ressource-and-get-id#dis-post-555447
$doc->save();
$id = $doc->get('id');

]]>
Bruno17 Nov 24, 2017, 04:27 PM https://forums.modx.com/thread/103221/create-ressource-and-get-id#dis-post-555447