This is inside a foreach loop since I am retrieving data from a 3rd party API, so for every object it finds that is being returned by the API I make a new resource. The problem now is that if I refresh the page it makes the same resource again, lets say Object756093. Now, I want to add a check that when that resource already exists it skips creating the resource to avoid duplicates. How would I go about doing this?
Okay, might have cheered a bit too early now it does not create a resource when there isn’t a previously defined one yet.
I figured it had something to do with $doc not being recognized in the else statement so added $doc = $modx->newObject('modDocument'); for testing purposes and then it loads the page, so how can I make it so that $doc is going to contain the resource that does not need to be added?
Thanks for the answer, the variable $pId isn’t the pagetitle it is the presentation id from the API, but you couldn’t have known that So… at the moment I don’t have the pagetitle of the resource that needs to be created, I have tried a couple of things that I thought might work, but I never get further then getting the current pagetitle of the page where the call to the api is being made on and not all pagetitles, so how would I go about getting the pagetitle for all the resources currently in use?
I’m not sure what you mean by “all the resources currently in use.” In order to get a particular resource and find out what it’s pagetitle is, you need to know at least one thing about it - its ID or alias are typical references.
You could get all the Resources under a particular parent or all resources at the site pretty easily, but I’m not sure that’s what you want.
You can also get a resource you’ve just created, but in that case you’d already know the pagetitle.
This actually is what I want, because say I have a resource that just got created because it doesn’t exist in the resource list, I don’t want it to be created again on refresh. So then I would need a list of all resources and check if the new resource already exists and if not, make a new resource.
How could I then get the resource ID of the newly created resource? I am trying to do it after the $newDoc->save(); by typing $newResourceID = $newDoc->get('id'); and this works but that is inside of the if, but when there are no new resources to be created the if (!$modx->getObject('modResource', array('pagetitle' => 'Object' . $pId))) will fail and not store the id in $newResourceID.