Asked  2 Years ago    Answers:  5   Viewed   125 times

How to use entity as service in doctrine (Using Symfony 2.1).

Example usage:


namespace MyNamespace;

class MyEntity
  protected $container = NULL;
  public function __construct($container)
    $this->container = $container;

   * @ORMPrePersist
  public function() 
    // Must call to container and get any parameters
    // for defaults sets entity parameters

As a result, I need to get access to the entire container.



An entity is a data model and should only hold data (and not have any dependencies on services). If you want to modify your model in case of a certain event (PrePersist in your case) you should look into making a Doctrine listener for that. You can inject the container when defining the listener:

        class: AcmeSearchBundleListenerYourListener
        arguments: [@your_service_dependency_or_the_container_here]
            - { name: doctrine.event_listener, event: prePersist }
Tuesday, December 6, 2022

When you have entities outside of any bundle or that the location is not the usual one, you'll have to change the doctrine section in config.yml from

    # ...
        # ...
        auto_mapping: true


    # ...
        # ...
            model: # replace `model` with whatever you want
                type: annotation # could be xml, yml, ...
                dir: %kernel.root_dir%/../path/to/your/model/directory
                prefix: PrefixNamespace # replace with your actual namespace
                alias: Model # replace with the desired alias
                is_bundle: false

The dir parameter informs Doctrine where to look for the mapping definitions. If you're using annotations, it will be your model directory. Otherwise, it will be the directory of your xml/yml files.

Entities's names — to access from Doctrine repositories — begin with Model in this case, for example, Model:User. It corresponds to the parameter alias.

When editing a configuration file, do not forget to clear the cache.

Moreover, in my question, I wrote that I changed something in my Bundle class but it was not useful as the bundle won't be reused by another project. So I removed everything.

See this answer for more details : https://.com/a/10001019/2720307

Thanks to Elnur Abdurrakhimov!

Thursday, October 20, 2022

The parent entity needs to give visibility of it's properties to it's children.

Change your $id property visibility to "protected".

Wednesday, September 28, 2022

You can modify your entity where you are returning your value. In your function getM01Ch() do something like this

 * @return DateTime
public function getM01Ch()
    $returnValue = $this->m_01_ch->format('h:i:s') 
    return $returnValue

Other than that I don't know if any better approach exists. More info

Sunday, November 27, 2022

Don't complicate your life, you can just do


This WILL NOT perform any additional query or trigger lazy load!

Why? Because your $food->foodGroup is a proxy object which knows about it's ID. It will only do lazy load if you call a getter method of some field which hasn't been loaded.

Wednesday, August 17, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages