I am quite confused about how to inject (Zend/Apigility) in a mapper class a specific db adapter (here named music) concerning following mapper:
AlbumMapper.php
<?php
namespace music\V1\Rest\Album;
use Zend\Db\Sql\Select;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Paginator\Adapter\DbSelect;
class AlbumMapper
{
protected $adapter;
public function __construct(AdapterInterface $adapter)
{
$this->adapter = $adapter;
}
public function fetchAll()
{
$select = new Select('album');
$paginatorAdapter = new DbSelect($select, $this->adapter);
$collection = new AlbumCollection($paginatorAdapter);
return $collection;
}
public function fetchOne($id)
{
$sql = 'SELECT * FROM album WHERE id = ?';
$resultset = $this->adapter->query($sql, array($id));
$data = $resultset->toArray();
if (!$data) {
return false;
}
$entity = new AlbumEntity();
$entity->exchangeArray($data[0]);
return $entity;
}
}
Module.php
public function getServiceConfig()
{
return array(
'factories' => array(
'music\V1\Rest\Album\AlbumMapper' => function ($sm) {
$adapter = $sm->get('Zend\Db\Adapter\Adapter');
return new \music\V1\Rest\Album\AlbumMapper($adapter);
},
),
);
}
AlbumResourceFactory.php
<?php
namespace music\V1\Rest\Album;
class AlbumResourceFactory
{
public function __invoke($services)
{
$mapper = $services->get('music\V1\Rest\Album\AlbumMapper');
return new AlbumResource($mapper);
}
}
This will fetch the db adapter credentials from config/autoload/user.global.php file which is according to this file comment not an appropriate way when it comes to productive mode.