Hello! I try to access data from several MySQL tables. Base in Denver. In the standard Zend2 tutorial in Album, the reference is built through TableGateway, as I understand it, that you cannot access several tables through it. Therefore, I do this:
I create a factory for the adapter to the database:
return array( 'service_manager' => array( 'factories' => array( 'ZendDbAdapterAdapterBarcervice' => function($sm){ $config = $sm->get('Config'); return new Zend\Db\Adapter\Adapter($config['dbconf2']); }, ), ), );
I prescribe dbconf2 settings:
return array( 'dbconf2' => array( 'driver' => 'Pdo', 'dsn' => 'mysql:dbname=bar_service;host=localhost', 'driver_options' => array( PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'" ), 'username' => 'root', 'password' => '', ), ); In the module I prescribe the getServiceConfig () method:
public function getServiceConfig() { return array( 'factories' => array( 'BarcerviceModelBarcerviceSql' => function($sm){ $sqlGateway = $sm->get('BarcerviceSQLService'); $sql = new Model\BarcerviceSql($sqlGateway); return $sql; }, 'BarcerviceSQLService' => function($sm){ $dbAdapter = $sm->get('ZendDbAdapterAdapterBarcervice'); return new Sql($dbAdapter); } ), ); } in the controller, I prescribe the getSQLGateway () method to get the gateway to the database:
public function getSQLGateway() { if (!$this->sqlGateway){ $sm = $this->getServiceLocator(); $this->sqlGateway = $sm->get('Barcervice\Model\BarcerviceSql'); } return $this->sqlGateway; } and use it in the controller to initialize the model's functional:
public function indexAction() { $this->initialise(); $answer = $this->model->getAllCableTypes(); $form = new BarcerviceForm(); var_dump($answer); return array('form' => $form); } So, the model->getAllCableTypes() method refers to the sql->getAllCabletypes() method - in the code of this method, the functional that should actually perform queries to the database:
public function getAllCableTypes() { $select = $this->sql->select()->from('cable_types'); $statement = $this->sql->prepareStatementForSqlObject($select); $result = $statement->execute(); return $result; } The documentation says that the execute() method should return an object of the ResultSet class, and it returns an object that contains nothing but connection settings:
object(Zend\Db\ResultSet\ResultSet)#257 (8) { ["allowedReturnTypes":protected]=> array(2) { [0]=> string(11) "arrayobject" [1]=> string(5) "array" } ["arrayObjectPrototype":protected]=> object(ArrayObject)#248 (1) { ["storage":"ArrayObject":private]=> array(0) { } } ["returnType":protected]=> string(11) "arrayobject" ["buffer":protected]=> NULL ["count":protected]=> int(1) ["dataSource":protected]=> object(Zend\Db\Adapter\Driver\Pdo\Result)#256 (8) { ["statementMode":protected]=> string(7) "forward" ["resource":protected]=> object(PDOStatement)#255 (1) { ["queryString"]=> string(25) "SELECT * FROM cable_types" } ["options":protected]=> NULL ["currentComplete":protected]=> bool(false) ["currentData":protected]=> NULL ["position":protected]=> int(-1) ["generatedValue":protected]=> string(1) "0" ["rowCount":protected]=> int(1) } ["fieldCount":protected]=> int(3) ["position":protected]=> int(0) } It seems that I am doing something wrong ... So how can I still access the database through the Zend\Db\Sql\Sql class?