0

リストを表示しようとしています。「コレクション」を使用しない場合、どういうわけかフィールドがアクティブに見える唯一の方法ですが、存在しない場合に備えて追加したいと思います。私はそれがそれをする方法だと読んだ。

    <?php

namespace Monse\WebBundle\Form;

use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilder;

class BasedeDatosType extends AbstractType {
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('nombreBD')
            ->add('ServidorBD', 'collection', array('type' => new ServidoresBDType()))
        ;
    }  public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'Monse\WebBundle\Entity\BasedeDatos',
        );
    }
    public function getName()
    {
        return 'basededatos';
    } }

これを使用すると、フィールドは表示されません。-> add('ServidorBD')を使用すると機能しますが、ドロップダウンリストではなく、コレクションを使用していません。

<?php

namespace Monse\WebBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Monse\WebBundle\Entity\BasedeDatos
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class BasedeDatos
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

   /**
* @ORM\ManyToOne(targetEntity="Monse\WebBundle\Entity\ServidoresBD")
* @ORM\JoinColumn(name="servidores_id", referencedColumnName="id",nullable=false)
*
*/
    private $servidorBD;

    /**
     * @var string $nombreBD
     *
     * @ORM\Column(name="nombreBD", type="string", length=255,unique=true)
     */
    private $nombreBD;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set servidorBD
     *
     * @param integer $servidorBD
     */
    public function setServidorBD(\Monse\WebBundle\Entity\ServidoresBD $servidorBD)
    {
        $this->servidorBD = $servidorBD;
    }

    /**
     * Get servidorBD
     *
     * @return integer 
     */
    public function getServidorBD()
    {
        return $this->servidorBD;
    }

    /**
     * Set nombreBD
     *
     * @param string $nombreBD
     */
    public function setNombreBD($nombreBD)
    {
        $this->nombreBD = $nombreBD;
    }

    /**
     * Get nombreBD
     *
     * @return string 
     */
    public function getNombreBD()
    {
        return $this->nombreBD;
    }
    public function __construct()
    {
        $this->servidorBD = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add servidorBD
     *
     * @param Monse\WebBundle\Entity\ServidoresBD $servidorBD
     */
    public function addServidoresBD(\Monse\WebBundle\Entity\ServidoresBD $servidorBD)
    {
        $this->servidorBD[] = $servidorBD;
    }
}

とにかくこれを行うことはありますか?(ばかげた質問だとすみません、symfony2の正しい使い方を学ぼうとしています)

4

1 に答える 1

2

コレクションを使用すると、新しいフォームを動的に追加できます。したがって、JavaScriptを追加する必要があります

フォームビルダーに:

$builder
            ->add('nombreBD')
            ->add('ServidorBD', 'collection', array(
              'type' => new ServidoresBDType(),
              'allow_add' => true,
              'by_reference' => false
             ));

あなたにedit.html.twig(またはファイルを表示):ボタンとクリックイベントを追加します

<div id="collectionContainer"></div>
<script type="text/javascript">
$('#addField').click(function(){
            var collectionHolder = $('#ServidorBD');
            var prototype = collectionHolder.attr('data-prototype');
            var index = $('#collectionContainer').length - 1;

            form = prototype.replace(/\$\$name\$\$/g, index);
            $('#collectionContainer').append(form);
</script>
于 2012-07-04T08:23:22.130 に答える