mnavarrocarter/doctrine-carbon-bundle

This package is abandoned and no longer maintained. No replacement package was suggested.

Automatically fetch your Doctrine DateTimes as Carbon instances in Symfony.

1.1.0 2018-01-15 12:58 UTC

This package is auto-updated.

Last update: 2019-08-18 18:32:29 UTC


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!