-4

How to query below in zend 2

select * from states st where TRIM(LOWER(st.state_name))='noida'

Any help is appreciated. Thanks

Arvind Jha
  • 62
  • 1
  • 10

3 Answers3

0

Use following:

$resultStates=$this->states->select()->where('TRIM(LOWER(st.state_name))=?','noida')
    ->query()
    ->fetchAll();

For details refer Here and Here.

Community
  • 1
  • 1
Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
0
/* DB Adapter get and SQL object create */
$adapter = GlobalAdapterFeature::getStaticAdapter();
$sql = new \Zend\Db\Sql\Sql($adapter);

/* Select object create */
$select = new \Zend\Db\Sql\Select();
$select->from('states');
$select->where->addPredicate(
    new \Zend\Db\Sql\Predicate\Expression(
        'TRIM(LOWER(state_name)) = ?',
        'noida'
    )
);

/* Select object convert to string and execute */
$queryString = $sql->getSqlStringForSqlObject($select);
$result = $adapter->query($queryString, Adapter::QUERY_MODE_EXECUTE);
newage
  • 899
  • 7
  • 18
0

In you model file just use below code here I am using module profile.

Profile/Model/Common.php

namespace Profile\Model;

use Zend\Db\Sql\Sql;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Select;

class Common 
{
        protected $dbConfig;
        protected $adapter;

        public function __construct($dbConfig)
         {
            $this->adapter = new Adapter($dbConfig);
         }
        public function getStateList()
         {

            $sql  = "select * from states st where TRIM(LOWER(st.state_name))='noida'";
            $statement = $this->adapter->query($sql);

            $results = $statement->execute();

            $resultSet = new ResultSet();
            $resultSet->initialize($results);
            $list = $resultSet->toArray();

            return $list; // This will return a list of array
        }

}       

Profile/Controller/IndexController

namespace Profile\Controller;
use Profile\Model\Common;


class IndexController extends AbstractActionController
{


    protected $dbConfig = array(
                        'driver' => DRIVER,
                        'database' => DB,
                        'username' => DBUSER,
                        'password' => DBPASS
                     );

    public function init(){
    $ssOrder = new Container(__CLASS__);

        //SET OPTIONS 
    }


    public function indexAction()
    {
        $plist = new Common($this->dbConfig);
        $resultList = $plist->getStateList(); // This will give you state list


    }


}

Good Luck