Is it possible to generate entities with automatic annotation - symfony

I would like to generate entities from DB with #ApiResource applied on each entity, is that possible ?

If you use the Maker Bundle, you can add an --api-resource flag for the make:entity command (See this merge request)

Related

Symfony form, EntytyType/ChoiceType from 2 different entities

I need to create a dropdown in a Symfony form that contains entities from 2 different tables, for example:
Where EntityA and EntityB are two different classes and MySQL tables, with a different structure.
Is it possible to achieve this?
You should use the choice_loader option of a ChoiceType field.
You can use a CallbackChoiceLoader to load your entities through a closure or implement your own choice loader as a service that you can inject to your form type.
This allows you to build a custom query (or execute two queries in this case) and return the choice list built from the results lazily.
Check the official documentation here (https://symfony.com/doc/current/reference/forms/types/choice.html#choice-loader).

doctrine:schema:update wants to create an already existing view in symfony 2.8

I have an existing view in my SQL database "view_account" which represents a account entity. This view is read only and has no primary_key field. Actually it has a primary key "id" but the field is not declared as primary key. I can also not change the table design, because its an automatic export from another application generating this tables (the automatic export is every week).
But thats not the problem cause doctrine don't care about "primary key" flag in the database until you don't update the schema.
But whenever i try to "doctrine:schema:update --force" doctrine wants to create this table. How can i ignore this view (or better the entity) from updating by doctrine? Marking the entity read_only with doctrine annotation is not working. Extending a own update-command will also not work, as i found out, it will not work since doctrine 2.4.7 to extend the doctrine-schema-update command (the update command method is ignored in any way).
Whats the best solution?
Edit:
I also tried to configure two entitymanager. One for the "internal" entities and for the "foreign" entities. Since all entities are linked to each other on some point, it is also not possible to have two separated manager don't knowing from each other entities.

Symfony clone an entity from one entity manager to another with FosUser

I want to copy data from one database to another. I get two Customers from two different entity managers (emLocal,emRemote).
The customer entity is in a oneToOne relationship with FosUser
The flush try to insert a blank user. even if I try to detach concerned entities and set every relation to null.
I read from github that FosUserBundle is not designed to works with two entity manager
$remoteCustomer = $order->getCustomer();
$onlineCustomer = $this->emLocal->getRepository("LilWorksStoreBundle:Customer")->findOneBy(array("remoteUser"=>$remoteCustomer->getUser()->getId()));
if(!$onlineCustomer){
$onlineCustomer = clone $remoteCustomer;
}
$this->emLocal->detach($onlineCustomer->getUser());
$onlineCustomer->getUser()->setCustomer(null);
$onlineCustomer->setUser(null);
$this->emLocal->persist($onlineCustomer);
$this->emLocal->flush();
How I can prevent this INSERT?
I believe you should use another Entity Manager to save $onlineCustomer.
$this->emOnline->persist($onlineCustomer);
$this->emOnline->flush();

Entity propetytype as object in Symfony2

I'm looking information, how use entity propety type as object.
I have entity Products and Categories. In Products Entity propety category is type object. How pass object Categories to that propety? Any ideas how that use? Any Example ?
I would say: don't use the object type. Instead use Doctrine's association mappping.
I would advice to use the one-to-many bidirectional or the many-to-many bidirectional if you want to be able to have one product in multiple categories too.
Follow these steps:
Place the examples in your own entity and change the annotations from #... to #ORM\...
generate the getters and setters with the commandline:
app/console doctrine:generate:entities AppBundle
update your database schema:
app/console doctrine:schema:update --force
Now check your created getter and setter functions. You might find something like getProducts() removeProduct() and addProduct().

How to log an entity that has collections?

I want to log all changes of an entity. I looked into Loggable doctrine extension as provided by the StofDoctrineExtensionsBundle.
I got it working for fields that store simple data, e.g. string and integers. But my entity also has ManyToMany relationship to another entity, e.g. Tags.
I am getting this error:
InvalidMappingException: Cannot versioned [tags] as it is collection in object - Hn\AssetDbBundle\Entity\Asset
Is there a way to log an entity with its relationships? I don't mind switching to another bundle.
Currently no bundles/extensions have this functionality out of the box. One option would be to implement it yourself. This can be done by making use of Doctrine Listeners. Particularly you need to listen to postUpdate and postPersist events - these happen when entity is updated and created and store your Tags there.
Another option is to get rid of ManyToMany relationship. For this create an intermediate entity AssetTag that would have OneToMany relationship to both Asset and Tag. After this is done, you can use EntityAudit Doctrine Extension, which supports this type of relationships.

Resources