Php Symfony表单实体来自连接的实体
我想创建一个表单选择字段,如下所示:Php Symfony表单实体来自连接的实体,php,symfony-2.1,symfony-forms,Php,Symfony 2.1,Symfony Forms,我想创建一个表单选择字段,如下所示: <select> <option value="product.product_id">product_details.detail_name</option> etc... </select> 我希望属性是details.detail\u名称 我为此属性值尝试了不同的值。比如“details.detail\u name”、“pd.detail\u name”和“p.details.detail\u name
<select>
<option value="product.product_id">product_details.detail_name</option>
etc...
</select>
我希望属性是details.detail\u名称
我为此属性值尝试了不同的值。比如“details.detail\u name”、“pd.detail\u name”和“p.details.detail\u name”
但似乎不可能让属性显示详细名称
当我使用上述代码时,出现以下错误:
Neither property "detail_name" nor method "getDetailName()" nor method "isDetailName()" exists in class "Doctrine\ORM\PersistentCollection"
这个getDetailName()方法确实存在于ProductDetails实体中,我检查了这些实体,它们看起来都正常。而且,当我在表单之外使用这些实体时,它们工作得很好
我还尝试直接在数据库上执行结果查询,它给出了预期的结果。详细信息名称使用正确的语言
那么,有人能帮助我如何通过一个联合查询来创建我想要的选择列表吗?从我看到的方法
Product::getDetails
返回Doctrine\ORM\PersistentCollection
而不是'ProductDetails'实体(所以集合不是单个对象)。这意味着产品与使用一对多/多对多关联的细节相关
您可以尝试从产品详细信息方面执行此操作,然后:
$builder->add(
'product',
'entity',
array(
'class' => 'MyBundle:ProductDetails',
'property' => 'detail_name',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('pd')
->select('pd, p')
->join('pd.product', 'p')
->where('pd.language_id = :lang')
->setParameter('lang', 'en');
}
)
);
我终于设法让它工作起来了。下面,我将展示我是如何做到这一点的,以防其他人也有同样的问题 我现在使用的是自定义表单类型 在setDefaultOptions中,我调用了一个repository方法,它返回一个带有“product\u id”=>“detail\u name”的数组 在$this->repository->findAllProductsForForm方法中,我使用一个查询和一个foreach循环来使数组适合选择列表 然后,我必须在services.xml文件中注册存储库和此类型:
<service id="mybundle.repository.product"
factory-service="doctrine.orm.default_entity_manager"
factory-method="getRepository"
class="MyBundle\Repository\ProductRepository" >
<argument>MyBundle:Product</argument> <!-- not sure why I need this, but it wouldn't work without it -->
</service>
<service id="mybundle.xxx.form.product_choice_type" class="Mybundle\Form\Type\ProductChoiceType">
<argument type="service" id="mybundle.repository.product" />
<tag name="form.type" alias="product_choice" />
</service>
MyBundle:产品
然后,在根表单类型中(我想这就是所谓的)我使用“product_choice”作为表单类型
我不确定这是否是最好的方法,但至少它是有效的
现在,我只需要找出如何将用户的当前语言传递到存储库,但这是以后的问题。我尝试了这段代码,它成功了。但是,select的值现在是产品详细信息的id。它应该是产品id。
class ProductChoiceType extends AbstractType
{
private $repository;
public function __construct(EntityRepository $repository)
{
$this->repository = $repository;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'choices' => $this->repository->findAllProductsForForm('nl', true)
));
}
public function getParent()
{
return 'choice';
}
public function getName()
{
return 'product_choice';
}
}
<service id="mybundle.repository.product"
factory-service="doctrine.orm.default_entity_manager"
factory-method="getRepository"
class="MyBundle\Repository\ProductRepository" >
<argument>MyBundle:Product</argument> <!-- not sure why I need this, but it wouldn't work without it -->
</service>
<service id="mybundle.xxx.form.product_choice_type" class="Mybundle\Form\Type\ProductChoiceType">
<argument type="service" id="mybundle.repository.product" />
<tag name="form.type" alias="product_choice" />
</service>