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

时间:2016-12-09 15:56:12

标签: php mongodb symfony sonata-admin sonata

我不确定这个问题是否适合在这里,但现在这是我的最后一个选择。

我在symfony2项目中工作,有一个奏鸣曲管理员和一个mongodb数据库。我们使用Vich包将图像上传到亚马逊服务器。

我有两个图像,一个正常工作,另一个不是出于某种未知原因。

当我尝试在Sonata管理员中添加图片时,它根本不会更新照片,或者只是保持空白。

以下是正常工作的代码。

@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()

但是以下代码

@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()

至少有什么建议可以调试吗?

1 个答案:

答案 0 :(得分:0)

显然我应该在更改图像时更新文档。否则它不会更新数据库。我不知道为什么会发生这种情况,而另一个变量没有发生。

public function setImage2lFile($image2lFile)
     {
        $this->image2l_file = $image2lFile;
        if ($image2lFile) {
            $this->setUpdatedAt(new \DateTime());
        }
        return $this;
    }
相关问题