0

我目前正在努力处理表单和持有的实体,因为我无法从表单中的相关实体访问属性。

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 层是否存在一般问题?

4

1 回答 1

0

我想到了。问题出在其他地方。相关实体及其属性可以通过以下方式轻松访问:

form.get('value').relatedEntity.property
于 2012-08-17T18:00:05.097 回答