What is the disadvantage of using abstract class as a database connectivity in zend framework 2 instead of service locator

Posted by arslaan ejaz on Programmers See other posts from Programmers or by arslaan ejaz
Published on 2013-10-21T06:01:22Z Indexed on 2013/10/21 10:14 UTC
Read the original article Hit count: 266

Filed under:
|
|

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?

© Programmers or respective owner

Related posts about database

Related posts about mysql