Code Monkey home page Code Monkey logo

doctrinetraitbundle's Introduction

DoctrineTraitBundle

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;
        }
    }

doctrinetraitbundle's People

Contributors

arnaudgoulpeau avatar thomasbeaujean avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

doctrinetraitbundle's Issues

Saut de ligne "en trop"

Lorsqu'une classe ne possède pas de doctrineConstruct() il y a un saut de ligne entre l'accolade d'ouverture du trait et la première méthode. Cela ne fait pas planter phpcs à priori, mais ce serait bien de revoir ça si c'est pas trop compliqué à corriger.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.