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

Customising Scaffold code

<?php
$this->view->title = 'Search Customer ::';
        $numberPage = 1;
        if ($this->request->isPost()) 
        {
            $query = Criteria::fromInput($this->di, "Customer", $_POST);
            $this->persistent->parameters = $query->getParams();
        } 
        else 
        {
            $numberPage = $this->request->getQuery("page", "int");
        }

        $parameters = $this->persistent->parameters;
        $parameters["order"] = "cus_id";

        if (!is_array($parameters)) 
        {
            $parameters = array();
        }

        var_dump($query->getParams());

        $customer = Customer::find($parameters);

This is a slightly modified scaffold code to search. I want to add one more condition to the query. The above code will return result for the query

  • select from customer where name like '%foo%' I wanto to change the query into something like

  • select from customer where name like '%foo%' and user_name = 'user_name'

user_name can be get from session data



24.2k

Not possible to customise ?



24.2k

I tried the code from the link. As the last post says, I am also getting 'At least one model is required to build the query' error



5.5k
Accepted
answer

Ah I see; I didn't test this before. Seems to me this functionality is not quite working as it should.

This works for me but seems to miss the point of using the Criteria object in the first place;

<?php

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

        $params = $criteria->getParams();

        $result = $this->modelsManager
            ->createBuilder()
            ->from($criteria->getModelName())
            ->where($criteria->getConditions(), $params['bind'])
            ->addWhere('user_name = "user_name"')
            ->getQuery()
            ->execute()
        ;


24.2k

Thanks, now it's working. How to add more conditions. Just for knowing. Also there is a typo

addWhere('user_name = "user_name"')

Which is wrong

andWhere('user_name = "user_name"')

is the correct.