APPLY FOR ONLINE Zend Framework Certification

                    
                         How to use mysql in Zend Framework 2.

Step 1  create a new model called employee, and in my module.php file and  add:

public function getServiceConfig()
{
  return array(
    'factories' => array(
        'Application\Model\Employee' => function($sm) {
        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
        $table = new \Application\Model\Employee('employee', $dbAdapter);
         return $table;
},
),
);
}

In my Controller, I will connect the created model:

$select = $this->getCategoriesTable()->fetchAll();

Now, I will write a sql query in my model:

public function fetchAll()
{
$statement = $this->adapter->query('SELECT * FROM employee');
$results = $statement->execute();
return iterator_to_array($results); // will return an array
}
                    
                


Comment


Back to Top