Check if content exist in database

Hello,

I'm currently converting my project to Phalcon PHP. In my old project I did a if to check if the conetnt I'm pulling from a API exist in a database, if it didn't I would add it to the DB and display the fresh content I just got from the API.

How could I do something similar to this with Phalcon?



19.5k

See this for working with models http://docs.phalconphp.com/es/latest/reference/models.html

// (..) => Inside put the binding params.
$apiContent = ApiContent::findFirst(..);

// If it is false, there isn't such record in the table. Then we prepare it to insert in the DB.
if (!$apiContent)
{
    $apiContent = new ApiContent();

    // Here do SETs.

    if ($apiContent->create()) // or '$apiContent->save()'.
    {
        // Set $apiContent to what do you want to display. This can be setting the var in the view, for example.
    }
    else
    {
        // Throw an exception, show a message error, etc.
    }
}