我正在尝试将我的平面 php 项目迁移到 Symfony2,但它变得非常困难。例如,我有一个 Products 规范表,它有几个规范,并且可以通过 Extraspecs DB 表中的“cat”属性来区分。因此,我为该表创建了一个实体,并希望创建一个仅包含“cat”= 0 的规范的数组......
我想代码是这个..对吗?
$typeavailable = $this->getDoctrine()
->getRepository('LabsCatalogBundle:ProductExtraspecsSpecs')
->findBy(array('cat' => '0'));
现在我怎样才能把它放在一个数组中以使用这样的表单?:
form = $this ->createFormBuilder($product)
->add('specs', 'choice', array('choices' => $typeavailableArray), 'multiple' => true)
先感谢您 :)
#谢谢你们..
但现在我遇到了另一个问题。事实上,我正在从现有对象构建一个表单:
$form = $this ->createFormBuilder($product)
->add('name', 'text')
->add('genspec', 'choice', array('choices' => array('0' => 'None', '1' => 'General', '2' => 'Specific')))
->add('isReg', 'choice', array('choices' => array('0' => 'Material', '1' => 'Reagent', '2' => 'Antibody', '3' => 'Growth Factors', '4' => 'Rodents', '5' => 'Lagomorphs')))
所以..在那种情况下,我的当前值被命名为“extraspecs”,所以我添加了这样的:
->add('extraspecs', 'entity', array(
'label' => 'desc',
'empty_value' => ' --- ',
'class' => 'LabsCatalogBundle:ProductExtraspecsSpecs',
'property' => 'specsid',
'query_builder' => function(EntityRepository $er) {
return $er ->createQueryBuilder('e');
但是“额外规格”来自 oneToMany 的关系,其中每个产品都有几个额外规格......
这是ORM:
Labs\CatalogBundle\Entity\Product:
type: entity
table: orders__regmat
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
name:
type: string
length: 100
catnumber:
type: string
scale: 100
brand:
type: integer
scale: 10
company:
type: integer
scale: 10
size:
type: decimal
scale: 10
units:
type: integer
scale: 10
price:
type: decimal
scale: 10
reqcert:
type: integer
scale: 1
isReg:
type: integer
scale: 1
genspec:
type: integer
scale: 1
oneToMany:
extraspecs:
targetEntity: ProductExtraspecs
mappedBy: product
Labs\CatalogBundle\Entity\ProductExtraspecs:
type: entity
table: orders__regmat__extraspecs
fields:
extraspecid:
id: true
type: integer
unsigned: false
nullable: false
generator:
strategy: IDENTITY
regmatid:
type: integer
scale: 11
spec:
type: integer
scale: 11
attrib:
type: string
length: 20
value:
type: string
length: 200
lifecycleCallbacks: { }
manyToOne:
product:
targetEntity: Product
inversedBy: extraspecs
joinColumn:
name: regmatid
referencedColumnName: id
我该怎么做?
谢谢!!!