0
votes

I would like to know if it is possible to avoid using getters and setters for a Symfony 2 entity. Although the php app/console doctrine:generate:entities is very helpful, line codes seem to increase a lot for an entity with a lot of fields

2
Doctrine won't work with public properties properly since it can't make lazy-loading work on them. - meze

2 Answers

2
votes

You can define the visibility of your properties from protected or private to public so you now have access to them in this way:

// on entity
public $someProp;

// On your code
$someEntity->someProp = someValue;

This is not a good OOP practice and should be avoided, getters and setters are the way to go if you want clean and secure code.

1
votes

You Code won't work properly if skip getters and setters. Especially if you have mapping with different Entities.

Doctrine uses getters nad setters to populate various members fields during storing and fetching of information

Its very bad practice to use public for every property in Entity. and also you will end up wasting lot of time in changing the property to public as doctinr uses proted when it autogenerate the code