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

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);

Related

Symfony3, creating entity without mapping all table fields

I have a database table which has a lot of fields. I want to create an Entity which maps only to few of those fields.
Is there a practical way to achieve that?
When I try to make an entity and mapping it via annotations, the doctrine:schema:validate command says that the entity is not in sync, and is right.
When I try to make a doctrine:schema:update it automatically drops all the fields that the entity doesn't have. I want that the schema update command updates only the fields written in my entity class.
With Doctrine ORM you map your database table to a PHP class and a row from that table is mapped to an instance of the entity class.
So i am pretty sure that you have to map all your fields. Otherwise if you dont want to - user ActiveRecord, it is possible there.

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.

Is it possible to have Symfony and/or Doctrine hydrate associated objects managed by different entity managers?

I have a legacy application that was using Xaraya to manage user content that I am trying to replace with a rewrite using Symfony/Sonata to manage users and/or content.
For whatever reason, previous developers managed this with two different databases (MySQL for Xaraya, and SQL Server for other things, including authenticating users).
I am trying to create Entity mappings such that the users/groups from SonataUserBundle (which extends FOSUserBundle) use the entity manager associated with the login database connection, and this works for logging into the admin site itself, but blows up when it tries to hydrate objects that have associations to the User entity.
It appears that Doctrine does not try to find the entity manager associated with an entity when hydrating an object's associations.
My question is this: it it possible to make Doctrine hydrate objects using the entity manager for an entity instead of assuming it's mapped to the current entity manager, and if not, is there any form of a clean code work-around for it?
Thanks.
(Note: The method of using the "databasename.tablename" syntax in the query that I have seen mentioned elsewhere will not work for my use case.)

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

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.

Does Doctrine 2.0 pre-generate model classes like Propel 1.5 does?

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

Categories