Doctrine实体自引用关联及其他信息

时间:2015-01-27 11:18:13

标签: symfony doctrine-orm symfony-forms

我有一个实体客户端,该实体需要与另一个客户关联,并提供有关关系的信息。该实体是ClientRelationship。 我需要在一个属性中使用此asociation,因为我想使用带有集合类型字段的symfony表单。 问题是我不知道如何编写它以在一个Client属性中拥有该关联。

现在我在Client有两个asociation属性,但那是我的问题。在一边的形式是关系可见,但在其他客户端它不是。

客户实体:

/**
* @ORM\OneToMany(targetEntity="ClientRelationship", mappedBy="leftClient", cascade={"persist", "remove"})
*/
private $leftRelationships;

/**
 * @ORM\OneToMany(targetEntity="ClientRelationship", mappedBy="rightClient")
 */
private $rightRelationships;

ClientRelationship实体:

/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(type="string", length = 255)
 * @Assert\Length(max = 255)
 * @Assert\NotBlank()
 */
private $relationship;

/**
 * @ORM\ManyToOne(targetEntity="Client", inversedBy="leftRelationships")
 * @ORM\JoinColumn(name="leftClient_id", referencedColumnName="id")
 */
private $leftClient;

/**
 * @ORM\ManyToOne(targetEntity="Client", inversedBy="rightRelationships")
 * @ORM\JoinColumn(name="rightClient_id", referencedColumnName="id")
 */
private $rightClient;

编辑:添加表格

ClientType表单:

$builder->add('leftRelationships', 'bootstrap_collection', array(
            'type' => new ClientRelationshipType(),
            'label' => 'relationships',
            'allow_delete' => true,
            'allow_add' => true,
            'by_reference' => false,
            'add_button_text'    => 'add',
            'delete_button_text' => 'remove',
            'options' => array(
                'businessCase' => $options['businessCase'],
                'client' => $options['client'],
            ),
        ));

ClientRelationshipType表单:

$builder->add('rightClient', 'entity', array(
            'label' => 'client',
            'class' => 'MyProject\CoreBundle\Entity\Client',
            'property' => 'name',
            'query_builder' => function (ClientRepository $er) use ($options)   {
                    $qb = $er->createQueryBuilder('c');
                    if ($options['businessCase'] instanceof BusinessCase) {
                        $qb->andWhere($qb->expr()->in('c.id', ':clients'));
                        $qb->setParameter(':clients',   $options['businessCase']->getClients()->toArray());
                    }
                    if ($options['client'] instanceof Client) {
                        $qb->andWhere($qb->expr()->neq('c.id', ':client'));
                        $qb->setParameter(':client', $options['client']);
                    }
                    return $qb;
                },
        ));

    $builder->add('relationship', 'text', array(
            'label' => 'relationship',
        ));

0 个答案:

没有答案