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

work with criteria

Hello all!!!

$query = Criteria::fromInput($this->di, 'Models\SomeModel', $this->request->getPost()); how to add an additional condition. for example $auth = $this->auth->get('auth-identity'); $additionalCondition = 'user_id = ' . $auth['user_id'];

and whether you can use a placeholder???



98.9k

You can create a query builder based on the parameters processed by Phalcon\Mvc\Model\Criteria:

$auth = $this->auth->get('auth-identity');

$criteria = Criteria::fromInput($this->di, 'Models\SomeModel', $this->request->getPost());

$result = $this->modelsManager
                    ->createBuilder($criteria->getParams())
                    ->andWhere('user_id = :user_id:', ['user_id' => $auth['user_id']]);
                    ->getQuery()
                    ->execute();


42.1k

Hello

I have the same issue, and I have the following code

        $criteria = Criteria::fromInput($this->di,'Categories', $this->request->getPost());
        $result = $this->modelsManager
        ->createBuilder($criteria->getParams())
        ->andWhere('languagekey = :languagekey:', ['languagekey' => 'en'])
        ->getQuery()
        ->execute();

Categories is my model (without namespaces), but executing this piece of code into my program It shows an empty screen with the following message:

At least one model is required to build the query

Do i forgot something?

Running code like this:

            $query = Criteria::fromInput($this->di, "Categories", $criteria);
            $extra = $this->modelsManager->createBuilder($query->getParams());
            $this->persistent->searchParams = $query->getParams();

works normal.