I'm learning the Doctrine ORM and I'm loving, but I have a question about how remap a already mapped entity.
I created some of attributes in my table, and I want to map them to my entity. The problem is that I have some of business rule methods on my entity and I can' t lose them, so I need to remap the class and convert to entity without lose the already existed methods.
Actually I use the commands below:
php doctrine orm:convert-mapping --from-database --force annotation /path/project/models
php doctrine orm:generate:entities --update-entities --generate-methods=1 /path/project/models
So, you can help me? How?
Related
I am using the Lexik Trnaslation Bundle.
After I ran
./bin/console doctrine:schema:update --force
It added some tables to my database. These tables however don't have any Entities or Repository Classes.
How can I fetch the Data from these tables?
Is this even possible with doctrine?
There are entity classes and repository classes mapped to these new tables, or course. Otherwise doctrine:schema:update wouldn't create any tables at all.
If you take a look at the source code of the plugin, you'll see the corresponding classes here.
The repositories are:
FileRepository
TransUnitRepository
TranslationRepository
These repositories are not declared as services, so you won't be able to inject them directly. But you can inject the ManagerRegistryInterface and get the repository like this:
// example to get the FileRepository, assuming that $this->manager
// holds the EntityManager
$fileRepository = $this
->manager
->getRepository(Lexik\Bundle\TranslationBundle\Entity\File::class);
Is there a way I can extend a symfony entity in another bundle using #DiscriminatorMap without having to specify it beforehand?
BundleA
Has a class AbstractQueueItem which is a MappedSuperclassfor
Event which is extended by
CreateEvent and DeleteEvent as Single Table Inheritene or Class Table Inheritence
BundleB
How can I add a new Event (i.e. UpdateEvent) to the Event-hierarchy without modifing BundleA?
You can try letting doctrine auto-generate the discriminator map.
From the last bullet point in this section of the docs:
If no discriminator map is provided, then the map is generated
automatically. The automatically generated discriminator map contains
the lowercase short name of each class as key.
So you would:
Omit the #DiscriminatorMap declaration in BundleA.
Extend the entity as normal in BundleB (making sure the short name of each class is unique).
Update the database schema.
EDIT
As pointed out by ju_ in the comments, this solution will apparently not work with Doctrine ORM 3.0, but should still be valid for versions 2.5 - 2.7
I suppose the answer is no but I am asking to be sure.
SensioGeneratorBundle contains a command to generate entities. Do you know if it can generate the mappings for a one2Many or Many2Many field? Is there any project that implements this?
The only example I found:
php app/console doctrine:generate:entity --entity=AcmeBlogBundle:Blog/Post --format=annotation --fields="title:string(255) body:text" --with-repository --no-interaction
Many thanks
I know this is old... but this could help some people.
Here is a Symfony3 bundle that can generate Doctrine 2 associations, including a one-to-many relationship:
https://github.com/Remg/GeneratorBundle
Associations
Handles all Doctrine2 association types (OneToOne, OneToMany, ManyToOne, ManyToMany).
Handles unidirectional and bidirectional associations.
AFAIK the answer is no. I have already asked a similar question and it seems that there is no free tool that can do that. But if you want to pay : http://www.orm-designer.com/
There is a workaround that makes the job perfectly, without any additional software or bundle to install.
You just have to edit the DatabaseDriver.php from doctrine like described here :
Symfony2 Doctrine2 - generate One-To-Many annotation from existing database by doctrine:mapping:import
I am using this to generate getters and setters for my all classes in Bundle.
php app/console doctrine:generate:entities Acme/UserBundle
This is working fine.
When i use this , to update single entity then i get error
php app/console doctrine:generate:entity AcmeUserBundle:User
Then i get error
Entity already exists.. But in the multiple entities method , my entities were still there but it updates them
I think there is issue with eingle entity generator
If you read the docs, you'll realize that this behavior is exactly as intended. That command creates a new entity from a code-generation template in order to save you a couple of lines of boilerplate code.
Propel can generate classes based on a schema file. Some of the resulting classes are:
The object (e.g. User)
The Peer (e.g. UserPeer)
The Query (e.g. UserQuery)
The object class (User) includes getters and setters for all of the attributes. E.g.
$user = new User();
echo $user->getEmailAddress();
My question is: can Doctrine 2.0 do this? Does it generate base classes and does it add getters and setters?
Yes Doctrine 2 does support schema to class generation, I prefer YAML over XML so here's the link covering that http://www.doctrine-project.org/docs/orm/2.0/en/reference/yaml-mapping.html
AND then via the Doctrine command line tools, you can take the provided YML files and generate http://www.doctrine-project.org/docs/orm/2.0/en/reference/tools.html
As for your second question, for the most part Doctrine does have simple setters/getters but they're called accessor methods in Doctrine terminology.
Update:
For completely generated classes, give a table like
user:
id: integer
name: string
active: bool
it would be $user->getName() and $user->setName("Joe"), $user->setActive(true) and $user->getActive();
How it generates these intermediate classes can be somewhat understood by checking out this file in the Doctrine 2 git repo https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Tools/EntityGenerator.php