GithubHelp home page GithubHelp logo

a5sys / doctrinetraitbundle Goto Github PK

View Code? Open in Web Editor NEW
6.0 4.0 2.0 23 KB

Generate the entities stub methods in a trait

License: MIT License

PHP 100.00%
symfony-bundle doctrine getters setters

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

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.