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

Cannot find the method findFirstById() in models how to use this findFirstByName findFirstById ...

$post = Posts::findFirstById($id);
if (!$post) {
    $this->flashSession->error('The discussion does not exist');
    return $this->response->redirect();
}

More details please.

You can use findFirst to find by id or by name:

$post = Posts::findFirst($id);
// or by name
$post = Posts::findFirst(["name = :name", "bind" => ["name" => $name]]);

if (!$post) {
    $this->flashSession->error('The discussion does not exist');
    return $this->response->redirect();
}

Lastly, there is the findFirstBy<property-name>() method. This method expands on the “findFirst()” method mentioned earlier. It allows you to quickly perform a retrieval from a table by using the property name in the method itself and passing it a parameter that contains the data you want to search for in that column.

class Robots extends \Phalcon\Mvc\Model
{
    public $id;

    public $name;

    public $price;
}

We have three properties to work with here. $id, $name and $price. So, let’s say you want to retrieve the first record in the table with the name ‘Terminator’. This could be written like so :

$name = "Terminator";
$robot = Robots::findFirstByName($name);

This is from the doc: https://docs.phalcon.io/en/latest/reference/models.html#finding-records