1
Hello, I’m trying to make a form where the user just inserts an image. In theory, the image should be stored as a Blob file (Base64) in the database, but Symfony is storing "tmp/php/random value" in the "photo" field of the credential table. I don’t know if something is missing, because with a return $this->json($credencial->getFoto())
, the returned is actually a Base64 string.
Credential:
class Credencial {
/**
* @ORM\Column(name="foto", type="blob")
*/
protected $foto;
public getFoto() {
return $this->foto();
}
public setFoto($foto) {
$this->foto = $foto;
}
}
Credeltype:
class CredencialType extends AbstractType {
protected $router;
public function __construct(RouterInterface $router) {
$this->router = $router;
}
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('foto', FileType::class, array(
'label' => 'Foto',
'required' => false
))->add('save', SubmitType::class, array(
'label' => 'SALVAR',
'attr' => ['class' => 'btn btn-info']
));
}
}
Credential Route Controller to add new credentials:
/**
* @Route("/credencial/add", name="credencial_add")
* @Template()
*/
public function add(Request $request) {
$credencial = new Credencial();
$form = $this->createForm(CredencialType::class, $credencial);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($credencial);
$entityManager->flush();
return $this->redirectToRoute('credencial_add');
}
return ['form' => $form->createView()];
}
All help will be welcome, thank you so much!
When flush() is executed what appears in /var/log/dev.log? appears an error or says it has been saved correctly?
– fajuchem
When adding a new credential, var/log/dev.log does not receive a new line. What happens is that it is saving, but the "photo" field in the database instead of receiving the string with the image in Base64, receives something like "tmp/phpsomething random"
– Gustavo Kimura
I did the test again, field "photo" in the Database = "/tmp/phpr2bYka", I do not know if you need to do some conversion to Base64, but what I find strange is the fact of returning in Base64, but not saving, also in Base64 in persist
– Gustavo Kimura
Okay, I was just looking at documentation, and from what I’ve seen being stored in $Credential->photo is an instance of Uploadedfile, so maybe if you try to put this before the persist: "$Credential->setFoto($form['photo']->getData());"
– fajuchem
Hello friend(a)! It seems that it didn’t work "$Credential->setFoto($form['photo']->getData());" :(, still saving "tmp/phpBlabla"
– Gustavo Kimura
What I find strange is that "Return $this->json($form["photo"]->getData());" is returning: "data:image/jpeg;Base64,/9j/.........................................................." as if it were a string with the data in Base64
– Gustavo Kimura
Hello!!! I just ran a test with "$credential->setFoto(file_get_contents($form["photo"]->getData());" and it worked perfectly! Saved in the database as Base64, damn it, thank you very much, you gave me the keywords to research more about the problem!
– Gustavo Kimura