質問

I've this form:

<?php
namespace FEB\TwitterBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class TwitterpostType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('titulo')
                ->add('tweet', 'textarea')
                ->add('photo', 'file', array('required' => false))              
                ->add('tags', 'entity', array(
                                                'class' =>    'FEBTagsBundle:tag',
                                                'property' => 'tag',
                                                'empty_value' => 'Selecciona tags',
                                                'multiple' => true));               
    }
    public function getName()
    {
        return 'twitter_form';
    }
}

When I save it in my database, the field "tag" save this string:

Doctrine\Common\Collections\ArrayCollection@000000000b3d932100000000287ad87a

and not the value of the property "tag", what is my mistake? Or conversely, that's right and I have to show it in twig template correctle formatted?

Thank you in advance.

Edit1:

Solution: Finally, I have had to modified only my twitterpost Entity:

<?php

namespace FEB\TwitterBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Twitterpost
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="FEB\TwitterBundle\Entity\TwitterpostRepository")
 */
class Twitterpost
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="titulo", type="string", length=50)
     * @Assert\NotNull(message="Debe escribir un titulo")
     */
    private $titulo;

    /**
     * @var string
     *
     * @ORM\Column(name="tweet", type="string", length=145)
     * @Assert\NotNull(message="Debe escribir un tweet")
     */
    private $tweet;


  /**
   * Many-To-Many, Unidirectional
   *
   * @var ArrayCollection $tags
   *
   * @ORM\ManyToMany(targetEntity="\FEB\TagsBundle\Entity\tag")
   * @ORM\JoinTable(name="twitter_has_tag",
   *      joinColumns={@ORM\JoinColumn(name="twitter_id", referencedColumnName="id")},
   *      inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
   * )
   */
    private $tags;

      public function __construct()
   {
    $this->tags = new ArrayCollection();
   }


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

    /**
     * Set titulo
     *
     * @param string $titulo
     * @return Twitterpost
     */
    public function setTitulo($titulo)
    {
        $this->titulo = $titulo;

        return $this;
    }

    /**
     * Get titulo
     *
     * @return string 
     */
    public function getTitulo()
    {
        return $this->titulo;
    }

    /**
     * Set tweet
     *
     * @param string $tweet
     * @return Twitterpost
     */
    public function setTweet($tweet)
    {
        $this->tweet = $tweet;

        return $this;
    }

    /**
     * Get tweet
     *
     * @return string 
     */
    public function getTweet()
    {
        return $this->tweet;
    }

    /**
     * Set tags
     *
     * @param string $tags
     * @return Twitterpost
     */
    public function setTags($tags)
    {
        $this->tags = $tags;

        return $this;
    }

    /**
     * Get tags
     *
     * @return string 
     */
    public function getTags()
    {
        return $this->tags;
    }   
}

The Entity tag has no change.

That changes generate the next database table:
Table: twitter_has_tag
twitter_id
tag_id

I think it's all right, no?

役に立ちましたか?

解決

You need to do a ManyToMany relationship between Twitterposts and Tags entity.

You need to put something like this into you Twitterposts entity:

/**
 * @ORM\ManyToMany(targetEntity="Tags", inversedBy="twitterposts")
 * @ORM\JoinTable(name="twitterpost_tags",
 *   joinColumns={@ORM\JoinColumn(name="twitterpost_id", referencedColumnName="id")},
 *   inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
 * )
 */
private $tags;

And get this as an ArrayCollection:

public function __construct() {
    $this->tags = new ArrayCollection();
}

Make getter and setter to this element and do the other side too in your Tags entity.

Here is the Doctrine documentation: http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html#many-to-many-unidirectional

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top