Symfony表示,猜测关联字段

时间:2016-09-30 16:38:40

标签: symfony symfony-forms

当我在Symfony表单中添加我的实体的字段时,实体注释被映射并基于此Symfony猜测我的字段是否可以为空,到目前为止一直很好,例如此转储:

public 'fieldMappings' => 
array (size=5)
  'acl' => 
    array (size=9)
      'fieldName' => string 'acl' (length=3)
      'type' => string 'smallint' (length=8)
      'scale' => int 0
      'length' => null
      'unique' => boolean false
      'nullable' => boolean true
      'precision' => int 0
      'options' => 
        array (size=2)
          ...
      'columnName' => string 'acl' (length=3)
  'id' => 
    array (size=10)
      'fieldName' => string 'id' (length=2)
      'type' => string 'integer' (length=7)
      'scale' => int 0
      'length' => null
      'unique' => boolean true
      'nullable' => boolean false
      'precision' => int 0
      'options' => 
        array (size=2)
          ...
      'columnName' => string 'id' (length=2)
      'id' => boolean true

但是如果我有关联字段(外键),Symfony有一个不同的映射结构,并且不会猜测该字段是否可以为空,例如这个转储:

public 'associationMappings' => 
array (size=3)
  'userObj' => 
    array (size=19)
      'fieldName' => string 'userObj' (length=7)
      'joinColumns' => 
        array (size=1)
          ...
      'cascade' => 
        array (size=0)
          ...
      'inversedBy' => null
      'targetEntity' => string 'AdminBundle\Entity\User' (length=23)
      'fetch' => int 2
      'type' => int 2
      'mappedBy' => null
      'isOwningSide' => boolean true
      'sourceEntity' => string 'AdminBundle\Entity\UserAcl' (length=26)
      'isCascadeRemove' => boolean false
      'isCascadePersist' => boolean false
      'isCascadeRefresh' => boolean false
      'isCascadeMerge' => boolean false
      'isCascadeDetach' => boolean false
      'sourceToTargetKeyColumns' => 
        array (size=1)
          ...
      'joinColumnFieldNames' => 
        array (size=1)
          ...
      'targetToSourceKeyColumns' => 
        array (size=1)
          ...
      'orphanRemoval' => boolean false
  'storeObj' => 
    array (size=19)
      'fieldName' => string 'storeObj' (length=8)
      'joinColumns' => 
        array (size=1)
          ...
      'cascade' => 
        array (size=0)
          ...
      'inversedBy' => null
      'targetEntity' => string 'AdminBundle\Entity\Store' (length=24)
      'fetch' => int 2
      'type' => int 2
      'mappedBy' => null
      'isOwningSide' => boolean true
      'sourceEntity' => string 'AdminBundle\Entity\UserAcl' (length=26)
      'isCascadeRemove' => boolean false
      'isCascadePersist' => boolean false
      'isCascadeRefresh' => boolean false
      'isCascadeMerge' => boolean false
      'isCascadeDetach' => boolean false
      'sourceToTargetKeyColumns' => 
        array (size=1)
          ...
      'joinColumnFieldNames' => 
        array (size=1)
          ...
      'targetToSourceKeyColumns' => 
        array (size=1)
          ...
      'orphanRemoval' => boolean false

因此,在这些情况下,需要手动配置必需属性,否则它将为true(Symfony表单的默认值)。

在这些情况下,任何人都知道Symfony如何猜测所需的属性? 我的实体有以下注释:

/**
 * @ORM\ManyToOne(targetEntity="User")
 * @ORM\JoinColumn(name="fk_user", referencedColumnName="id", nullable=false, unique=false, onDelete="CASCADE")
 */
private $userObj;

/**
 * @ORM\ManyToOne(targetEntity="Store")
 * @ORM\JoinColumn(name="fk_store", referencedColumnName="id", nullable=false, unique=false, onDelete="CASCADE")
 */
private $storeObj;

1 个答案:

答案 0 :(得分:0)

您可以创建TypeGuesser service。但是,这是一项很多工作,所以为什么不手动设置字段一次并完成它呢?