Symfony 如何在SonataAdminBundle中的博客实体中上载?
我是新的symfony,我尝试在SonataAdminBundle中上载img。我遵循以下文档: 但我有一个错误: 可捕获致命错误:参数1传递给 MyBlogBundle\Entity\Blog::setFile()必须是的实例 MyBlogBundle\Entity\UploadedFile,的实例 Symfony\Component\HttpFoundation\File\UploadedFile给定,在中调用 C:\OpenServer\domains\Symfony.test\vendor\Symfony\Symfony\src\Symfony\Component\PropertyAccess\PropertyAccessor.php 在第442行并定义 有人能帮我吗 我的博客实体: //src/MyBLogBundle/Entity/Blog.php 班级博客{Symfony 如何在SonataAdminBundle中的博客实体中上载?,symfony,sonata-admin,Symfony,Sonata Admin,我是新的symfony,我尝试在SonataAdminBundle中上载img。我遵循以下文档: 但我有一个错误: 可捕获致命错误:参数1传递给 MyBlogBundle\Entity\Blog::setFile()必须是的实例 MyBlogBundle\Entity\UploadedFile,的实例 Symfony\Component\HttpFoundation\File\UploadedFile给定,在中调用 C:\OpenServer\domains\Symfony.test\vendo
const SERVER_PATH_TO_IMAGE_FOLDER = 'src/MyBlogBundle/Resources/public/images';
/**
* Unmapped property to handle file uploads
*/
private $file;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="author", type="string", length=255)
*/
private $author;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="text", type="text")
*/
private $text;
/**
* @var \DateTime
*
* @ORM\Column(name="createDate", type="datetime")
*/
private $createDate;
/**
* @var \DateTime
*
* @ORM\Column(name="updateDate", type="datetime")
*/
private $updateDate;
/**
* @var string
*
* @ORM\Column(name="image", type="string", length=255)
*/
private $image;
/**
* @var string
*
* @ORM\Column(name="tag", type="string", length=255)
*/
private $tag;
/**
*
* @ORM\OneToMany(targetEntity="Comment",mappedBy="blog")
*/
private $comment;
/**
* Get id
*
* @return integer
*/
public function getId() {
return $this->id;
}
/**
* Set author
*
* @param string $author
* @return Blog
*/
public function setAuthor($author) {
$this->author = $author;
return $this;
}
/**
* Get author
*
* @return string
*/
public function getAuthor() {
return $this->author;
}
/**
* Set title
*
* @param string $title
* @return Blog
*/
public function setTitle($title) {
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle() {
return $this->title;
}
/**
* Set text
*
* @param string $text
* @return Blog
*/
public function setText($text) {
$this->text = $text;
return $this;
}
/**
* Get text
*
* @return string
*/
public function getText($count = NULL) {
if ($count != NULL) {
$arr = explode(' ', $this->text);
$arr = array_slice($arr, 0, $count);
$this->text = implode(' ', $arr) . '...';
}
return $this->text;
}
/**
* Set createDate
* @ORM\PrePersist
* @param \DateTime $createDate
* @return Blog
*/
public function setCreateDate() {
$this->createDate = new \DateTime();
return $this;
}
/**
* Get createDate
*
* @return \DateTime
*/
public function getCreateDate() {
return $this->createDate;
}
/**
* Set updateDate
* @ORM\PreUpdate
* @param \DateTime $updateDate
* @return Blog
*/
public function setUpdateDate() {
$this->updateDate = new \DateTime();
return $this;
}
/**
* Get updateDate
*
* @return \DateTime
*/
public function getUpdateDate() {
return $this->updateDate;
}
/**
* Set image
*
* @param string $image
* @return Blog
*/
public function setImage($image) {
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return string
*/
public function getImage() {
return $this->image;
}
/**
* Set tag
*
* @param string $tag
* @return Blog
*/
public function setTag($tag) {
$this->tag = $tag;
return $this;
}
/**
* Get tag
*
* @return string
*/
public function getTag() {
return $this->tag;
}
public function __construct() {
$this->comment = new ArrayCollection();
$this->updateDate = new \DateTime();
}
/**
* Add comment
*
* @param \MyBlogBundle\Entity\Comment $comment
* @return Blog
*/
public function addComment(\MyBlogBundle\Entity\Comment $comment) {
$this->comment[] = $comment;
return $this;
}
/**
* Remove comment
*
* @param \MyBlogBundle\Entity\Comment $comment
*/
public function removeComment(\MyBlogBundle\Entity\Comment $comment) {
$this->comment->removeElement($comment);
}
/**
* Get comment
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getComment() {
return $this->comment;
}
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null) {
$this->file = $file;
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile() {
return $this->file;
}
/**
* Manages the copying of the file to the relevant place on the server
*/
public function upload() {
// the file property can be empty if the field is not required
if (null === $this->getFile()) {
return;
}
// we use the original file name here but you should
// sanitize it at least to avoid any security issues
// move takes the target directory and target filename as params
$this->getFile()->move(
Image::SERVER_PATH_TO_IMAGE_FOLDER, $this->getFile()->getClientOriginalName()
);
// set the path property to the filename where you've saved the file
$this->filename = $this->getFile()->getClientOriginalName();
// clean up the file property as you won't need it anymore
$this->setFile(null);
}
/**
* Lifecycle callback to upload the file to the server
*/
public function lifecycleFileUpload() {
$this->upload();
}
/**
* Updates the hash value to force the preUpdate and postUpdate events to fire
*/
public function refreshUpdated() {
$this->setUpdated(new \DateTime("now"));
}
}
我的博客管理员:
名称空间MyBlogBundle\Admin
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
class BlogAdmin extends Admin
{
protected $baseRouteName = 'MyBlogBundle\Entity\BlogAdmin';
protected $baseRoutePattern = 'blog_admin';
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('title', 'text', array('label' => 'Blog Title'))
->add('tag','text')
->add('file', 'file', array('required' => false))
->add('text','textarea') //if no type is specified, SonataAdminBundle tries to guess it
->add('author')
;
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('title')
->add('author')
;
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('title')
->add('author')
;
}
public function prePersist($image) {
$this->manageFileUpload($image);
}
public function preUpdate($image) {
$this->manageFileUpload($image);
}
private function manageFileUpload($image) {
if ($image->getFile()) {
$image->refreshUpdated();
}
}
}
和Blog.orm.yml
src/MyBlogBundle/Resources/config/Doctrine/Image.orm.yml
您忘记为UploadedFile类添加use语句,必须添加: src/MyBLogBundle/Entity/Blog.php
<?php
use Symfony\Component\HttpFoundation\File\UploadedFile;
class Blog
{
}
这对我很有帮助,但随后出现了另一个错误:尝试调用类“MyBlogBundle\Entity\Blog”上的方法“setupDate”。我添加了新的proportie updated和方法setupDate和getUpdated:但是如果我再次尝试添加新的post-occure错误:在创建项的过程中发生了错误“MyBlogBundle\Entity\Blog:00000000 18B75CC600000001BADB528”。我罚款了我的错误:repositoryClass:MyBlogBundle\Entity\Repositories\ImageRepository-ImageRepository需要在BlogRepository上替换,并添加->添加('image','file',数组('data_class'=>NULL))
<?php
use Symfony\Component\HttpFoundation\File\UploadedFile;
class Blog
{
}