Reference setting value in entity symfony

Reference setting value in entity symfony



I have a doubt about code organization using symfony3 and doctrine: I'll try to explain as clear as I can. Let's say I have a FootballClub entity:


class FootballClub

// other code
private $memberships;

public function addMembership(Membership $membership) : FootballClub

$this->memberships = $membership;
return $this;



public function removeMembership(Membership $membership) : bool

return $this->memberships->removeElement($membership);




The entity is in a many-to-one relationship with another entity, Membership, which represents the contract a player has with the club. Let's say each club
has only a limited number of membership it can acquire, number that is represented as a setting, for example, as a property in a Setting entity.
The question is: how should I reference that setting when removing a membership from the club and check that is respected? Entities should not have any dependency, so what would be the correct way to implement this? A service? can you provide an example? Thank you for your time.




1 Answer
1



You could create a Settings entity, linked in OneToOne relation with FootballCluc entity.


Settings


OneToOne


FootballCluc



Define Settings like this and instanciate it in the FootballClub's constructor


FootballClub



Settings entity


/** @Entity */
class Settings

/**
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(type="integer")
*/
private $id;

/**
* @ORMColumn(type="integer")
*/
private $maxMembership;

// Other configurable properties ...

__constructor($maxMembership = 50)

$this->maxMembership = $maxMembership;


public function getMaxMembership()

return $this->maxMembership;


public function setMaxMembership($maxMembership)

$this->maxMembership = $maxMembership;




Football Entity


class FootballClub


/**
* One FootballClub has One Settings.
* @OneToOne(targetEntity="Settings")
* @JoinColumn(name="settings_id", referencedColumnName="id")
*/
private $settings;

// other code
private $memberships;

__constructor(Settings $settings = null)

if (null === $settings)
$settings = new Settings();

$this->settings = $settings;


public function addMembership(Membership $membership) : FootballClub

if ($this->settings->getMaxMembership() <= count($this->memberships))
// throw new Exception("Max number of membership reached"); Strict mode
// return false // soft mode

$this->memberships-> = $membership;
return $this;



public function removeMembership(Membership $membership) : bool

return $this->memberships->removeElement($membership);







Thank you for your answer. In your example you set the Setting entity as an optional argument of the FootballClub constructor, but in the addMembership method of the same entity getMaxMembership() it's called unconditionally; what if $settings is null? Plus, from what I know, entities should not have any dependencies. Am I missing something?

– Egidiux
Sep 8 '18 at 8:04






Yes my bad I was tired last night, i fix this

– Mcsky
Sep 8 '18 at 12:56






What do you mean by dependencies ? Do you know a case when you can create a footballclub without any settings ? If yes you can add a setSettings to FootballClub entity and test if the settings is defined in your addMembership method. And set the settings property nullable

– Mcsky
Sep 8 '18 at 12:59



setSettings


nullable



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)