簡體   English   中英

如何訪問表單中的相關實體?

[英]How to access related entities in a form?

我目前正在努力處理表單和保留的實體,因為我無法從表單中的相關實體訪問屬性。

form.get('value') // access current entity
form.get('value').relatedEntity  // (access the related entity)
form.get('value').relatedEntity.property // is not working

我想更詳細地說明我的整個情況,因為我認為當前的解決方案不是最好的,也許我可以通過設計一些稍微不同的表格來避免整個問題。

我基本上遵循了有關如何在一種表單中提交多個實體的說明https://groups.google.com/forum/?fromgroups#!topic/symfony2/DjwwzOfUIuQ%5B1-25%5D

首先,這是我的實體:

//@ORM\Entity
class Game {

    //@ORM\Column(name="scoreT1", type="integer", nullable=true)
    private $scoreT1;

    //@ORM\Column(name="scoreT2", type="integer", nullable=true)
    private $scoreT2;


    //@ORM\OneToOne(targetEntity="Bet", mappedBy="game")
    private $bet;
}
//@ORM\Entity
class Bet {
    //@ORM\Column(name="scoreT1", type="integer", nullable=true)
    private $scoreT1;

    //@ORM\Column(name="scoreT2", type="integer", nullable=true)
    private $scoreT2;

    // @ORM\OneToOne(targetEntity="Game", inversedBy="bet")
    private game;
}

這些是我的表格:

class GamesListType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('bets', 'collection',array(
                      'required' => false,
                      'allow_add' => false,
                      'type' => new BetType()
        ))
        ;
    }

    public function getDefaultOptions(array $options)
    {
        return array('data_class' => 'Strego\TippBundle\Form\Model\BetCollection');
    }
}
class BetType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('scoreT1')
                ->add('scoreT2');
        ;
    }

     public function getDefaultOptions(array $options)
    {
        return array('data_class' => 'Strego\TippBundle\Entity\Bet');
    }
}

為了從GamesListForm中的“ mainEntity”獲得賭注,我創建了一個專用的Collectionclass:

use Doctrine\Common\Collections\ArrayCollection;
class BetCollection extends ArrayCollection {

    public function getBets(){
        return $this;//->toArray();
    }
}

這是我正在研究的環境。 我的要求是以某種方式顯示游戲列表和該游戲的投注形式。 我公司目前正在努力實現它是這樣的:

{% for bet in form.bets %}
     bet.get('value').game.scoreT1  // <-- this is my current issue
        <div class="row">{{ form_widget(item) }}</div>
{% endfor %}

我正在解釋整個場景,因為我想輸入一些信息來實現游戲列表及其旁邊的表單。 另一個想法是采用3種形式:GamesList / Game / Bet,但是不知何故,我陷入了一個無限循環,被symfony阻止了。 是否存在3層表單的一般問題?

我想到了。 問題出在別的地方。 相關實體及其屬性可以通過以下方式輕松訪問:

form.get('value').relatedEntity.property

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM