私がやろうとしているのは、別のエンティティ(ファイルエンティティ)から注目の写真を選択できる投稿(投稿エンティティ)フォームを取得することです。
最後に、「featured Image」タイプのファイルのみを表示したいのですが、query_builderを削除しても、次のような例外が発生します。
クラス「Site\Backend \ Adminbundle \ Entity \ File」は、管理されたDoctrineエンティティではないようです。マップするのを忘れましたか?
これが私のPostTypeフォームです
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('title')
->add('thumb', 'entity', array(
'class' => 'Site\Backend\Adminbundle\Entity\File',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('f')
//->where('f.state = :state')
//->setParameter('state', $prms['state'])
->orderBy('f.dateUpdated', 'DESC');
}
))
}
一方、私には2つのエンティティがあります。
これが私のファイルエンティティです
<?php
namespace Site\Backend\AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Site\Backend\AdminBundle\Entity\File
*
* @ORM\Table()
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class File {
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var \DateTime
*
* @ORM\Column(name="dateCreated", type="datetime")
*/
private $dateCreated;
/**
* @var \DateTime
*
* @ORM\Column(name="dateUpdated", type="datetime")
*/
private $dateUpdated;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @Assert\File(maxSize="6000000")
*/
private $file;
/**
* @ORM\Column(type="string", length=255)
*/
public $path;
/**
* @ORM\ManyToOne(targetEntity="TypeFile", inversedBy="files")
*/
private $type;
/**
* @var string
*
* @ORM\OneToMany(targetEntity="Site\Backend\BlogBundle\Entity\Post", mappedBy="thumb")
*/
private $posts;
public function __construct() {
$this->setDateCreated(new \DateTime());
$this->setDateUpdated(new \DateTime());
$this->type = new ArrayCollection();
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload() {
$this->setDateUpdated(new \DateTime());
if (null !== $this->file) {
// do whatever you want to generate a unique name
$this->path = sha1(uniqid(mt_rand(), true)) . '.' . $this->file->guessExtension();
} else {
//throwException($e);
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload() {
if (null === $this->file) {
return;
}
// if there is an error when moving the file, an exception will
// be automatically thrown by move(). This will properly prevent
// the entity from being persisted to the database on error
$this->file->move($this->getUploadRootDir(), $this->path);
unset($this->file);
}
/**
* @ORM\PostRemove()
*/
public function removeUpload() {
if ($file = $this->getAbsolutePath()) {
if (file_exists($file)) {
unlink($file);
}
}
}
public function getAbsolutePath() {
return null === $this->path ? null : $this->getUploadRootDir() . '/' . $this->path;
}
public function getWebPath() {
return null === $this->path ? null : './' . $this->getUploadDir() . '/' . $this->path;
}
protected function getUploadRootDir() {
// the absolute directory path where uploaded documents should be saved
return __DIR__ . '/../../../../../web/' . $this->getUploadDir();
}
protected function getUploadDir() {
// get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view.
return 'uploads/' . $this->type;
}
/**
* Get id
*
* @return integer
*/
public function getId() {
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return File
*/
public function setName($name) {
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName() {
return $this->name;
}
/**
* Set path
*
* @param string $path
* @return File
*/
public function setPath($path) {
$this->path = $path;
return $this;
}
/**
* Get path
*
* @return string
*/
public function getPath() {
return $this->path;
}
public function __toString() {
return $this->name;
}
/**
* Add type
*
* @param Site\Backend\AdminBundle\Entity\TypeFile $type
* @return File
*/
public function addType(\Site\Backend\AdminBundle\Entity\TypeFile $type) {
$this->type[] = $type;
return $this;
}
/**
* Remove type
*
* @param Site\Backend\AdminBundle\Entity\TypeFile $type
*/
public function removeType(\Site\Backend\AdminBundle\Entity\TypeFile $type) {
$this->type->removeElement($type);
}
/**
* Get type
*
* @return Doctrine\Common\Collections\Collection
*/
public function getType() {
return $this->type;
}
/**
* Set type
*
* @param Site\Backend\AdminBundle\Entity\TypeFile $type
* @return File
*/
public function setType(\Site\Backend\AdminBundle\Entity\TypeFile $type = null) {
$this->type = $type;
return $this;
}
public function getFile() {
return $this->file;
}
public function setFile($file) {
$this->file = $file;
}
/**
* Set dateCreated
*
* @param \DateTime $dateCreated
* @return File
*/
public function setDateCreated($dateCreated) {
$this->dateCreated = $dateCreated;
return $this;
}
/**
* Get dateCreated
*
* @return \DateTime
*/
public function getDateCreated() {
return $this->dateCreated;
}
/**
* Set dateUpdated
*
* @param \DateTime $dateUpdated
* @return File
*/
public function setDateUpdated($dateUpdated) {
$this->dateUpdated = $dateUpdated;
return $this;
}
/**
* Get dateUpdated
*
* @return \DateTime
*/
public function getDateUpdated() {
return $this->dateUpdated;
}
/**
* Add posts
*
* @param \Site\Backend\BlogBundle\Entity\Post $posts
* @return File
*/
public function addPost(\Site\Backend\BlogBundle\Entity\Post $posts)
{
$this->posts[] = $posts;
return $this;
}
/**
* Remove posts
*
* @param \Site\Backend\BlogBundle\Entity\Post $posts
*/
public function removePost(\Site\Backend\BlogBundle\Entity\Post $posts)
{
$this->posts->removeElement($posts);
}
/**
* Get posts
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPosts()
{
return $this->posts;
}
}
そして、これが私の投稿エンティティです
<?php
namespace Site\Backend\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Post
*
* @ORM\Table()
* @ORM\Entity
*/
class Post
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* @var \DateTime
*
* @ORM\Column(name="dateCreated", type="datetime")
*/
private $dateCreated;
/**
* @var \DateTime
*
* @ORM\Column(name="dateUpdated", type="datetime")
*/
private $dateUpdated;
/**
* @var \DateTime
*
* @ORM\Column(name="datePublished", type="datetime", nullable=true)
*/
private $datePublished;
/**
* @var string
*
* @ORM\Column(name="state", type="string", length=255)
*/
private $state;
/**
* @var string
*
* @ORM\Column(name="content", type="text")
*/
private $content;
/**
* @var Author
* @ORM\ManyToOne(targetEntity="Site\Backend\AdminBundle\Entity\User", inversedBy="posts")
*/
private $author;
/**
* @var Thumb
* @ORM\ManyToOne(targetEntity="Site\Backend\AdminBundle\Entity\File", inversedBy="posts")
*/
private $thumb;
/**
* @var Comments
* @ORM\OneToMany(targetEntity="Comment", mappedBy="post")
*/
private $comments;
/**
* @var Categories
* @ORM\ManyToMany(targetEntity="Category", cascade={"persist"})
*/
private $categories;
/**
* @var Tags
* @ORM\ManyToMany(targetEntity="Tag", cascade={"persist"})
*/
private $tags;
/**
* Constructeur
*/
public function __construct()
{
$this->setDateCreated(new \DateTime());
$this->setDateUpdated(new \DateTime());
$this->comments = new ArrayCollection();
$this->Categories = new ArrayCollection();
$this->tags = new ArrayCollection();
}
public function __toString(){
return $this->title;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
* @return Post
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set dateCreated
*
* @param \DateTime $dateCreated
* @return Comment
*/
public function setDateCreated($dateCreated)
{
$this->dateCreated = $dateCreated;
return $this;
}
/**
* Get dateCreated
*
* @return \DateTime
*/
public function getDateCreated()
{
return $this->dateCreated;
}
/**
* Set dateUpdated
*
* @param \DateTime $dateUpdated
* @return Comment
*/
public function setDateUpdated($dateUpdated)
{
$this->dateUpdated = $dateUpdated;
return $this;
}
/**
* Get dateUpdated
*
* @return \DateTime
*/
public function getDateUpdated()
{
return $this->dateUpdated;
}
/**
* Get datePublished
*
* @return \DateTime
*/
public function getDatePublished()
{
return $this->datePublished;
}
/**
* Set datePublished
*
* @param \DateTime $datePublished
* @return Comment
*/
public function setDatePublished($datePublished)
{
$this->datePublished = $datePublished;
return $this;
}
/**
* Set state
*
* @param string $state
* @return Post
*/
public function setState($state)
{
$this->state = $state;
return $this;
}
/**
* Get state
*
* @return string
*/
public function getState()
{
return $this->state;
}
/**
* Set content
*
* @param string $content
* @return Post
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set author
*
* @param \Site\Backend\AdminBundle\Entity\User $author
* @return Post
*/
public function setAuthor(\Site\Backend\AdminBundle\Entity\User $author = null)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* @return \Site\Backend\AdminBundle\Entity\User
*/
public function getAuthor()
{
return $this->author;
}
/**
* Add comments
*
* @param \Site\Backend\BlogBundle\Entity\Comment $comments
* @return Post
*/
public function addComment(\Site\Backend\BlogBundle\Entity\Comment $comments)
{
$this->comments[] = $comments;
return $this;
}
/**
* Remove comments
*
* @param \Site\Backend\BlogBundle\Entity\Comment $comments
*/
public function removeComment(\Site\Backend\BlogBundle\Entity\Comment $comments)
{
$this->comments->removeElement($comments);
}
/**
* Get comments
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getComments()
{
return $this->comments;
}
/**
* Add Categories
*
* @param \Site\Backend\BlogBundle\Entity\Category $categories
* @return Post
*/
public function addCategory(\Site\Backend\BlogBundle\Entity\Category $category)
{
$this->categories[] = $category;
return $this;
}
/**
* Add Categories
*
* @param \Site\Backend\BlogBundle\Entity\Category $categories
* @return Post
*/
public function addCategoriesNew(\Site\Backend\BlogBundle\Entity\Category $category)
{
$this->categories[] = $category;
return $this;
}
/**
* Remove Categories
*
* @param \Site\Backend\BlogBundle\Entity\Category $categories
*/
public function removeCategory(\Site\Backend\BlogBundle\Entity\Category $category)
{
$this->categories->removeElement($category);
}
/**
* Remove Categories
*
* @param \Site\Backend\BlogBundle\Entity\Category $categories
*/
public function removeCategoriesNew(\Site\Backend\BlogBundle\Entity\Category $category)
{
}
/**
* Get Categories
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCategories()
{
return $this->categories;
}
/**
* Get Categories
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCategoriesNew()
{
//return $this->categories;
}
/**
* Add tags
*
* @param \Site\Backend\BlogBundle\Entity\tag $tags
* @return Post
*/
public function addTag(\Site\Backend\BlogBundle\Entity\Tag $tags)
{
$this->tags[] = $tags;
return $this;
}
/**
* Add tags
*
* @param \Site\Backend\BlogBundle\Entity\tag $tags
* @return Post
*/
public function addTagsNew(\Site\Backend\BlogBundle\Entity\Tag $tags)
{
$this->tags[] = $tags;
return $this;
}
/**
* Remove tags
*
* @param \Site\Backend\BlogBundle\Entity\Tag $tags
*/
public function removeTag(\Site\Backend\BlogBundle\Entity\Tag $tags)
{
$this->tags->removeElement($tags);
}
/**
* Remove Tags
*
* @param \Site\Backend\BlogBundle\Entity\tag $tags
*/
public function removeTagsNew(\Site\Backend\BlogBundle\Entity\Tag $tags)
{
//$this->tags->removeElement($tags);
}
/**
* Get Tags
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTags()
{
return $this->tags;
}
/**
* Get Tags
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTagsNew()
{
//return $this->Tags;
}
/**
* Add Categories
*
* @param \Site\Backend\BlogBundle\Entity\Category $categories
* @return Post
*/
public function addCategorie(\Site\Backend\BlogBundle\Entity\Category $categories)
{
$this->categories[] = $categories;
return $this;
}
/**
* Remove Categories
*
* @param \Site\Backend\BlogBundle\Entity\Category $categories
*/
public function removeCategorie(\Site\Backend\BlogBundle\Entity\Category $categories)
{
$this->categories->removeElement($categories);
}
/**
* @ORM\preUpdate
*/
public function setUpdateValue(){
$this->setDateUpdated(new \DateTime());
}
/**
* Set thumb
*
* @param \Site\Backend\AdminBundle\Entity\File $thumb
* @return Post
*/
public function setThumb(\Site\Backend\AdminBundle\Entity\File $thumb = null)
{
$this->thumb = $thumb;
return $this;
}
/**
* Get thumb
*
* @return \Site\Backend\AdminBundle\Entity\File
*/
public function getThumb()
{
return $this->thumb;
}
}
よろしくお願いします。はっきりしているといいのですが、そうでない場合は教えてください。