We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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?



33.8k

See this for working with models https://docs.phalcon.io/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.
    }
}