Apply for Zend Framework Certification Training

Zend Framework 2




 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
}
                                    

< First Last >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top