MODX Community

Check to see if there are duplicate resources

Hello Community,

I currently have a snippet with the following code:

  $doc = $modx->newObject('modDocument');
  $doc->set('pagetitle', 'Object' . $pId);
  $doc->set('longtitle', 'Object' . $pId);
  $doc->set('alias', 'Object pagina' . $pId);
  $doc->set('description','Object' . $pId);
  $doc->set('template', '2');
  $doc->set('published', 1);
  $doc->setContent($content);
  $doc->save();

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?

If you want to prevent the refresh creation of another duplicate: save the ID in a cookie or something, check it’s page title against the resource you try to make:

$page = $modx->getObject('modResource', $ID);
$output = $page->get('pagetitle');
1 Like

First check if there is already a resource with the same fields you are adding.

if ($doc != $getObject('modResource', ['pagetitle' =>  'Object' . $pId, 'alias' =>  'Object pagina' . $pId ])) {
$doc = $modx->newObject('modDocument');
  $doc->set('pagetitle', 'Object' . $pId);
  $doc->set('longtitle', 'Object' . $pId);
  $doc->set('alias', 'Object pagina' . $pId);
  $doc->set('description','Object' . $pId);
  $doc->set('template', '2');
  $doc->set('published', 1);
  $doc->setContent($content);
  $doc->save();
} else {
// you could update the existing document here
$doc->set(‘pagetitle’, ‘Object’ . $pId);
$doc->set(‘longtitle’, ‘Object’ . $pId);
$doc->set(‘alias’, ‘Object pagina’ . $pId);
$doc->set(‘description’,‘Object’ . $pId);
$doc->set(‘template’, ‘2’);
$doc->set(‘published’, 1);
$doc->setContent($content);
$doc->save();
}
1 Like

Thanks for this piece of code, this does look like it is what I want it to be, but when I add this to the page, I get a HTTP ERROR 500 saying that it can’t process the request at the moment.

Where would I go about adding this? Before the $doc->save(); or afterwards?

What php version do you use?
If not 7+ then try this:

if ($doc != $getObject('modResource', array('pagetitle' =>  'Object' . $pId, 'alias' =>  'Object pagina' . $pId ))) {
$doc = $modx->newObject('modDocument');
  $doc->set('pagetitle', 'Object' . $pId);
  $doc->set('longtitle', 'Object' . $pId);
  $doc->set('alias', 'Object pagina' . $pId);
  $doc->set('description','Object' . $pId);
  $doc->set('template', '2');
  $doc->set('published', 1);
  $doc->setContent($content);
  $doc->save();
} else {
$doc->set(‘pagetitle’, ‘Object’ . $pId);
$doc->set(‘longtitle’, ‘Object’ . $pId);
$doc->set(‘alias’, ‘Object pagina’ . $pId);
$doc->set(‘description’,‘Object’ . $pId);
$doc->set(‘template’, ‘2’);
$doc->set(‘published’, 1);
$doc->setContent($content);
$doc->save();
}

This will be instead of the code you posted in the first post.

1 Like

I can’t find what version of PHP I am on, but I am running MODX Revolution 2.7.3-pl at the moment, and I replaced

  $doc = $modx->newObject('modDocument');
  $doc->set('pagetitle', 'Object' . $pId);
  $doc->set('longtitle', 'Object' . $pId);
  $doc->set('alias', 'Object pagina' . $pId);
  $doc->set('description','Object' . $pId);
  $doc->set('template', '2');
  $doc->set('published', 1);
  $doc->setContent($content);
  $doc->save();

With your code

if ($doc != $getObject('modResource', array('pagetitle' =>  'Object' . $pId, 'alias' =>  'Object pagina' . $pId ))) {
$doc = $modx->newObject('modDocument');
  $doc->set('pagetitle', 'Object' . $pId);
  $doc->set('longtitle', 'Object' . $pId);
  $doc->set('alias', 'Object pagina' . $pId);
  $doc->set('description','Object' . $pId);
  $doc->set('template', '2');
  $doc->set('published', 1);
  $doc->setContent($content);
  $doc->save();
} else {
$doc->set(‘pagetitle’, ‘Object’ . $pId);
$doc->set(‘longtitle’, ‘Object’ . $pId);
$doc->set(‘alias’, ‘Object pagina’ . $pId);
$doc->set(‘description’,‘Object’ . $pId);
$doc->set(‘template’, ‘2’);
$doc->set(‘published’, 1);
$doc->setContent($content);
$doc->save();
}

Which still results in the same error, I thought it had to do with $getObject so changed it to $modx->getObject but without success.

Okay, might have cheered a bit too early :frowning: 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?

Maybe it would be enough just to check the pagetitle:

$doc = $modx->getObject('modResource', array('pagetitle' => $pagetitle));

if (! ($doc)) {
    /* Go ahead and create it */
}

FWIW, $pId is not a good name for the pagetitle variable, since it implies that you’re using the ID, not the pagetitle.

1 Like

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 :stuck_out_tongue_winking_eye: 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.

1 Like

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.

You shouldn’t need a list of all resources.

If you’re creating a resource, you must know its pagetitle.

So, you can just do this:

if (! $modx->getObject('modResource', array('pagetitle' => 'Object' . $pId))) {
  /* Create it */
}
1 Like

Amazing! Thanks, now it works. Thanks a bunch :slight_smile:

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.

Erm … if you’re not inside the if statement, there is no new resource – hence no id.

Yeah, just noticed haha… thanks for everything :slight_smile:

Glad I could help. :slight_smile: