doulu8341 2016-12-09 15:56
浏览 32
已采纳

如何使用Symfony2和SonataAdmin从mongodb数据库上传图像?

I don't know for sure if this question is fit to be here, but now that is my last option.

I am working in a symfony2 project, with a sonata admin and a mongodb database. We use Vich bundle to upload image to a amazon server.

I have two images, one is working normally, and the other not for some unknown reason.

When I try to add an image in the Sonata admin it simply doesn't update the photo, or simply keep in blank.

Here is the code of the one that is working normally.

@my document

/**
 * @var string
 *
 * @MongoDB\String
 * @Serializer\Since("2.0")
 */
protected $image_button;


/**
   * @Vich\UploadableField(mapping="folder_images", fileNameProperty="image_button")
   * @var File $image_button_file
   */
protected $image_button_file;


//GETTERS AND SETTERS

/**
 * Set imageButton
 *
 * @param string $imageButton
 * @return self
 */
public function setImageButton($imageButton)
{
    $this->image_button = $imageButton;
    return $this;
}

/**
 * Get imageButton
 *
 * @return string $imageButton
 */
public function getImageButton()
{
    return $this->image_button;
}



public function setImageButtonFile($imageButtonFile)
{
    $this->image_button_file = $imageButtonFile;
    if ($imageButtonFile) {
        $this->setUpdatedAt(new \DateTime());
    }
    return $this;
}


public function getImageButtonFile()
{
    return $this->image_button_file;

@my admin

->tab('images')
     ->with('OTHERS',['class' => 'col-md-6'])
     ->add('image_button_file', 'custom_image')
  ->end()
->end()

BUT the following code doesn't

@my document

/**
 * @var string
 *
 * @MongoDB\String
 * @Serializer\Since("2.0")
 */
protected $image_wordpress_header;

/**
 * @Vich\UploadableField(mapping="folder_images", fileNameProperty="image_wordpress_header")
 * @var File $image_wordpress_header_file
 */
protected $image_wordpress_header_file;


//GETTERS AND SETTERS

/**
 * @return string
 */
public function getImageWordpressHeader()
{
    return $this->image_wordpress_header;
}

/**
 * @param string $image_wordpress_header
 */
public function setImageWordpressHeader($image_wordpress_header)
{
    $this->image_wordpress_header = $image_wordpress_header;
}

/**
 * @return File
 */
public function getImageWordpressHeaderFile()
{
    return $this->image_wordpress_header_file;
}

/**
 * @param File $image_wordpress_header_file
 */
public function setImageWordpressHeaderFile($image_wordpress_header_file)
{
    $this->image_wordpress_header_file = $image_wordpress_header_file;

}

@my admin

->tab('images')
     ->with('OTHERS',['class' => 'col-md-6'])
     ->add('image_wordpress_header_file', 'custom_image')
  ->end()
->end()

There is any suggestion at least how could I debug that?

  • 写回答

1条回答 默认 最新

  • doudun8705 2016-12-12 13:24
    关注

    Apparently I should update the document when changing the image. Otherwise it would not update the database. I don't know why this happened with one and didn't happen with the another variable.

    public function setImage2lFile($image2lFile)
         {
            $this->image2l_file = $image2lFile;
            if ($image2lFile) {
                $this->setUpdatedAt(new \DateTime());
            }
            return $this;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口
  • ¥15 如何能达到用ping0.cc检测成这样?如图
  • ¥15 关于#DMA固件#的问题,请各位专家解答!
  • ¥15 matlab生成的x1图不趋于稳定,之后的图像是稳定的水平线
  • ¥15 请问华为OD岗位的内部职业发展通道都有哪些,以及各个级别晋升的要求
  • ¥20 微信小程序 canvas 问题
  • ¥15 系统 24h2 专业工作站版,浏览文件夹的图库,视频,图片之类的怎样删除?
  • ¥15 怎么把512还原为520格式
  • ¥15 MATLAB的动态模态分解出现错误,以CFX非定常模拟结果为快照
  • ¥15 求高通平台Softsim调试经验