Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto generate get set methods for Doctrine

I am using Doctrine I have to make a lot of models, and it would be nice if I wouldnt have to do everything manually.

I set and attribute like this:

/**
     * @var string $name
     *
     * @Column(name="Name", type="string", length=100, nullable=false)
     */
    private $name;

The get & set method are made from information, which is entirely included in the attribute declaration. So does anyone know any tools that would generate the get set methods like below from the attribute declaration.

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

        return $this;
    }

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

In the doctrine documentation I found this tool(Entity Generation), but I have trouble understanding what I should do.

like image 862
Borut Flis Avatar asked Feb 05 '13 08:02

Borut Flis


People also ask

What are the methods of a get and set method?

A property is like a combination of a variable and a method, and it has two methods: a get and a set method: The Name property is associated with the name field. It is a good practice to use the same name for both the property and the private field, but with an uppercase first letter.

What is a get and set method in APTA?

A property is like a combination of a variable and a method, and it has two methods: a get and a set method: The Name property is associated with the name field. It is a good practice to use the same name for both the property and the private field, but with an uppercase first letter. The get method returns the value of the variable name.

Is there a Get/Set method for @Dave private?

@dave private properties can't be accessed outside of the class (as I'm sure you know). As such, there's not much use in having a get/set method for something you can access directly. public bool Monday { get; set; } // etc … (You don’t need your fields now, backing fields are generated by the compiler.)

Is it possible to generate Get/Set methods from persistent object?

Of course if you have a Persistent Object then the GET/SET methods are already generated for you. Actually in the 7.10 and higher code line (not be confused with 7.0 and its enhancement packages that run under the Business Suite), SAP has added GET/SET generation to the Refactoring capabilities within the class editor.


2 Answers

  1. Navigate to the root of your symfony2 project
  2. Type into your command-line shell: php app/console doctrine:generate:entities
  3. Enjoy your auto-generated getters and setters
like image 155
DonCallisto Avatar answered Sep 28 '22 02:09

DonCallisto


Since you are not mentioning Symfony, just run

vendor/bin/doctrine orm:generate:entities entities/

from the root directory of your project (replace entities/ with directory, where you stores your entity classes).

like image 27
jkulak Avatar answered Sep 28 '22 03:09

jkulak