a5sys/doctrine-trait-bundle

Installs: 5 667

Dependents: 0

Suggesters: 0

Security: 0

Stars: 6

Watchers: 4

Forks: 2

Open Issues: 2

Type:symfony-bundle

V2.0.0 2018-07-23 12:41 UTC

This package is auto-updated.

Last update: 2024-10-27 18:14:54 UTC


README

Generate the entities stub methods in a trait

The generated traits contains the getters/setters generated by the doctrine generator. The trait should be used in your entity.

Benefits

Your entities contains only the usefull information:

  • The doctrine columns and relationships
  • The getters/setters that have been modified
  • The custom methods

All basics getters/setters are in the trait.

Composer

Use composer to get the bundle

composer require --dev "a5sys/doctrine-trait-bundle"

Activate the bundle

In the AppKernel, activate the bundle for the dev environment

    if (in_array($this->getEnvironment(), array('dev'))) {
        ...
        $bundles[] = new A5sys\DoctrineTraitBundle\DoctrineTraitBundle();
    }

Usage

Generate traits

Run the command

    php app/console generate:doctrine:traits AppBundle\\Entity

Or this one if you want to act on a single entity

    php app/console generate:doctrine:traits "AppBundle:MyEntity"

Those commands will generate the trait(s) in the /src/AppBundle/Entity/Traits directory

Use custom method

  • Go in the trait related to your entity
  • Cut the method
  • Go in your entity
  • Paste the method
  • Update the method
  • The command will not re-generate this method because it is already defined in entity

Exemple

  • Create your entity (User) without the getters/setters
  • Run the command
  • Add the trait to your entity
  • You are done

The entity

    namespace AppBundle/Entity;

    class User
    {
        use AppBundle/Entity/Traits/UserTrait;

        /**
         * @ORM\Id
         * @ORM\GeneratedValue
         * @ORM\Column(type="integer", nullable=false)
         */
        protected $id;

        /**
         * @ORM\Column(type="string", length=50, nullable=false)
         */
        protected $name;
    }

The trait

    namespace AppBundle/Entity/Traits;

    /**
     * User
     */
    trait UserTrait
    {

        /**
         *
         * @param integer $id
         */
        public function setId($id)
        {
            $this->id = $id;
        }

        /**
         *
         * @return integer
         */
        public function getId()
        {
            return $this->id;
        }

        /**
         *
         * @param string $name
         */
        public function setName($name)
        {
            $this->name = $name;
        }

        /**
         * Get the name
         *
         * @return string
         */
        public function getName()
        {
            return $this->name;
        }
    }