Symfony 3.1从表单修饰符中的另一个实体获取Id

时间:2016-08-24 17:52:10

标签: symfony

我在表单构建器中有这个:

        $form->add('region', EntityType::class, array(
            'class'       => 'AppBundle:Regions',
            'placeholder' => '',
            'required' => false,
            'query_builder' => function (EntityRepository $er) use ($country_2a) {

                                    return $er->createQueryBuilder('s')
                                        ->where('s.countryId= :country_id')
                                        ->setParameter('country_id', $country_2a)
                                        ->addOrderBy('s.name');
            }
        ));

案例是在实体区域中我有country_id但我收到country_2a,然后在运行createBuilder之前我需要在实体的国家/地区搜索code_2a的id。

我已经在实体中声明了这种关系:

Countries Entity:

/**
 * @ORM\OneToMany(targetEntity="Regions", mappedBy="country_id")
 */
private $regions;


public function __construct() {
    $this->regions = new ArrayCollection();
}

Regions Entity:

/**
 * @var int
 *
 * @ORM\Column(name="country_id", type="integer")
 * @ORM\ManyToOne(targetEntity="Countries", inversedBy="id")
 * @ORM\JoinColumn(name="country_id", referencedColumnName="id")
 */
private $countryId;

我不知道如何设法在实体之间使用这种关系,我尝试过JOIN LEFT和subselect但没有任何效果。

也许我的问题是我需要访问国家/地区实体,但是$ er连接到Regions实体,或者如何创建createQueryBuilder语句来利用显示的关系。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

你的区域实体应该是这样的:

/**
 * @var int
 *
 * @ORM\Column(name="country_id", type="integer")
 * @ORM\ManyToOne(targetEntity="Countries", inversedBy="regions")
 * @ORM\JoinColumn(name="country_id", referencedColumnName="id")
 */
private $countryId;

尝试改变。

相关问题