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.

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 username = 'username' *

user_name can be get from session data



14.3k

Not possible to customise ?



14.3k

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



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


14.3k

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

addWhere('username = "username"')

Which is wrong

andWhere('username = "username"')

is the correct.