Warning: Cannot use a scalar value as an array in /home/admin/public_html/forum/include/fm.class.php on line 757

Warning: Invalid argument supplied for foreach() in /home/admin/public_html/forum/include/fm.class.php on line 770
Форумы портала PHP.SU :: Версия для печати :: Загрузка изображения в SonataAdminBundle
Форумы портала PHP.SU » » CMS и фреймворки » Загрузка изображения в SonataAdminBundle

Страниц (1): [1]
 

1. KEFIR4UK - 20 Марта, 2015 - 19:24:38 - перейти к сообщению
Пытаюсь зарузить изображение в сонату но пока сохраняет только в временную папку с временным именем типа такого C:\OpenServer\userdata\temp\php8090.tmp. Читал https://sonata-project[dot]org/bundl[dot][dot][dot]ile_uploads[dot]html и http://symfony[dot]com/doc/current/c[dot][dot][dot]ile_uploads[dot]html но к так и не понял толком как это делать.
Код сущности Блог:
PHP:
скопировать код в буфер обмена
  1. <?PHP
  2.  
  3. namespace MyBlogBundle\Entity;
  4.  
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Symfony\Component\HttpFoundation\File\UploadedFile;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use MyBlogBundle\Admin\BlogAdmin;
  9.  
  10. /**
  11.  * Blog
  12.  * @ORM\Table()
  13.  * @ORM\Entity(repositoryClass="MyBlogBundle\Entity\BlogRepository")
  14.  * @ORM\HasLifecycleCallbacks()
  15.  */
  16. class Blog {
  17.  
  18.     /**
  19.      * @var integer
  20.      * @ORM\Column(name="id", type="integer")
  21.      * @ORM\Id
  22.      * @ORM\GeneratedValue(strategy="AUTO")
  23.      */
  24.     private $id;
  25.  
  26.     /**
  27.      * @var string
  28.      * @ORM\Column(name="author", type="string", length=255)
  29.      */
  30.     private $author;
  31.  
  32.     /**
  33.      * @var string
  34.      * @ORM\Column(name="title", type="string", length=255)
  35.      */
  36.     private $title;
  37.  
  38.     /**
  39.      * @var string
  40.      * @ORM\Column(name="text", type="text")
  41.      */
  42.     private $text;
  43.  
  44.     /**
  45.      * @var \DateTime
  46.      * @ORM\Column(name="createDate", type="datetime")
  47.      */
  48.     private $createDate;
  49.  
  50.     /**
  51.      * @var \DateTime
  52.      * @ORM\Column(name="updateDate", type="datetime")
  53.      */
  54.     private $updateDate;
  55.  
  56.     /**
  57.      * @var string
  58.      * @ORM\Column(name="image", type="string", length=255)
  59.      */
  60.     private $image;
  61.  
  62.     /**
  63.      * @var string
  64.      * @ORM\Column(name="tag", type="string", length=255)
  65.      */
  66.     private $tag;
  67.  
  68.     /**
  69.      * @ORM\OneToMany(targetEntity="Comment",mappedBy="blog")
  70.      */
  71.     private $comment;
  72.  
  73.     /**
  74.      * @return integer
  75.      */
  76.     public function getId() {
  77.         return $this->id;
  78.     }
  79.  
  80.     /**
  81.      * @param string $author
  82.      * @return Blog
  83.      */
  84.     public function setAuthor($author) {
  85.         $this->author = $author;
  86.  
  87.         return $this;
  88.     }
  89.  
  90.     /**
  91.      * @return string
  92.      */
  93.     public function getAuthor() {
  94.         return $this->author;
  95.     }
  96.  
  97.     /**
  98.      * @param string $title
  99.      * @return Blog
  100.      */
  101.     public function setTitle($title) {
  102.         $this->title = $title;
  103.  
  104.         return $this;
  105.     }
  106.  
  107.     /**
  108.      * @return string
  109.      */
  110.     public function getTitle() {
  111.         return $this->title;
  112.     }
  113.  
  114.     /**
  115.      * @param string $text
  116.      * @return Blog
  117.      */
  118.     public function setText($text) {
  119.         $this->text = $text;
  120.  
  121.         return $this;
  122.     }
  123.  
  124.     /**
  125.      * Get text
  126.      * @return string
  127.      */
  128.     public function getText($count = NULL) {
  129.         if ($count != NULL) {
  130.             $arr = explode(' ', $this->text);
  131.             $arr = array_slice($arr, 0, $count);
  132.             $this->text = implode(' ', $arr) . '...';
  133.         }
  134.         return $this->text;
  135.     }
  136.  
  137.     /**
  138.      * Set createDate
  139.      * @ORM\PrePersist
  140.      * @param \DateTime $createDate
  141.      * @return Blog
  142.      */
  143.     public function setCreateDate() {
  144.         $this->createDate = new \DateTime();
  145.  
  146.         return $this;
  147.     }
  148.  
  149.     /**
  150.      * @return \DateTime
  151.      */
  152.     public function getCreateDate() {
  153.         return $this->createDate;
  154.     }
  155.  
  156.     /**
  157.      * @ORM\PreUpdate
  158.      * @param \DateTime $updateDate
  159.      * @return Blog
  160.      */
  161.     public function setUpdateDate() {
  162.         $this->updateDate = new \DateTime();
  163.  
  164.         return $this;
  165.     }
  166.  
  167.     /**
  168.      * @return \DateTime
  169.      */
  170.     public function getUpdateDate() {
  171.         return $this->updateDate;
  172.     }
  173.  
  174.     /**
  175.      * @param string $image
  176.      * @return Blog
  177.      */
  178.     public function setImage($image) {
  179.         $this->image = $image;
  180.  
  181.         return $this;
  182.     }
  183.  
  184.     /**
  185.      * @return string
  186.      */
  187.     public function getImage() {
  188.         return $this->image;
  189.     }
  190.  
  191.     /**
  192.      * @param string $tag
  193.      * @return Blog
  194.      */
  195.     public function setTag($tag) {
  196.         $this->tag = $tag;
  197.  
  198.         return $this;
  199.     }
  200.  
  201.     /**
  202.      * @return string
  203.      */
  204.     public function getTag() {
  205.         return $this->tag;
  206.     }
  207.  
  208.     public function __construct() {
  209.         $this->comment = new ArrayCollection();
  210.         $this->updateDate = new \DateTime();
  211.     }
  212.  
  213.     /**
  214.      * @param \MyBlogBundle\Entity\Comment $comment
  215.      * @return Blog
  216.      */
  217.     public function addComment(\MyBlogBundle\Entity\Comment $comment) {
  218.         $this->comment[] = $comment;
  219.         return $this;
  220.     }
  221.  
  222.     /**
  223.      * @param \MyBlogBundle\Entity\Comment $comment
  224.      */
  225.     public function removeComment(\MyBlogBundle\Entity\Comment $comment) {
  226.         $this->comment->removeElement($comment);
  227.     }
  228.  
  229.     /**
  230.      * @return \Doctrine\Common\Collections\Collection
  231.      */
  232.     public function getComment() {
  233.         return $this->comment;
  234.     }
  235.  
  236.     const SERVER_PATH_TO_IMAGE_FOLDER = '/web/images';
  237.     private $file;
  238.  
  239.     /**
  240.      * @param UploadedFile $file
  241.      */
  242.     public function setFile(UploadedFile $file = null) {
  243.         $this->file = $file;
  244.     }
  245.  
  246.     /**
  247.      * @return UploadedFile
  248.      */
  249.     public function getFile() {
  250.         return $this->file;
  251.     }
  252.  
  253.    /**
  254.      * @ORM\PrePersist()
  255.      * @ORM\PreUpdate()
  256.      */
  257.     public function upload() {
  258.         if (null === $this->getFile()) {
  259.             return;
  260.         }
  261.         $this->getFile()->move(
  262.                 Blog::SERVER_PATH_TO_IMAGE_FOLDER, $this->getFile()->getClientOriginalName()
  263.         );
  264.         $this->filename = $this->getFile()->getClientOriginalName();
  265.         $this->setFile(null);
  266.     }
  267.  
  268.     public function lifecycleFileUpload() {
  269.         $this->upload();
  270.     }
  271.  
  272.     public function refreshUpdated() {
  273.         $this->setUpdated(new \DateTime("now"));
  274.     }
  275.    
  276. }
  277.  

