How can generate Accessors fron entity in Doctrine2 using generate:entity - php

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.

Related

How to access a the database data for Lexik Translation Bundle?

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 CLI command for adding properties to an existing Doctrine entity?

Just out of sheer laziness, I'm interested in knowing whether there's a command that will allow me to add a single field to an existing Doctrine entity, using prompts similar to the ones that appear when I run doctrine:generate:entity. Does such a command exist?
(For now, I'll go ahead and manually add my property, getters and setters in the entity's PHP file. I'm just curious about whether there's a CLI way to do it.)
Yes, there is. If you are using Syfmony 3.x or Symfony 4 you can install the symfony maker bundle (composer require symfony/maker-bundle).
Then, you can run the command php bin/console make:entity and select an existing entity name. It will detect that is created and allow you to add fields dynamically.
UPDATE: And it's not laziness. Code generation tools save time in repetitive tasks and allow you to focus more on your business logic. :)
No there isn't a command to add a new property to an existing entity because is no sense to do it I think.
For example if you want to add a new property you can add simply into the entity file and after you can launch the command to generate getter and setter.
Add a new property to an existing entity is very easy but it's not possible with a command.
Another way could be to use migration but you don't do it all by the CLI.
So the answer is:
if you want to add a new property add it into the entity file and after generate getter and setter

Symfony2 doctrine update schema from specific entity

If I run
php app/console doctrine:schema:update --force
It will update my database from all entities.
I need to update database only for the User entity, what is the solution?
One solution is to define a custom entity manager and then pass that entity manager to
php app/console doctrine:schema:update --force --em="custom"
But maybe it exists something faster without defining a custom entity manager?
According to the command documentation, there is no such option. If this is something you have to do only once, I'd suggest to use the --dump-sql option to get the required SQL instructions, and manually run the ones you need.
P.S. I fail to understand what's the reason to only update the schema for an entity, and leave all the rest of entities not sync'd with the database. That sounds like a recipe for getting db errors.
You can manually use the Doctrine SchemaTool class to generate an SQL diff based only on a single entity. You’ll need access to Doctrine’s EntityManager – the example below assumes access to Symfony’s container.
use Doctrine\ORM\Tools\SchemaTool;
$em = $this->container->get('doctrine')->getManager();
$schemaTool = new SchemaTool($em);
$metadata = $em->getClassMetadata(YourEntity::class);
$sqlDiff = $schemaTool->getUpdateSchemaSql([$metadata], true);
The variable $sqlDiff will now contain an array of SQL statements that are needed to bring the database schema up to date with your entity mapping.
The second argument passed to SchemaTool::getUpdateSchemaSql() is important – without it, the default behaviour would be to generate DROP TABLE statements for every other table that appears in your database.

Doctrine - Remap and convert a already existing entity

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?

Symfony, reading entity fields from controller

I'm trying to make some kind of "reusable" base controller in Symfony 2. Given the name of an entity, is it possible to read the schema of that entity? That is, a list of the entity's fields with the name and the type of each one.
This is in order to automatically generate the inputs in an admin panel and other similar operations.
SensioGeneratorBundle provides CRUD generation capabilities.
Its source code is available on GitHub.
Its documentation is well written; you should probably read it before making your decision.
This bundle can generate a CRUD controller based on a Doctrine entity. Once your application is set up, run the following command:
php app/console generate:doctrine:crud
It will generate forms, controllers and views.
Generated classes are easy to extend.

Categories