I am currenty struggling with the forms and the held entities, because I am not able to access attributes from a related entity within a form.
form.get('value') // access current entity
form.get('value').relatedEntity // (access the related entity)
form.get('value').relatedEntity.property // is not working
I would like to explain my whole scenario more in a detail, because I think my current solution is not the best and maybe I can avoid the whole issue with designing my forms a little bit different.
I basically followed the instruction on how to submit multiple entities within one form https://groups.google.com/forum/?fromgroups#!topic/symfony2/DjwwzOfUIuQ%5B1-25%5D
First, here are my Entities:
//@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;
}
And these are my Forms:
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');
}
}
In order to get the bets from the “mainEntity” in the GamesListForm I created a dedicated Collectionclass:
use Doctrine\Common\Collections\ArrayCollection;
class BetCollection extends ArrayCollection {
public function getBets(){
return $this;//->toArray();
}
}
This is more the environment I’m working on. My requirement is to somehow show a list of games and a form to bet on this games. I currently try to achieve it like this:
{% for bet in form.bets %}
bet.get('value').game.scoreT1 // <-- this is my current issue
<div class="row">{{ form_widget(item) }}</div>
{% endfor %}
I am explaining the whole scenario, because I would like some input how to achieve the list of games and the forms next to it. Another Idea was to have 3 Forms: GamesList / Game / Bet, but somehow i ran into an endlessloop which was stopped by symfony. Is there a general issue with 3 layers in forms?
I figured it out. The issue was somewhere else.
Related entities and their properties can easily accessed via: