XF 2.0 How to check if a value exists before saving an entity

Matt C.

Well-known member
I'm trying to save content from a JSON file to an entity and each piece of content has its own id which is an 8 character string.

Here is part of the code that runs through each piece of content. When it runs through each piece of content, it's supposed to check if the id exists before saving the entity.

Code:
foreach($response['data'] as $key => $value)
{
    $entryId = $value['id'];
    $service = $value['account']['service'];
    $timestamp = $value['timestamp'];
    $nick = $value['account']['developer']['nick'];
    $role = $value['account']['developer']['role'];
    $content = $value['content'];
    $url = $value['url'];

    if ($params[$entryId])
    {
        $newEntry = $this->assertEntryExists($params[$entryId]);
    }
    else
    {
        $newEntry = \XF::app()->em()->create('AH\DevTracker:Entry');

        $newEntry->bulkSet([
            'entry_id' => $entryId,
            'service' => $service,
            'timestamp' => $timestamp,
            'nick' => $nick,
            'role' => $role,
            'content' => $content,
            'url' => $url
        ]);
    }

    $newEntry->save();
}

When it runs through each piece of content, its supposed to check if the id exists before saving the entity.

I keep getting this error:
Code:
All entry_id values must be unique.

Any help would be appreciated. Thank you.
 
its supposed to check if the id exists before saving the entity.
so the magic should happen here, i suppose:
PHP:
    if ($params[$entryId])
    {
        $newEntry = $this->assertEntryExists($params[$entryId]);
    }
why do you run $newEntry->save(); for each piece of content (i.e. on every loop and not just for non-exiting entries)?

have you tried to insert \XF::dump($entryId); in the if-condition I quoted above in order to see if the condition is ever met?
 
I'd use the finder of that entity, plus what @nocte already said.
PHP:
$entryFinder = \XF::app()->finder('AH\DevTracker:Entry')
    ->where('entry_id', (int)$entryId); // no need to cast it to an int if the input gets sanitized in the JSON parser or if you manually do that via the XF\Http\Request->filter method.

$entry = $entryFinder->fetchOne();
if (!$entry) {
    // entry record doesn't exist in table, insert and save it?
} else {
    // entry exists, update and save it or do something else.
}
 
Thanks guys for the response. I forgot to post that @Snog helped me out. Here is what we ended up doing:
Code:
if ($entryId)
{
    $newEntry = $this->finder('AH\DevTracker:Entry')->where('entry_id', $entryId)->fetchOne();
}

if ($newEntry)
{
    $newEntry->bulkSet([
        'timestamp' => $timestamp,
        'time' => $time,
            'service' => strtolower($service),
            'username' => $username,
            'nick' => $nick,
            'role' => $role,
            'content' => $content,
            'url' => $url
        ]);
    }
else
{
    $newEntry = \XF::app()->em()->create('AH\DevTracker:Entry');

    $newEntry->bulkSet([
        'entry_id' => $entryId,
        'timestamp' => $timestamp,
        'time' => $time,
        'service' => strtolower($service),
        'username' => $username,
        'nick' => $nick,
        'role' => $role,
        'content' => $content,
        'url' => $url
    ]);
}

$newEntry->save();
 
Back
Top Bottom