我當前正在與表單和所持有的實體鬥爭,因爲我無法從表單中的相關實體訪問屬性。如何訪問表單中的相關實體?
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層的普遍問題?