我有那个实体 TwitterPost.php
<?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")
* @ORM\HasLifecycleCallbacks()
*/
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="twitterpost_tags",
* joinColumns={@ORM\JoinColumn(name="twitterpost_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="tag", referencedColumnName="id")}
* )
*/
private $tags;
/**
* @var string
*
* @ORM\Column(name="photo", type="string", length=255, nullable=true)
*/
private $photo;
/**
* @var string
*
* @ORM\Column(name="idpost", type="string", length=100)
*/
private $idpost;
/**
* @var string
*
* @ORM\Column(name="autor", type="string", length=50)
*/
private $autor;
/**
* @var \DateTime
*
* @ORM\Column(name="created", type="datetime")
*/
private $created;
public function __construct()
{
$this->setCreated(new \DateTime());
$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;
}
/**
* Set photo
*
* @param string $photo
* @return Twitterpost
*/
public function setPhoto($photo)
{
$this->photo = $photo;
return $this;
}
/**
* Get photo
*
* @return string
*/
public function getPhoto()
{
return $this->photo;
}
/**
* Set idpost
*
* @param string $idpost
* @return Twitterpost
*/
public function setIdPost($idpost)
{
$this->idpost = $idpost;
return $this;
}
/**
* Get idpost
*
* @return string
*/
public function getIdPost()
{
return $this->idpost;
}
/**
* Set autor
*
* @param string $autor
* @return Twitterpost
*/
public function setAutor($autor)
{
$this->autor = $autor;
return $this;
}
/**
* Get autor
*
* @return string
*/
public function getAutor()
{
return $this->autor;
}
/**
* Set created
*
* @param \DateTime $created
* @return Tag
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* @return \DateTime
*/
public function getCreated()
{
return $this->created;
}
}
和这个
/**
* Tag
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="FEB\TagsBundle\Entity\TagRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Tag
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="tag", type="string", length=255)
*/
private $tag;
/**
* @var string
*
* @ORM\Column(name="autor", type="string", length=50)
*/
private $autor;
/**
* @var \DateTime
*
* @ORM\Column(name="created", type="datetime")
*/
private $created;
public function __construct()
{
$this->setCreated(new \DateTime());
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set tag
*
* @param string $tag
* @return Tag
*/
public function setTag($tag)
{
$this->tag = $tag;
return $this;
}
/**
* Get tag
*
* @return string
*/
public function getTag()
{
return $this->tag;
}
/**
* Set autor
*
* @param string $autor
* @return Tag
*/
public function setAutor($autor)
{
$this->autor = $autor;
return $this;
}
/**
* Get autor
*
* @return string
*/
public function getAutor()
{
return $this->autor;
}
/**
* Set created
*
* @param \DateTime $created
* @return Tag
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* @return \DateTime
*/
public function getCreated()
{
return $this->created;
}
}
这是 TwitterPost 和标签之间的多对多关系。在我的表单中有一个选择字段,它是一个实体类标签。
<?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';
}
}
当我提交数据表单时,在“twitterpost”表中数据被正确保存,在辅助表“twitterpost_tags”中也是如此。
例如:
twitterpost_id tag_id
1-----------1
1-----------2
2-----------1
2-----------3
The tag table:
id---------tag
1----------tagA
2----------tagB
3----------tagC
当我想显示 Twitterpost 的所有数据时,显示标签名称而不是标签 ID 的最佳方法是什么?
比你先进。