简体   繁体   中英

Symfony2/Doctrine - form using two entities and a jointable/joinentity with metadata

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?

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM