【发布时间】:2023-03-22 16:20:01
【问题描述】:
我正在使用 Sonata Media Bundle 在 Sonata User Bundle avatar 中工作。 Sonata Admin(后端)中的每件事都按预期工作。即在更新表单管理员时,仅替换旧文件。(效果很好)。
问题:尝试在前端通过自定义表单编辑用户个人资料时。图像正确上传并替换旧图像,但它额外在文件夹中创建了一个新图像,也是媒体中的一个新条目。只有新的图像 id 被添加到用户条目(头像)中,而不是旧的。通常只会使用旧的。
我需要的是编辑/更新旧图像,而不是添加新图像和条目。
提示:文件夹权限很好 - 因为它编辑旧文件。
问题的预期解决方案:新条目如何?而不是编辑/更新。!
用户.php
namespace Application\Sonata\UserBundle\Entity;
// all use ... i deleted to reduce lines
/**
* @ORM\Table(name="fos__user_user")
* @ORM\Entity()
* @ORM\HasLifecycleCallbacks()
* @Assert\Callback(methods={ "isMediaSizeValid" })
*/
class User extends BaseUser {
/**
* @var string
*
* @ORM\OneToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media",cascade={"persist"} )
* @ORM\JoinColumns( { @ORM\JoinColumn( referencedColumnName="id", onDelete="CASCADE" ) } )
* @Assert\NotNull()
*/
private $profilePicture;
// generated getter and setter
public function setProfilePicture(\Application\Sonata\MediaBundle\Entity\Media $profilePicture = null) {
$this->profilePicture = $profilePicture;
return $this;
}
public function getProfilePicture() {
return $this->profilePicture;
}
public function isMediaSizeValid(ExecutionContextInterface $context) {
$this->fzValidateImage($context, $this->getProfilePicture());
}
private function fzValidateImage($context, $f) {
if ($f == NULL) {
// IMAGE can be null . If null it uses the default image
} else if ($f->getSize() > (self::FILE_SIZE * 1024)) {
$context->buildViolation('The file is too large ( %a% kb). Allowed maximum size is %b% kb.')->atPath(self::FILE_PATH)->setParameters(['%a%' => intval($f->getSize() / 1024), '%b%' => self::FILE_SIZE])->addViolation();
} else if ($f->getWidth() < self::FILE_MIN_WIDTH) {
$context->buildViolation('The image width is too small ( %a% px). Minimum width expected is %b% px.')->atPath(self::FILE_PATH)->setParameters(['%a%' => $f->getWidth(), '%b%' => self::FILE_MIN_WIDTH])->addViolation();
} else if ($f->getWidth() > self::FILE_MAX_WIDTH) {
$context->buildViolation('The image width is too big ( %a% px). Allowed maximum width is %b% px.')->atPath(self::FILE_PATH)->setParameters(['%a%' => $f->getWidth(), '%b%' => self::FILE_MAX_WIDTH])->addViolation();
} else if ($f->getHeight() < self::FILE_MIN_HEIGHT) {
$context->buildViolation('The image height is too small ( %a% px). Minimum height expected is %b% px.')->atPath(self::FILE_PATH)->setParameters(['%a%' => $f->getHeight(), '%b%' => self::FILE_MIN_HEIGHT])->addViolation();
} else if ($f->getHeight() > self::FILE_MAX_HEIGHT) {
$context->buildViolation('The image height is too big ( %a% px). Allowed maximum height is %b% px.')->atPath(self::FILE_PATH)->setParameters(['%a%' => $f->getHeight(), '%b%' => self::FILE_MAX_HEIGHT])->addViolation();
}
}
}
UserController.php 类型 1
/**
* @Route("/profile/edit", name="fz_user_profile_edit")
*/
public function editProfileAction(Request $request) {
$form = $this->get('sonata.user.profile.form');
$form->add('profilePicture', 'sonata_media_type', array(
'provider' => 'sonata.media.provider.image',
'context' => 'profile'
));
$form->get('profilePicture')->add('binaryContent', 'file', ['label' => 'Profile Picture']);
$form->get('profilePicture')->remove('unlink');
$formHandler = $this->get('sonata.user.profile.form.handler');
$process = $formHandler->process($user);
if ($process) {
$this->setFlash('notice', 'Profile updated!');
return $this->redirectToRoute('fz_user');
}
$x = ['cmf' => '', 'pTitle' => 'Profile'];
return $this->render('user/layout.html.twig', ['x' => $x, 'form' => $form->createView()]);
UserController.php 类型 2
/**
* @Route("/profile/edit", name="fz_user_profile_edit")
*/
public function editProfileAction(Request $request) {
$user = $this->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw $this->createAccessDeniedException('This user does not have access to this section.');
}
$builder = $this->createFormBuilder($user);
$builder->add('profilePicture', 'sonata_media_type', array(
'provider' => 'sonata.media.provider.image',
'context' => 'profile'
));
$form = $builder->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
$this->setFlash('notice', 'Profile updated!');
}
$x = ['cmf' => '', 'pTitle' => 'Profile'];
return $this->render('user/layout.html.twig', ['x' => $x, 'form' => $form->createView()]);
}
UserController.php 类型 1 - 代码使用 FOSUserBundle。和 UserController.php 类型 2 我的 symfony 文档的自定义代码..
【问题讨论】:
-
new_on_update => false ,替换拇指并添加新的参考文件。但它未能删除文件夹中的旧参考媒体。在 Admin 中,媒体实体被替换(好)。来自奏鸣曲媒体文档sonata-project.org/bundles/media/master/doc/reference/form.html。新的问题是:旧的参考文件,没有被删除。
标签: symfony sonata sonata-user-bundle sonata-media-bundle