Symfony doctrine:generate:entities How to find is and has methods - symfony

I have an entity called Member with a boolen property called "active". When I generate the getters and setters with doctrine:generate:entities I get the methods getActive() and setActive(). Now I rename the getter to isActive. When I now call doctrine:generate:entities on this entity again, the process generates a new method getActive as he does not find the method isActive.
Is there a way to tell it to search for is and has getter methods too?

No, this is not possible at the moment. You can vote for this feature here: http://www.doctrine-project.org/jira/browse/DDC-2287. However it is not recommended to use the "doctrine:generate:entities"-command to update entities. The command was built to generate the entities for the first time and not to update them. You should maintain your entities with your ide.

Related

Symfony2 getters, setters, add and remove

I'm getting really confused by entity properties and what they mean. I understand that get and set allow the application to interact with protected and private properties, but what about add and remove?
When running the command
php app/console doctrine:generate:entities bundle:entity
Sometimes it will generate getters and setters and other times it will generate add and remove properties (and usually a get method as well). I've noticed that it also sometimes depends on the relationship with other entities (i.e. OnetoMany), but not always :-S
Nowhere in the Symfony documentation that I can find is this addressed, and it's causing me to see the message "Neither the property "x" nor one of the methods exist and have public access" way too often. Can anybody provide a succinct explanation of this?
add and remove are used to deal with collections. If for example your Entity (Let's say A) contains a collection of B elements, then the command will provide an addB() and a removeB() public methods to help you add and remove elements from your collection. It'll also provide a getter which returns the whole collection.
The command generates methods based on the type of attributes you're working with (ArrayCollection, string, ...)
For xxxToMany associations, Doctrine will generate a "adder" and a "remover" instead of a plain setter. The idea is to easily add and/or remove a single object from the collection without needing to pass around the entire collection everytime to the setter.
Note though that these generated methods are an implementation detail you are free to revise. If you prefer a single setter method for example, feel free to implement that one yourself.
I personally don't rely on the accessor generation of Doctrine anymore. Doing it manually allows greater control of your entity's API, and is also quite easy in an IDE like Netbeans or PHPStorm.

Symfony PageEntity->getByPath() or PageController->getByPath()?

I would like to return the page Entity from the method: getByPath($path). I just would like to know where this method should be in the script. Inside the controller or inside the entity class?
In my opinion the entity "Page" shouldn't have a function called "getByPath()" since an entity should only contain database information of one entity, which can be get or set by getters and setters. And this "getByPath" function is not just a getter or setter it requires me to run the entitymanager within the entity. Am I right?
So am I right that I should make a PageController and create the "getByPath()" (which will return the page object) function there? Or would anyone create that function inside the entity class?
I would like to know what the nicest way is to accomplish this.
Thanks in advance.
You should put that function inside a custom repository for the Page entity
While the Entities are the objects you are storing, the Repository is the class that provides methods to access/load those objects, eg when you call $em->getRepository('Entities\Page')->find($page_id);, you call the find() method on your Page repository and it's its job to find it for you.
Doctrine provides a default repository for each entity (with the various find*() methods, ...), but you can provide a custom one where you can add your own method, such as getByPath().
Symfony 2 - Database and Doctrine - Custom Repository Classes
Doctrine 2 - Custom repository

How to access the Doctrine replace() method?

I am doing my first project with Symfony2 + Doctrine, and currently trying to implement replacing records. However when I try to call
$em->save($product);
or
$em->replace($product);
(instead of)
$em->persist($product);
I get fatal errors. So I started digging around to try to find the persist() method so I could see what other methods were available. I searched the entire contents of the vendors/doctrine directory and could not find any references to the persist or flush methods. Where the heck are these located? I tried following the code but quickly got lost.
So the main question: How can I do a replace() with doctrine in Symfony2?
Sub-question: Where are the persist() and flush() methods? Not being able to find them is frustrating in itself.
Incase somebody else is wondering, try looking into:
$em = $service->get('doctrine.orm.entity_manager');
$entity = $em->merge($entity);
$em->flush();
From the docs, Doctrine\ORM\EntityManager::merge():
Merges the state of a detached entity into the persistence context of
this EntityManager and returns the managed copy of the entity. The
entity passed to merge will not become associated/managed with this
EntityManager.
Should do the trick for you.
I don't think that Doctrine's replace() method is supported in Symfony, or at least it is not accessible through EntityManager.
If you need to update your existing entity, then you can simply do it as described here - http://symfony.com/doc/current/book/doctrine.html#updating-an-object.
As for persist() and flush() methods - you can find them in vendor/doctrine/common/lib/Doctrine/Common/Persistence/ObjectManager.php

Doctrine 2 base entities like those in doctrine 1?

I'm starting a new project with Symfony2/Doctrine2 and I generated my entities from the DB with the mapping:convert and mapping:import commands.
My entities are fine, but I only have the generated entities. Back in Doctrine1/Symfony1, you had your entities generated twice : once in the doctrine folder, an almost empty class just extending the second one in doctrine/base folder where all the doctrine internals were (getters/setters, fields...)
This way, you could add your own methods in the first file where they remained untouched even if you generated again the entities (only the doctrine/base files were modified by the generator).
Am I missing something ?
Well the generate command only generates getters and setters that are undefined and appends them to the (entity) file. This means that your custom getters/setters will never be overwritten. Does that answer your question?

Handling both EntityRepository and QueryBuilder for PagerBundle in Symfony2 and Doctrine

The PagerBundle is a very useful bundle for handling pagination in Symfony2. However, it provides
the adapter DoctrineOrmAdapter for Doctrine2 based on QueryBuilder.
Then, one should create a specific query with the QueryBuilder inside each controller. This approach will bring to the need of defining ad hoc functions that can be used inside the controllers, as well as possible problem when the table schema change. In fact, the correct approach is to use the user-defined EntityRepository, which contains all the useful queries for the Doctrine Entity.
Each call to the EntityRepository should return the result of a query, not a Query object or a QueryBuilder object.
So, how do you suggest to handle this problem?
PS: Please, don't answer that I can return a QueryBuilder by the functions of the entity repository. It is trivial!

Resources