Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Run executeQuery in function in Micro App

I have followed the API tutorial.

I have everything working.

Now I am creating an authentication function that I will call at the beginning of each api call.

Example:

$app->get('/api/robots', function() use ($app) {
 $auth_data=is_authorized($app);
 ...
 ..

So now in the "index.php" file I have a function titled "is_authorized"

The function runs fine however I cannot execute SQL within this function.

The following example does not execute within the is_authorized function.

$phql = "SELECT * FROM Members ORDER BY id";
            $robots = $app->modelsManager->executeQuery($phql);

While the above will run within my initial api call function it does not run in the is_authorized function. Is there a way to pass "$app" to this function so that I can use it to execute queries?



3.9k
Accepted
answer

This issue was unrelated to Phalcon and more of a issue with my understanding of how to pass by reference in PHP 5.4.

See this post for the reason on why I was running into the issue.

http://stackoverflow.com/questions/8971261/php-5-4-call-time-pass-by-reference-easy-fix-available

Please close the thread if you solved your problem. If you are still looking for a solution take a look at the following code:

class BaseController extends \Phalcon\Mvc\Controller
{
    private function isAuthorized()
    {
        //Here you can execute your PHQL queries
    }

    public function initialize()
    {
        $auth_data = $this->isAuthorized();
        //[...]
    }
}

class RandomController extends BaseController
{
    //Your API actions
}