0

I have an Entity i.e Users. I want to make getters and setters of this entity in Doctrine, so that Doctrine can read it.

How can I do it, can someone provide me basic example? I am a beginner

How to insert data in this database table?

Here is my Users entity

<?php
 /**
 * @Entity
 * @Table(name="users")
 * Total Number of Columns : 32
 */
class Users{

/* Attributes of Users */

     /** 
     * @Id 
     * @Column(type="integer") 
     * @GeneratedValue
     * @dummy
     * @Assert\NotEmpty
     */
       private $id;

     /** 
     * @Column(type="string")
     * @Assert\NotEmpty
     */
       private $name;


     /** 
     * @Column(type="string")
     * @Assert\NotEmpty
     */
       private $email;

}

?>
Hassan Sardar
  • 4,413
  • 17
  • 56
  • 92

3 Answers3

7

Try with this command:

php app/console doctrine:generate:entities YourBundle:YourEntity
zizoujab
  • 7,603
  • 8
  • 41
  • 72
  • But first I have to define getters and setters . How to do that ? – Hassan Sardar Oct 21 '13 at 09:23
  • 1
    If you use this command, doctrine generates getters and setters automatically for your entity. –  Oct 21 '13 at 09:52
  • 2
    This would be the only correct answer, because no need to create the getters and setters yourself, this command does it for you! – acrobat Oct 21 '13 at 10:00
  • There are various instances where the generators don't work - especially if you're not storing your entities in the default place. So, while this is likely to work for the OP, it's actually not the 'correct' answer. Relying on generators is a bad idea until you know what's happening under the hood and how to do it yourself. – Matthew Davis Jan 16 '14 at 14:31
3

For example, if you wanted to have a setter for your email property, you would do:

public function setEmail($email)
{
    $this->email = $email;

    return $this;
}

public function getEmail()
{
    return $this->email;
}

The first is the setter (it sets the value of email on the object) and the second is the getter (it gets the value of email from the object). Hope that helps :)

Matthew Davis
  • 398
  • 1
  • 7
2

You can use magic methods if you're lazy enough not to define your own methods for each property.

    public function __get($property)
    {
        return $this->$property;
    }
    public function __set($property,$value)
    {
        $this->$property = $value;
    }

It's better to create a method for each property though

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

Have a look at the answers here Doctrine 2 Whats the Recommended Way to Access Properties?

Community
  • 1
  • 1
Mina
  • 1,508
  • 1
  • 10
  • 11