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

Phalcon\Mvc\Model\Resultset\Simple::success()

I'm creating a simple REST API signin and got this error in success() method: Uncaught Error: Call to undefined method Phalcon\Mvc\Model\Resultset\Simple::success() in F:\xampp\htdocs\phalcon-api\index.php:101 The code is given below:

<?php

use Phalcon\Loader; use Phalcon\Mvc\Micro; use Phalcon\Di\FactoryDefault; use Phalcon\Db\Adapter\Pdo\Mysql as PdoMysql; use Phalcon\Http\Response;

// Post API Signin
$app->post(
    "/signin",
    function () use ($app) {
        $robot = $app->request->getJsonRawBody();

        $phql = "SELECT * FROM Store\\Toys\\user WHERE email = :email: AND password = :password:";

        $status = $app->modelsManager->executeQuery(
            $phql,
            [
                "email" => $robot->email,
                "password" => $robot->password,
            ]
        );

        // Create a response
        $response = new Response();

        // Check if the insertion was successful
        if ($status->success() === true) {
            // Change the HTTP status
            $response->setStatusCode(201, "Created");

            $robot->u_id = $status->getModel()->u_id;

            $response->setJsonContent(
                [
                    "status" => "OK",
                    "data"   => $robot,
                ]
            );
        } else {
            // Change the HTTP status
            $response->setStatusCode(409, "Conflict");

            // Send errors to the client
            $errors = [];

            foreach ($status->getMessages() as $message) {
                $errors[] = $message->getMessage();
            }

            $response->setJsonContent(
                [
                    "status"   => "ERROR",
                    "messages" => $errors,
                ]
            );
        }

        return $response;
    }
);

$app->handle();


77.7k
Accepted
answer

Where did you get that success from? :P

As show by the error message, executeQuery returns a Phalcon\Mvc\Model\Resultset\Simple insance, which has no success method...

See the docs: https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Model_Resultset_Simple.html

Your comment says Check if the insertion was successful, but you are issuing a select statement. What are you trying to achieve with calling success? Checking if a row exists? Then you should do this:

        // Check if the SELECT returned any rows
        if ($status->count() > 0) {
            // login
        } else {
            // invalid credentials
        }