mnavarrocarter / doctrine-carbon-bundle
Automatically fetch your Doctrine DateTimes as Carbon instances in Symfony.
Installs: 44
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 2
Open Issues: 2
Type:symfony-bundle
Requires
- php: >=7.1.0
- doctrine/doctrine-bundle: ^1.6
- doctrine/orm: ^2.5
- nesbot/carbon: ^1.22
- symfony/framework-bundle: ~2.1|~3.0
README
Automatically fetch your Doctrine DateTimes as Carbon instances in Symfony.
Installation
To install, simply run at your project root:
composer require mnavarrocarter/doctrine-carbon-bundle
While composer is working, thank @Jordi and enable the bundle in your AppKernel:
// app/AppKernel.php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new MNC\DoctrineCarbonBundle\MNCDoctrineCarbonBundle(),
);
// ...
}
// ...
}
How it works
MNCDoctrineCarbonBundle leverages the popular Carbon library as a service in order to convert your DateTime instances to Carbon ones.
By default, it listens for createdAt
, updatedAt
and deletedAt
properties
in your Entities (and their getters and setters) and transforms them into Carbon
instances everytime you fetch them from the database, so you can do cool things like:
$entity->getCreatedAt()->diffForHumans() // Ex, Outputs '21 months ago'.
Since MNCDoctrineCarbonBundle listens for those fields by default, it's a perfect companion if you have libraries like StofDoctrineExtensionsBundle in your project.
Specifying other properties names
In your config.yml file you can specify the properties to listen to:
# app/config/config.yml
# ...
mnc_doctrine_carbon:
properties: ['createdAt', 'editedAt', 'publishedAt'] # Array of property names
Note that you need camelCase getters and setters for the properties you desire to convert in order for MNCDoctrineCarbonBundle to do it correctly. Note also that when a property doesn't exist in a class, it's ignored completely and errors are not triggered.
Fine Graining
As from 1.1.0 you can use the special @Carbon
annotation to convert a single DateTime property into a Carbon instance.
<?php
class Entity {
use MNC\DoctrineCarbonBundle\Annotations;
/**
* @ORM\Column(type="datetime")
* @Carbon()
*/
private $publishedAt;
}
What is Carbon?
Carbon is a library that serves as a wrapper to php's DateTime built-in class, providing an easy-to-use and richly-featured API to make common operations with date and time a trivial and delightful task.
Go thank Brian Nesbitt for his awesome work!