Reputation: 506
Simple question i have a collection Users and Users_Preferences. Users Preferences are suppose to a be an extension of users. For example:
Users
--------------------------------
Id | Name
1 | Test
2 | Luis
3 | Lopes
Users_Preferences
--------------------------------
Id | User_ID | Color | TrackSystem | ReceiveNewsletter
1 | 1 | Green | after_seen | no
2 | 3 | Red | dont_track | yes
So as you can see i want to create a relation but there may not be records on users_preferences as i am not forcing people to pick their preferences neither set defaults.
ALSO i would like to access it from my Users Repository and Entity.
# src/CrazyBundle/Controllers/WhateverController.php
/** @var UsersPreferences $userPreferences **/
$userPreferences = $this->getUser()->getPreferences();
$userPreferences->getColor()
// OR
$this->getUser()->getPreference("color")
This should be a basic question for someone who works with Symfony :). The best Practise first please.
Thanks
Upvotes: 0
Views: 98
Reputation: 523
I assume, that User and UserProperty are entites.
For the User entity you can aet a preference attribute, with a OneToOne relation:
/**
* @ORM\OneToOne(targetEntity="UserPreferences")
* @ORM\JoinColumn(name="preference_id", referencedColumnName="id", nullable=true)
**/
protected $preference;
You generate a getter and a setter for it.
After this you can get the UserPreference entity from the User:
$user->getPreference()->getColor()
Or in TWIG:
{{ user.preference.color}}
Upvotes: 2