0

我有那个实体 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 的最佳方法是什么?

比你先进。

4

1 回答 1

0

我不确定我是否正确。要在 twitterpost 表单中显示所有标签,您已经在做正确的事情:

    ->add('tags', 'entity', array(
          'class' =>    'FEBTagsBundle:tag',
          'property' => 'tag',
          'empty_value' => 'Selecciona tags',
          'multiple' => true
));

这将创建一个多选框,您可以在其中从所有标签中进行选择。从文档

财产

类型:字符串

这是应该用于在 HTML 元素中将实体显示为文本的属性。如果留空,实体对象将被转换为字符串,因此必须具有 __toString() 方法。

如果您不想要多选而是复选框或单选按钮,请查看文档的这一部分


如果您询问不在 内部时如何输出数据TwitterpostType,您可以将它们视为Controller中的对象:

$repository = $this->getDoctrine()->getRepository('TwitterBundle:Twitterpost');
$post = $repository->findOne($id);

// to get all _tags_ of a post just call the getter:
$tags = $post->getTags();

树枝模板也是如此:

{# imagine you have all posts in a variable called 'posts' #}
{% for post in posts %}
     <h1>{{ post.titulo }}</h1>
     <div class="tags">
         {% for tag in posts.tags %}
         {{ tag.tag }}
         {% endfor %}
     </div>
{% endfor %}

编辑:

对于本机 SQL/DQL 查询,您需要一个自定义存储库类。在那里您可以存储自己的查询:

class TwitterpostRepository extends EntityRepository
{
    public function findAllOrderedByName()
    {
        return $this->getEntityManager()
            ->createQuery(
                'SELECT p FROM TwitterBundle:Twitterpost p ORDER BY p.name ASC'
            )
            ->getResult();
    }
}

然后你可以在控制器中这样使用它:

$repository = $this->getDoctrine()->getRepository('TwitterBundle:Twitterpost');
$post = $repository->findAllOrderedByName();
于 2013-08-13T13:03:09.610 回答