Thanks @matthew for your answer, i didn’t knew this naming strategy but sadly it change nothing with my problem… is same result then with ObjectProperty or ClassMethods. Is like it accept only “id” parameter or nothing with “_” underscore for id relation in HAL.
Below i more complete example…
Database table fields:
Routes:
$app->get('/customers', App\Action\AllCustomerAction::class, 'customers.get');
$app->get('/customer/{customer_id}', App\Action\CustomerAction::class, 'customer.get');
Entity:
namespace App\Entity;
class Customer
{
public $customerId;
public $name;
}
Hydrator:
namespace App\Hydrator;
use Zend\Hydrator\ObjectProperty;
use Zend\Hydrator\NamingStrategy\UnderscoreNamingStrategy;
class DefaultHydrator extends ObjectProperty
{
public function __construct()
{
parent::__construct();
$this->setNamingStrategy(new UnderscoreNamingStrategy());
return $this;
}
}
Model:
namespace App\Model\Factory;
...
class CustomerTableFactory
{
public function __invoke($container)
{
// Get Adapter
$dbAdapter = $container->get(AdapterInterface::class);
//$hydrator = new ClassMethods(true);
//$hydrator = new ObjectProperty();
$hydrator = new DefaultHydrator();
$resultSet = new HydratingResultSet($hydrator, new Customer());
// Create TableGateway
$tableGateway = new TableGateway('customer', $dbAdapter, null, $resultSet);
return new CustomerTable($tableGateway);
}
}
…
namespace App\Model;
...
class CustomerTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchOne($id)
{
$id = (int) $id;
$rowset = $this->tableGateway->select(
['customer_id' => $id]
);
$row = $rowset->current();
if (!$row) {
return false;
}
return $row;
}
public function fetchAll()
{
$select = $this->tableGateway->getSql()->select();
$paginatorAdapter = new DbSelect(
$select,
$this->tableGateway->getAdapter(),
$this->tableGateway->getResultSetPrototype()
);
return new EntityCollection($paginatorAdapter);
}
}
Collection:
namespace App\Collection;
use Zend\Paginator\Paginator;
class EntityCollection extends Paginator
{
}
MetadataMap:
return [
MetadataMap::class => [
[
'__class__' => RouteBasedResourceMetadata::class,
'resource_class' => Customer::class,
'route' => 'customer.get',
'extractor' => DefaultHydrator::class
],
[
'__class__' => RouteBasedCollectionMetadata::class,
'collection_class' => EntityCollection::class,
'collection_relation' => 'customer',
'route' => 'customers.get',
],
]
];
Result for localhost:8080/customer/1 → OK
{
"_links": {
"self": {
"href": "http://localhost:8080/customer/1"
}
},
"customer_id": "1",
"name": "Test"
}
Result for localhost:8080/customers → FAIL
I tried with different hydrators, not working. BUT it works if by example i use the variable “id” instead of “customer_id” (in database, route, entity, …)
> Expressive\Router\Exception\InvalidArgumentException thrown with message “Route customer.get
expects at least parameter values for [customer_id], but received []”
Stacktrace:
#40 Zend\Expressive\Router\Exception\InvalidArgumentException in /home/michael/AppTest/api/expressive/vendor/zendframework/zend-expressive-fastroute/src/FastRouteRouter.php:305
#39 Zend\Expressive\Router\FastRouteRouter:generateUri in /home/michael/AppTest/api/expressive/vendor/zendframework/zend-expressive-helpers/src/UrlHelper.php:98
#38 Zend\Expressive\Helper\UrlHelper:__invoke in /home/michael/AppTest/api/expressive/vendor/zendframework/zend-expressive-helpers/src/UrlHelper.php:128
#37 Zend\Expressive\Helper\UrlHelper:generate in /home/michael/AppTest/api/expressive/vendor/zendframework/zend-expressive-hal/src/LinkGenerator/ExpressiveUrlGenerator.php:38
#36 Zend\Expressive\Hal\LinkGenerator\ExpressiveUrlGenerator:generate in /home/michael/AppTest/api/expressive/vendor/zendframework/zend-expressive-hal/src/LinkGenerator.php:36
#35 Zend\Expressive\Hal\LinkGenerator:fromRoute in /home/michael/AppTest/api/expressive/vendor/zendframework/zend-expressive-hal/src/ResourceGenerator/RouteBasedResourceStrategy.php:54
#34 Zend\Expressive\Hal\ResourceGenerator\RouteBasedResourceStrategy:createResource in /home/michael/AppTest/api/expressive/vendor/zendframework/zend-expressive-hal/src/ResourceGenerator.php:142
#33 Zend\Expressive\Hal\ResourceGenerator:fromObject in /home/michael/AppTest/api/expressive/vendor/zendframework/zend-expressive-hal/src/ResourceGenerator/ExtractCollectionTrait.php:118
#32 Zend\Expressive\Hal\ResourceGenerator\RouteBasedCollectionStrategy:extractPaginator in /home/michael/AppTest/api/expressive/vendor/zendframework/zend-expressive-hal/src/ResourceGenerator/ExtractCollectionTrait.php:52
#31 Zend\Expressive\Hal\ResourceGenerator\RouteBasedCollectionStrategy:extractCollection in /home/michael/AppTest/api/expressive/vendor/zendframework/zend-expressive-hal/src/ResourceGenerator/RouteBasedCollectionStrategy.php:39
#30 Zend\Expressive\Hal\ResourceGenerator\RouteBasedCollectionStrategy:createResource in /home/michael/AppTest/api/expressive/vendor/zendframework/zend-expressive-hal/src/ResourceGenerator.php:142
#29 Zend\Expressive\Hal\ResourceGenerator:fromObject in /home/michael/AppTest/api/expressive/src/App/Action/AllCustomerAction.php:33
#28 App\Action\AllCustomerAction:process in /home/michael/AppTest/api/expressive/vendor/zendframework/zend-expressive/src/Middleware/LazyLoadingMiddleware.php:60
…
Thanks in advance!