소나타 미디어 번들을 사용하여 소나타 사용자 번들 아바타에서 일하고 있습니다. 모든 것은 Sonata Admin (백엔드)에서 예상대로 작동합니다. 즉, 양식을 업데이트하는 동안 이전 파일 만 대체됩니다. (잘 작동합니다).소나타 사용자 번들 소나타 미디어 번들을 사용하는 아바타는 사용자 정의 형식으로 이미지를 업데이트하면서 이전 이미지를 업데이트하고 새 것을 만듭니다. [해결]
문제점 : 프론트 엔드에서 사용자 정의 양식으로 사용자 프로파일을 편집하려고하는 중. 이미지는 올 바르고 올드 파일 인 을 대체하지만 파일 폴더에 새 이미지를 추가하고 미디어의 새 항목을 추가합니다. 새 이미지 ID 만 이전 항목 인 대신 사용자 항목 (아바타)에 추가됩니다. 일반적으로 이전 버전 만 사용됩니다.
무엇이 필요합니까?은 기존 이미지를 편집/업데이트하고 새로운 이미지를 추가하지 않습니다. & 항목입니다.
힌트 : 오래된 파일을 편집하기 때문에 폴더 사용 권한이 좋습니다.
예상되는 문제 : 새 항목은 어떻게됩니까? 편집/업데이트하는 대신!
User.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 - 코드는 FOSUserBu 사용 ~. 그리고 UserController.php를 입력하십시오. symfony doc에 대한 내 커스텀 코드를 입력하십시오 ..
new_on_update => false는 엄지를 대체하고 새 참조 파일을 추가합니다. 그러나 폴더에있는 이전 참조 미디어를 삭제하지 못했습니다. 관리자에서 미디어 엔티티가 대체됩니다 (양호). 소나타 미디어 doc https://sonata-project.org/bundles/media/master/doc/reference/form.html에서 새로운 문제는 다음과 같습니다. 이전 참조 파일은 삭제되지 않습니다. –