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 = 'userid = ' . $auth['userid'];

and whether you can use a placeholder???



84.1k

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();


25.4k

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.