PHP Composite Design Pattern with Symfony2 and Doctrine - symfony

Is there any example for Composite Pattern with Doctrine? I mean PHP source code using Doctrine annotations.
I could find Decorator Pattern here : http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/decorator-pattern.html but no luck about Composite Pattern.

You should be able to do this with Inheritance Mapping (see http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/inheritance-mapping.html) and just normal associations with the type you want (see http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/association-mapping.html)

Related

override mapping for a specific Entity class

I'm using a standard Symfony 2.8 framework with Doctrine.
My entities' mappings are all annotated, but I would need to map a single entity by using the PHP way (by defining loadMetadata static method) . I know there's a way to override a mapping Bundle configuration like explained here, but what I would like is specifying a single Entity. Is that possible? Thanks
No you can't mix the formats
A bundle can accept only one metadata definition format. For example, it's not possible to mix YAML metadata definitions with annotated PHP entity class definitions.
see doc here:
http://symfony.com/doc/current/doctrine.html
if you really need to change the mapping format then I suggest you create a new bundle for your specific entity. I also had this problem. I wished to have different mapping format (yml and annotation in my case) but I had to create a new bundle.

how to use only annotation mapping in doctrine 2

-Is there a way to make doctrine neglect or don't look in entityxx.orm.xml mapping files and look only in annotation mapping in the entity class?
-I'm getting error when try to modify an entity without modifying it's underline xml mapping too so i have to modify both!,actually my entities was imported from database but now it's converted to annotation and i don't want to use xml any more .
it's impossible to use 2 differents method mapping for entity in symfony. Xml or annotation but not both in the same time.
You must delete xml mapping file if you want to use a annotation mapping.

symfony2 annotation documentation

Where can I find documentation for a complete list of annotations possible for different Symfony2 components?
Example, http://symfony.com/doc/current/components/routing/introduction.html here there's not enough information about "options" of the config and documentation about routing via yml in whole.
The same questions for controller annotations. I've found separate references to different annotations possible in controller(http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html, http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/view.html), but no COMPLETE list of possible annotations.
There MUST be something I'm missing, because people somehow know what options to use and how.
You can find all details about annotation in SensioFrameworkExtraBundle documentation (#Route , #Method , #ParamConverter ,#Template ,#Cache and #Security )
If you want the list for your Entity you can read doctrine documentation .

SonataNewsBundle Entity Relationship Definition

I'm trying to create a new entity which will have relationship with SonataNewsBundle Post entity. But I can't find any example in the source how to define relation mapping in xml.
I've read all the configuration under Resources/config/doctrine, none of them had entity relation mapping. Or maybe Sonata use other method to define relationship? Give me some clues.
Ahhhh... Found it...
It's in the DependencyInjection.
Sorry for the lame question

Generic comment system in Symfony2

I have in my Symfony 2.1 RC app a simple Comment model (using Doctrine 2). Every comment has a user and a message.
Currently, the CommentBundle manages comments on articles. I'd like it to be more generic to be able to comment any kind of entity without copying code across different bundles dedicated to comments...
For this to work, I also need a way to reference any entity from the comment one. I think having two fields entity_type and entity_id can be a nice solution. However, I can't get the object from these without mapping entity_type to classes manually and using the find method.
So how do I reference an entity from a comment ? And how can I create generic behavior working on several entities ?
You can create a abstract base class entity called Commentable and create entities that inherit Commentable such as Document or Post.
Since Document and Post are derived from Commentable, you can create a one to many relationship between the entities Commentable and Comment respectively.
Make sure to include in your base class ORM annotations for inheritance:
#InheritanceType
#DiscriminatorColumn
#DiscriminatorMap
Examples can be found on Doctrine Project Inheritance Documentation

Resources