и админ Блог
PHP:
скопировать код в буфер обмена
  1. <?PHP
  2.  
  3. /*
  4.  * To change this license header, choose License Headers in Project Properties.
  5.  * To change this template file, choose Tools | Templates
  6.  * and open the template in the editor.
  7.  */
  8.  
  9. namespace MyBlogBundle\Admin;
  10.  
  11. use Sonata\AdminBundle\Admin\Admin;
  12. use Sonata\AdminBundle\Datagrid\ListMapper;
  13. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  14. use Sonata\AdminBundle\Form\FormMapper;
  15. use MyBlogBundle\Entity\Blog;
  16. use Symfony\Component\HttpFoundation\File\File;
  17. class BlogAdmin extends Admin
  18. {
  19.     protected $baseRouteName = 'MyBlogBundle\Entity\BlogAdmin';
  20.     protected $baseRoutePattern = 'blog_admin';  
  21.    
  22.     protected function configureFormFields(FormMapper $formMapper)
  23.     {
  24.         $formMapper
  25.             ->add('title', 'text', array('label' => 'Blog Title'))
  26.             ->add('tag','text')
  27.             ->add('image', 'file', array(
  28.                     'data_class'      => NULL))
  29.             ->add('text','textarea')
  30.             ->add('author')
  31.         ;
  32.     }
  33.     protected function configureDatagridFilters(DatagridMapper $datagridMapper)
  34.     {
  35.         $datagridMapper
  36.             ->add('title')
  37.             ->add('author')
  38.  
  39.         ;
  40.     }
  41.     protected function configureListFields(ListMapper $listMapper)
  42.     {
  43.         $listMapper
  44.             ->addIdentifier('title')
  45.             ->add('author')
  46.             ->add('image')
  47.         ;
  48.     }
  49.    
  50.        public function prePersist($image) {
  51.         $this->manageFileUpload($image);
  52.     }
  53.  
  54.     public function preUpdate($image) {
  55.         $this->manageFileUpload($image);
  56.     }
  57.  
  58.     private function manageFileUpload($image) {
  59.         if ($image->getFile()) {
  60.             $image->refreshUpdated();
  61.         }
  62.     }
  63.      
  64. }

Буду признательній за совет.

 

Powered by ExBB FM 1.0 RC1