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-ModelModelDB;
abstract class MysqliDB {
protected $adapter;
public function __construct(){
$this->adapter = new ZendDbAdapterAdapter(array(
'driver' => 'Mysqli',
'database' => 'my-database',
'username' => 'root',
'password' => ''
));
}
}
And use abstract class of database like this in my models:
class States extends DBMysqliDB{
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 ModelMyAdapterFactory('addserver-mysqli'),
'loginDB' => function ($sm){
$adapter = $sm->get('addserver-mysqli');
return new LoginDB($adapter);
}
)
);
}
In module.
Am i Ok with this approach?
Abstract classes don’t share.
The primary benefit of the service locator in this instance is that a request will only open a single adapter to the database and share that adapter with all objects that request it. Whereas in your structure of extending from abstract classes each instance will setup a new adapter to run its statements against the database.
$states1 = new States();
$states2 = new States();
Right there are two separate database connections.
0