2017-01-03 54 views
1

所以可以說我有Travel實體級:Symfony 2 - 如何允許作爲另一個實體的對象的實體變量爲空?

<?php 

namespace Bundle\TravelCostsBundle\Entity; 

class Travel { 

    ... 

    /** 
    * 
    * @var \Bundle\TravelCostsBundle\Entity\MilageAllowance 
    */ 
    private $milageAllowance; 

    ... 

    /** 
    * Set milageAllowance 
    * 
    * @param \Bundle\TravelCostsBundle\Entity\MilageAllowance $milageAllowance   
    * 
    * @return Travel 
    */ 
    public function setMilageAllowance(\Bundle\TravelCostsBundle\Entity\MilageAllowance $milageAllowance = null) { 
     $this->milageAllowance = $milageAllowance; 

     return $this; 
    } 

    /** 
    * Get milageAllowance 
    * 
    * @return \Bundle\TravelCostsBundle\Entity\MilageAllowance 
    */ 
    public function getMilageAllowance() { 
     return $this->milageAllowance; 
    } 

    ... 
} 

這裏是教義.yml文件:

# src/TravelCostsBundle/Resources/config/doctrine/Travel.orm.yml 
Pec\Bundle\TravelCostsBundle\Entity\Travel: 
    type: entity 
    repositoryClass: Pec\Bundle\TravelCostsBundle\Repository\TravelRepository 
    id: 
    id: 
     type: integer 
     generator: {strategy: AUTO} 
    ... 
    oneToOne: 
    milageAllowance: 
     targetEntity: MilageAllowance 
     mappedBy: travel 
    fields: 
    ... 

的應用與使用Doctrine數據庫連接。我使用validation.yml-文件驗證實體,並使用表單創建類型爲Travel的實體,其中包含milageAllowance-Entity變量的字段。

我想它是可選滿山遍野所以......

  • 如果字段爲空$milageAllowance住宿NULL和(更重要的)將被創建
      NO在數據庫條目
    • 如果填充字段,則會創建數據庫中的條目。

是,即使可能有oneToOne-關係?

如何驗證實體Travel可以只有1個或沒有milageAllowance

我很感謝您的幫助...請問是否有什麼不清楚!

+0

如何你想完成這個?在同一表格中添加多個millageAllowance,並在提交時將此millageAllowance附加到旅行實體? –

回答

0

感謝Stas Parshin這給了我答案的一部分。

Travel.orm.yml

Projekt\Bundle\MyBundle\Entity\Travel: 
    oneToOne: 
    milageAllowance: 
     targetEntity: MilageAllowance 
     mappedBy: travel 
     JoinColumn: 
     nullable: true 

裏面的TravelType類設置的增加ReceiptType形式'required' => false

class TravelType extends AbstractType { 

    public function buildForm(FormBuilderInterface $builder, array $options) { 
     $builder 
      ->add('milageAllowance', MilageAllowanceType::class, array(
       'required'  => false, 
      //Are all fields of milageAllowance empty => $travel->milageAllowance == NULL 
       ... 
     )) 

就像是如果所有字段是ReceiptType註釋,以便在orm文件中設置nullable: true留下空白屬性$travel->milageAllowance == NULL

1

你可以讓你的OneToOne關係可選設置JoinColumn(可爲空=真)

使用YML符號就應該是這樣的:

... 
    oneToOne: 
    milageAllowance: 
     targetEntity: MilageAllowance 
     mappedBy: travel 
    JoinColumn 
    nullable: true 
    fields: 
    ... 

更多信息,請參見本頁面http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html

+0

好的,謝謝,現在我有''milageAllowance'字段和'validation.yml'中的'milageAllowance'驗證規則。如果我提交基本表單,那麼驗證就會出錯,或者我必須明確告訴Symfony這個字段可以保持空白嗎? – goulashsoup

+0

如果你在這個字段上有NotBlank或NotNull這樣的斷言 - 我想你應該刪除它們才能使它工作,如果沒有的話 - 一切都會好的 –

+0

對不起,我遲到了。我閱讀了文章[如何嵌入表單集合](https://symfony.com/doc/2.8/form/form_collections.html),其中也沒有包含Entity對任意數量Entity對象的驗證。但是我認爲這或多或少是凌亂的,因爲Symfony中的簡單驗證可能性在那裏使用它們。有沒有一種方法可以添加驗證約束,這些約束在我希望Entitity對象獲取值時使用?稍後我可能會在我的問題中添加一些代碼,以更確切地顯示我的意思...... – goulashsoup

相關問題