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 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分