symfony - What is the best practice for accessing the value of an associated entity in Symfony2? -


using doctrine's entity framework, let's assume have entity called $user has onetoone association $address entity. city stored in address entity, not in user entity.

i understand can access value of associated entity this:

$user_city = &$user->getaddress()->getcity(); 

reference: http://symfony.com/doc/current/book/doctrine.html#fetching-related-objects

if need access user's city multiple times in code, best assign value variable (like above or without ampersand reference)? or best call entity value via entire chain $user->getaddress()->getcity() every time?

is $user->getaddress() stored in $user entity after first time it's called, , it's properties accessible on without overhead?

what standard practice , efficient on long run?

if $address separate entity, means have own lifecycle , whether save in separate variable or not, doctrine handle operations on properly. doctrine using design pattern called identity map (more here: http://docs.doctrine-project.org/en/2.0.x/reference/working-with-objects.html ) guarantees until explicitly clone it, return same object. link:

in case article accessed entity manager twice, modified in between. doctrine 2 realizes , ever give access 1 instance of article id 1234, no matter how retrieve entitymanager , no matter kind of query method using (find, repository finder or dql). called “identity map” pattern, means doctrine keeps map of each entity , ids have been retrieved per php request , keeps returning same instances.

as "best practice", laws of software design apply. if thing need address, pass address object. if you're operating on whole user, pass user object. follow dependency injection , law of demeter principles , you'll good.

also & operator not necessary here objects in php passed reference.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -