What is the disadvantage of using abstract class as a database connectivity in zend framework 2 instead of service locator
- by arslaan ejaz
If I use database by creating adapter with drivers, initialize it in some abstract class and extend that abstract class to required model. Then use simple query statement. Like this:
namespace My-Model\Model\DB;
abstract class MysqliDB {
protected $adapter;
public function __construct(){
$this->adapter = new \Zend\Db\Adapter\Adapter(array(
'driver' => 'Mysqli',
'database' => 'my-database',
'username' => 'root',
'password' => ''
));
}
}
And use abstract class of database like this in my models:
class States extends DB\MysqliDB{
public function __construct(){
parent::__construct();
}
protected $states = array();
public function select_all_states(){
$data = $this->adapter->query('select * from states');
foreach ($data->execute() as $row){
$this->states[] = $row;
}
return $this->states;
}
}
I am new to zend framework, before i have experience of working in YII and Codeigniter.
I like the object oriented in zend so i want to use it like this.
And don't want to use it through service locater something like this:
public function getServiceConfig(){
return array(
'factories' => array(
'addserver-mysqli' => new Model\MyAdapterFactory('addserver-mysqli'),
'loginDB' => function ($sm){
$adapter = $sm->get('addserver-mysqli');
return new LoginDB($adapter);
}
)
);
}
In module.
Am i Ok with this approach?