0

So I have three entities...

Contract (id, name, services)
Service (id, name, recommendedPrice)
ContractService (client_id, service_id, adjustedPrice)

I would like to have a form for creating/editing a 'Contract', that displays a checkbox for each possible 'Service', and a text field for the adjusted price of that service.

I have spent days trying to figure out the best way to structure all of this and am having a really hard time. I would assume it is similar in design to a form you would use to enable/disable permissions for a user, but I cannot find any good examples.

Right now I have my 'Contract' entity, which has an oneToMany association to its 'ContractServices', as a Contract can have many ContractServices. The 'ContractServices' entity has a manyToOne association to 'Services', is manyToOne correct here?

When I try to use the ContractServiceType form to collect services, I get no data on the form unless I have assigned one or more dummy 'ContractService' entities to the 'Contract' entity prior to rendering the form (similar to the task/tags embedded form tutorial on the Symfony site).

Also, once the data is persisted, the 'ContractServiceType' forms start duplicating, once for the dummy and again for any that have been persisted to the database for that 'Contract'. This is the closest I've been so far, so I decided I could write logic in the controller to decide which dummy entities to create later, based on which have already been associated with the 'Contract', though it seems like there should be a better solution.

So my solution right now is to use the 'Service' entity repo, to query for all unique 'Services', and then use those services to generate a number of dummy 'ContractService' entities to use when the form is built. It yields what I am looking for, in that I get a checkbox(to "enable") and adjustedPrice field for each of the possible 'services'.

Is this the best way to handle this?

If so, is there a way to reference the associated 'Service' name (should be available via the joined table?) in the ContractService form builder?

Donald P
  • 823
  • 6
  • 22
  • was unable to find a better design strategy, though it probably exists. the answer to the second question appears to be through use of the PRE_SET_DATA event listener, as described here (http://stackoverflow.com/questions/9723713/access-entity-data-inside-formtype-for-a-child-in-a-collection-in-symfony2) – Donald P Sep 24 '13 at 15:28

1 Answers1

0

Couldn't you do a ManytoMany?

This is how ZfcRbac does it in ZF2/Doctrine2

RbacRole Entity

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="Admin\Entity\RbacPermission", inversedBy="role")
 * @ORM\JoinTable(name="rbac_role_permission",
 *   joinColumns={
 *     @ORM\JoinColumn(name="role_id", referencedColumnName="role_id")
 *   },
 *   inverseJoinColumns={
 *     @ORM\JoinColumn(name="perm_id", referencedColumnName="perm_id")
 *   }
 * )
 */
 protected $perm;
 /**
 * Constructor
 */
public function __construct()
{
    $this->perm = new \Doctrine\Common\Collections\ArrayCollection();
}

RbacPermission Entity

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="Admin\Entity\RbacRole", mappedBy="perm")
 */
protected $role;
/**
 * Constructor
 */
public function __construct()
{
    $this->role = new \Doctrine\Common\Collections\ArrayCollection();
}
jas
  • 58
  • 8
  • I believe to have the additional data, such as adjusted price, I need to use the ContractService entity in the middle (discussed here: http://stackoverflow.com/questions/3542243/doctrine2-best-way-to-handle-many-to-many-with-extra-columns-in-reference-table ). I will dig deeper into the manyToMany association though and see if maybe that is my answer. – Donald P Sep 23 '13 at 21:58