Zend Framework is considered as one of the best PHP Framework where model view controller are used. Data base is handled through controller and controller is used for business logic. To Control database some time we use doctrine methodologies.Doctrine is used to interact with database and mapping is done in that context. I will post an article about doctrine as well but this time am going to show you that how zend controller uses doctrine to select query result. Here no model is being used.Controller creates an instance for Doctrine EntityManager and uses its select method.
Here is small  code
I am going to use select query that selects all states related to a  specific country.
Code is here
public function fetchStatesAction()
        $Request = $this->getRequest();
        $cList = array();
$States = ”;
//$rsm = new DoctrineORMEntityManager();
        $Repository = $this->getServiceLocator()->get(‘EntityManager’)->getRepository(‘AdministrationEntityState’);
        $queryBuilder = $Repository->createQueryBuilder(‘S’);
            $States = $queryBuilder->select(‘S’)
                            ->where(‘S.Country = :country_id’)
                            ->setParameter(‘country_id’, $Request->getPost(‘country_id’))
        if (count($States) > 0)
//            $States = $Country->getStates();
            foreach ($States as $State)
                $cList[$State->getId()] = $State->getName();
            echo json_encode($cList);


Leave a Reply

Your email address will not be published. Required fields are marked *