https://github.com/mpalourdio/mpacustomdoctrinehydrator
Module that helps you deal with dates for DoctrineModule & ZF2 : filtering, hydration, Locale etc.
https://github.com/mpalourdio/mpacustomdoctrinehydrator
date doctrine2 doctrineormmodule hydration zf2 zf2-module
Last synced: 5 months ago
JSON representation
Module that helps you deal with dates for DoctrineModule & ZF2 : filtering, hydration, Locale etc.
- Host: GitHub
- URL: https://github.com/mpalourdio/mpacustomdoctrinehydrator
- Owner: mpalourdio
- License: mit
- Created: 2013-10-02T19:13:11.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2018-12-29T09:08:56.000Z (over 7 years ago)
- Last Synced: 2025-02-01T01:12:23.860Z (over 1 year ago)
- Topics: date, doctrine2, doctrineormmodule, hydration, zf2, zf2-module
- Language: PHP
- Homepage:
- Size: 141 KB
- Stars: 4
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/mpalourdio/MpaCustomDoctrineHydrator)
[](https://scrutinizer-ci.com/g/mpalourdio/MpaCustomDoctrineHydrator/)
[](https://scrutinizer-ci.com/g/mpalourdio/MpaCustomDoctrineHydrator/)
[](https://insight.sensiolabs.com/projects/58b40eee-e087-4489-b169-71434b8c2879)
[![PHP 7.0+][ico-engine]][lang]
[![MIT Licensed][ico-license]][license]
[ico-engine]: http://img.shields.io/badge/php-7.0+-8892BF.svg
[lang]: http://php.net
[ico-license]: http://img.shields.io/packagist/l/adlawson/veval.svg
[license]: LICENSE
MpaCustomDoctrineHydrator
=========================
Module that helps you deal with date/datetime/time for DoctrineORMModule & ZF2 : filtering, hydration, Locale etc.
Extends and replace the ZF2 Date Element, ZF2 DateTime Element, ZF2 Time Element to make them compliant 'out-of-the-box' with doctrine hydration.
Provides an extension of the DoctrineORMModule ```AnnotationBuilder``` and a factory for more ease. The ```ElementAnnotationsListener``` is overridden too in order to better suit needs regarding filtering and validation.
The filters and the elements can be used as standalone. Using the provided elements via the ```FormElementManager``` adds automatic conversion formats for date/date and time/time strings to ```DateTime```.
Automatic filtering and validation are provided regarding the date format (Y-m-d, Y-m-d H:i:s, H:i:s, etc.) that depends of the ```Locale```. A placeholder is added to your form element too when rendered.
The hydrator service adds a strategy to every date column in your entity for extraction and hydration.
Requirements
============
PHP 7.0+ - Only Composer installation supported
Installation
============
Run the command below to install via Composer
```shell
composer require mpalourdio/mpa-custom-doctrine-hydrator
```
Add "MpaCustomDoctrineHydrator" to your **modules list** in **application.config.php**
Configuration
=============
Copy **mpacustomdoctrinehydrator.config.global.php.dist** in your **autoload folder** and rename it by removing the .dist
extension.
Add your own date / time formats (if needed) that are compliant with php ```DateTime```
see http://www.php.net/manual/fr/datetime.createfromformat.php
Usage (the easy and lazy way)
=============================
Create your forms with the provided annotation builder.
```php
$builder = new \MpaCustomDoctrineHydrator\Form\Annotation\AnnotationBuilder($this->entityManager, $this->formElementManager);
$form = $builder->createForm('Application\Entity\Myentity');
```
Or with the factory
```php
$form = $this->sm->get('annotationbuilder')->createForm('Application\Entity\Myentity');
```
Then, hydrate your form
```php
$hydrator = $this->sm->get('hydrator')->setEntity('Application\Entity\Myentity');
$form->setHydrator($hydrator);
```
You're done! Date/Date & Time/ Time colums will be hydrated/extracted, filtered and validated automatically, without providing anything else in your entities.
Your form elements will be rendered with a placeholder.
Usage (the hard and decoupled way)
==================================
```php
$hydrator = $this->sm->get('hydrator')->setEntity('Application\Entity\Myentity');
$form->setHydrator($hydrator);
```
In your forms classes, when not using the ```FormElementManager``` :
```php
$this->add(
[
'name' => 'mydate',
'type' => 'MpaCustomDoctrineHydrator\Form\Element\Date',
'attributes' => [
'id' => 'mydate',
],
'options' => [
'label' => 'My date',
'format' => 'd/m/Y' // format needed
],
]
);
```
If you pull your forms from the ```FEM```, just grab the element as a ```'Date'``` or ```'Zend\Form\Element\Date'```. The format option is not needed here, config will be pulled from service config.
```php
$this->add(
[
'name' => 'mydate',
'type' => 'Date',
'attributes' => [
'id' => 'mydate',
],
'options' => [
'label' => 'My date',
],
]
);
```
You can too use the filter as standalone on other form elements with custom formats, if needed. For this, use the filter FQCN.
If you use the filter shortname (```DateToDateTime ```), the config will be pulled from the service config (ie. The options array will be ignored).
```php
public function getInputFilterSpecification()
{
$filters = [
'otherdate' => [
'filters' => [
[
'name' => 'MpaCustomDoctrineHydrator\Filter\DateToDateTime',
'options' => [
'format' => 'd/m/Y' ('date_format' key is also accepted)
]
],
],
],
];
return $filters;
}
```
or simply
```php
public function getInputFilterSpecification()
{
$filters = [
'otherdate' => [
'filters' => [
[
'name' => 'DateToDateTime',
], // no options needed here, would be ignored anyway
],
],
];
return $filters;
}
```
/!\ If you don't create your fieldsets/forms via the ```FormElementManager```, you must manually inject the SL so the ```Date``` element can fetch the configuration
```php
$this->getFormFactory()->getFormElementManager()->setServiceLocator($this->sm);
```
/!\ Tip : To use the ```'DateToDateTime'``` filter short name in a form grabbed without the ```FEM```, you must do the following :
```php
$plugins = $this->sm ->get('FilterManager');
$chain = new FilterChain;
$chain->setPluginManager($plugins);
$myForm->getFormFactory()->getInputFilterFactory()->setDefaultFilterChain($chain);
```
You can use the provided strategy as standalone with your hydrators too. **Date Time and Time handling work the same way as the example above**, with only few changes, like the 'format' keys names.