2013-08-28 55 views
0

我是一名学生,实际工作在我自己的Symfony2项目上,现在有几天我找不到解决方案。SonataAdminBundle文件上传:错误

更新:2013年3月9日

我的symfony和奏鸣曲管理包的当前版本,需要一个形式,我的管理与多个图片上传。

下面的代码我提出是基于此安装文件:

http://sonata-project.org/bundles/admin/master/doc/reference/recipe_file_uploads.html

在我的情况我在束有一个实体的项目(PF \捆绑\ BlogBu​​ndle \实体\ Projects.php)。在这个实体中,我有$ image1(这相当于文档中的文件名),当然还有未映射的属性文件。所有的字符串和配置,因为它必须是。 (请注意,我使用image1而不是文件名在我的情况下//文档)。

<?php 

namespace Pf\Bundle\BlogBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\HttpFoundation\File\UploadedFile; 
use Symfony\Component\Validator\Constraints as Assert; 


/** 
* Projects 
* 
* @ORM\Table() 
* @ORM\Entity(repositoryClass="Pf\Bundle\BlogBundle\Entity\ProjectRepository") 
    * @ORM\HasLifecycleCallbacks() 
*/ 
class Projects 
{ 

    const SERVER_PATH_TO_IMAGE_FOLDER = '/uploads/medias'; 

    /** 
    * Unmapped property to handle file uploads 
    */ 
    private $file; 

    /** 
    * 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(
      Projects::SERVER_PATH_TO_IMAGE_FOLDER, 
      $this->getFile()->getClientOriginalName() 
     ); 

     // set the path property to the filename where you've saved the file 
     $this->image1 = $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(date('Y-m-d H:i:s')); 
    } 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="image1", type="string", length=100) 
    */ 
    private $image1; 

    //... 

    /** 
    * @var datetime 
    * 
    * @ORM\Column(name="updated", nullable=true) 
    */ 
    private $updated; 

    /** 
    * Set updated 
    * 
    * @param string $updated 
    * @return Projects 
    */ 
    public function setUpdated($updated) 
    { 
     $this->updated = $updated; 

     return $this; 
    } 

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

我也有一个管理控制器(PF \捆绑\ BlogBu​​ndle \管理员\ ProjectsAdmin.php)其中I具有以下形式(在 “奏管理员方式” 创建):

<?php 
namespace Pf\Bundle\BlogBundle\Admin; 

use Sonata\AdminBundle\Admin\Admin; 
use Sonata\AdminBundle\Datagrid\ListMapper; 
use Sonata\AdminBundle\Datagrid\DatagridMapper; 
use Sonata\AdminBundle\Validator\ErrorElement; 

use Sonata\AdminBundle\Form\FormMapper; 

class ProjectsAdmin extends Admin 
{ 
    // setup the default sort column and order 
    protected $datagridValues = array(
     '_sort_order' => 'DESC', 
     '_sort_by' => 'id' 
    ); 

    protected function configureFormFields(FormMapper $formMapper) 
    { 
     $formMapper 
      ->add('file', 'file', array('required' => false, 'data_class' => null)) 
      ->add('image2', 'text') 
      ->add('image3', 'text') 
      ->add('link', 'text') 
      ->add('download_link', 'text') 
      ->add('content1', 'text') 
      ->add('content2', 'text') 
      ->add('title', 'text') 
      ->add('thumbnail', 'text') 
     ; 
    } 

    public function prePersist($projects) { 
     $this->manageFileUpload($projects); 
    } 

    public function preUpdate($projects) { 
     $this->manageFileUpload($projects); 
    } 

    private function manageFileUpload($projects) { 
     if ($projects->getFile()) { 
      $projects->refreshUpdated(); 
     } 
    } 

    protected function configureDatagridFilters(DatagridMapper $datagridMapper) 
    { 
     $datagridMapper 
      ->add('title') 
     ; 
    } 

    protected function configureListFields(ListMapper $listMapper) 
    { 
     $listMapper 
      ->addIdentifier('title') 
     ; 
    } 
} 

我有几个问题:

  • 如果我试图创建一个新的项目,在此搜索似乎是空每次我试图上传时间 。我可以使它在实体可空,但 那我就不在数据库中执行时发生

    例外得到任何网址都“INSERT INTO项目... 完整性约束违规:1048列‘图像1’不能为空

  • 通过编辑管理中的现有项目,它似乎工作..以及近..我没有得到任何错误,当上传文件,但我得到一个临时路径在数据库中,没有文件已被移动到良好的文件夹中。

看起来上传函数没有被调用。我尝试调试它,但找不到解决方案。

我已经按照一步一步的文件。唯一的区别是我不使用任何.yaml文件来配置我的实体..我必须?我在我的symfony上使用了注释,我想在同一时间使用orm.yaml和注释并不好...对吧?

任何帮助都比欢迎!

+0

您好!刚更新了帖子。 – daneczech

回答

0

“关于此主题的任何信息” 你见过http://symfony.com/doc/current/cookbook/form/form_collections.html

您应该将图像表单嵌入到父表单中。例如,

->add('myImage','collection',array('type'=>new MyImageType()))

而不是把多个图像1,图像2,...再拍窗体类,如。 MyImageType()并将其作为集合类型添加到现有表单中。

朝着这个方向努力,祝你好运。

+0

感谢您的回复。你已经在谈论我不知道的事情了。学习这个框架并不容易!尤其是独自一人:/ – daneczech

+0

仍然不知道如何通过这个诚实的..这样你可以写一个例子吗?会很好.. – daneczech

+0

我提供的链接有你需要的所有信息。我建议你制作两个实体:Task和Tag,就像它在文档中所说的一样。按照这个例子来理解它。然后,您可以一步一步调整以满足您的特定需求。 –