Symfony2 - 表单 - 创建一个"团队"添加一个" Player"一次

时间:2014-04-14 21:00:13

标签: forms

首先感谢您的时间。我是一个使用Symfony2的新手,我试图建立一个管理足球比赛的网站。目前,我一直试图建立一个表单页面,我可以在其中更新一个" Team"实体逐一添加玩家。

我从关系开始:

FSS\CommonBundle\Entity\Team:
  type: entity
  repositoryClass: FSS\CommonBundle\Entity\TeamRepository
  table: team

  id:
    id:
      type: integer
      generator:
        strategy: AUTO

  fields:
    name:
      length: 30

  oneToOne:
    user:
      targetEntity: User
      mappedBy: team
      joinColumn:
        name: user_id
        referencedColumnName: id
    ranking:
      targetEntity: Ranking
      mappedBy: team
    cupRanking:
      targetEntity: CupRanking
      mappedBy: team

  manyToMany:
    players:
      targetEntity: Player
      joinTable:
        name: team_players
        joinColumns:
          team_id:
            referencedColumnName: id
        inverseJoinColumns:
          player_id:
            referencedColumnName: id
            unique: true

和玩家实体

FSS\CommonBundle\Entity\Player:
  type: entity
  repositoryClass: FSS\CommonBundle\Entity\PlayerRepository
  table: player

  id:
    id:
      type: integer
      generator:
        strategy: AUTO

  fields:
    name:
      length: 30
    surname:
      length: 30
    realTeam:
      length: 20
    role:
      length: 15
    cost:
      type: integer

我的实际形式是:

class FillTeamType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {

        $entityManager = $options['em'];
        $transformer = new UserToUsernameTransformer($entityManager);

        $builder->add('name', 'text', array('read_only' => true));
        $builder->add($builder->create('user', 'text', array('read_only' => true))->addModelTransformer($transformer));

        /* I would here to have exactly 25 fields, divided into 4 groups:
            3 Goalkeepers
            8 Defenders
            8 Midfielders
            6 Strikers

        For every player, I'd like an editor area or choice list or something, in which I can select one player from the Players Database Table and select it. On the form submitting, All Players should be added to the "players" field in my Team entity.   */


        $builder->add('save', 'submit');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'FSS\CommonBundle\Entity\Team',
        ))
        ->setRequired(array(
            'em',
        ))
        ->setAllowedTypes(array(
            'em' => 'Doctrine\Common\Persistence\ObjectManager',
        ));
    }

    public function getName() {
        return 'fillteam';
    }
} 

这是相关的控制器操作:

public function updateTeamAction(Request $request, $name) {
    /* @var $entityManager EntityManager */
    $entityManager = $this->getEntityManager();
    $teamService = new TeamService($entityManager);
    /* @var $user Team */
    $team = $teamService->findTeamByName($name);
    $form = $this->createForm('fillteam', $team, array('em' => $entityManager));

    $form->handleRequest($request);

    if ($form->isValid()) {

        $teamService->saveTeam($team);

        $this->get('session')->getFlashBag()->add(
            'confirmation',
            'Squadra aggiornata con successo!'
        );
        return $this->redirect($this->generateUrl('get_teams'));
    }

    return $this->render('FSSBackBundle:Admin:team.html.twig', array(
        'form' => $form->createView(),
        'name' => $team->getName()));
}

这是相关的树枝模板

{% extends 'FSSCommonBundle::common.html.twig' %}

{% form_theme form 'FSSBackBundle:Form:itemform.html.twig' %}

{% block title %} - Amministra Squadre{% endblock %}

{% block contentcontainer %}
    <div id="last-box" class="content">
        <h1>Amministra Squadra</h1>
        <hr>
        <div class="user-form">
            {{ form_start(form) }}
                {{ form_row(form.name) }}
                {{ form_row(form.user) }}
                <hr>
                <!-- Here I'd like to have my players fields -->
                {{ form_rest(form) }}
            {{ form_end(form) }}
        </div>
        <div class="admin-users">
            <a href="{{ url('get_teams' ) }}">
                <div id="register-item" class="button" >
                    Torna a Elenco Squadre
                </div>
            </a>
        </div>
        <hr>
        {% for flashMessage in app.session.flashbag.get('error') %}
            <div class="flash-error">
                {{ flashMessage }}
            </div>
        {% endfor %}
    </div>
{% endblock %}

那就是它。尝试不同的解决方案已经失去了三天,但没有人为我工作,所以如果有人愿意帮助我,我将非常感激:)

提前再次感谢:)

0 个答案:

没有答案