I'm learning Symfony 4 right now and I came across this documentation about file upload in the controller page that I got confused.
public function new(Request $request)
{
$product = new Product();
$form = $this->createForm(ProductType::class, $product);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// $file stores the uploaded PDF file
/** #var Symfony\Component\HttpFoundation\File\UploadedFile $file */
$file = $product->getBrochure();
On form submission, $file was used to store the content of the uploaded file as specified by the annotation. But what's the point of this $product->getBrochure()? $product should held nothing inside since it was newly created from the Product entity.
In your controller you generate a form with an underlying object to store the data which is the Product Entity. The controller can be accessed on two ways.
first in the GET method without any form data and second in the POST method with data from the form fields. In Symfony the data from the form fields will be placed in your entity automatically and even validated automatically. It all happens with the line
$form->handleRequest($request);
So the uploaded file data is stored in the entity too and if you want to access it you will have to get it out of the entity by a public method like $product->getBrochure();
It seems the documentation in the Symfony 4 File Upload page is incorrect. This might be a little late for you, but the problem still exists.
Instead of using:
$file = $product->getBrochure();
use:
$file = $form['brochure']->getData();
The first will return the file name which is a string while the second will get the actual file.
Related
I have a form that I use both for registration and edition of the user informations. This form contains a profile picture property on which I put #Assert\Image.
I succeed in creating a new user through my registration form but when I try to edit the user informations (with a PATCH method, just to update what need to be updated) I encounter an error with a 'File could not be found' message.
I suppose it's because the path stored in the database is a string and my #Assert\Image want an image.
I'm not sure about how I should manage this kind of update.
When I dd() the $user right after the submission, I see that the profilePicture property still contains the path saved in the database.
Here is my function regarding the form handling:
public function myProfile(Request $request)
{
$user = $this->getUser();
$form = $this->createForm(UserFormType::class, $user, ['method' => 'PATCH']);
if ($request->isMethod('PATCH')){
$form->submit($request->request->get($form->getName()), false);
if ($form->isSubmitted() && $form->isValid()) {
//...
}
}
//if no request just display the page
return $this->render('connected/myProfile.html.twig', [
'user' => $user,
'userProfileForm' => $form->createView()
]);
}
The Validator will check if your object contains a image and that seems not the case when you’re updating your object.
A workaround is to use group validation you define a specific group to the property that have the assert Image and in the method getGroupSequence you return the group if you’re in creation (id == null) or if the property is setted.
i'm trying to do a simple add without the form generated by doctrine
$mail = new Subscription();
$request = $this->getRequest();
if ($request->getMethod() == "POST") {
$em = $this->getDoctrine()->getManager();
$samplees = $request->get("samplees");
$mail->setEmail($samplees);
$em->persist($mail);
$em->flush();
return $this->redirect($this->generateUrl('user_homepage'));
}
First of all, Doctrine2 will not handle any form facility (nor creation neither data binding process): the whole process is up to symfony and its form bundle.
That said, if you need to retrieve a posted data you need to modify
$samplees = $request->get("samplees");
into
$samplees = $request->request->get("samplees");
This because $request is the whole Request object (so, basically, it will handle also get parameters [$request->query->get(...)] just to say one of the functionalities)
I'm writing an import script for our database (which is running on mySql) from CSV files. Since importing with doctrine entities is so slow and memory intensive, I'm opting for the option to write native queries to do the import task.
However, before the actual import, I need to validate the values in the csv file, and I wonder if there is any way to make use of the entity properties definition (already defined in the orm xml files) to do the validation. For example, if that field is already defined as a string with max 255 char in length then I can some how grab that definition and do the validation of the value in the csv file.
I hope it makes sense, please let me know if my question is not clear at any part.
You could use the Symfony2 validator service to check data before importing it. However you would have to add the max length constraint as an assertion.
Example entity:
<?php
// src/Acme/YourBundle/Entity/Author.php
// ...
use Symfony\Component\Validator\Constraints as Assert;
class YourEntity
{
/**
* #Assert\Length(max=255)
*/
public $someString;
}
Your controller which handles the import:
<?php
// ...
use Acme\YourBundle\Entity\YourEntity;
public function indexAction()
{
//omitted: get your csv data first
// create a new instance of your entity
$entity = new YourEntity();
// populate your entity with data from your csv file
$entity->setSomeString($stringFromCsvFile);
// get the validator and validate your entity
$validator = $this->get('validator');
$errors = $validator->validate($entity);
if (count($errors) > 0) {
// there are errors! do something with them
} else {
// there are no errors, persist the entity
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
}
}
See http://symfony.com/doc/current/book/validation.html for more information.
We are using Symfony2 to create an API. When updating a record, we expect the JSON input to represent a serialized updated entity. The JSON data will not contain some fields (for instance, CreatedAt should be set only once when the entity is created - and never updated). For instance, here is an example JSON PUT request:
{"id":"1","name":"anyname","description":"anydescription"}
Here is the PHP code on the Controller that should update the entity according to the JSON above (we are using JMS serializer Bundle):
$supplier = $serializer->deserialize(
$this->get('request')->getContent(),
'WhateverEntity',
'json'
);
The EntityManger understands (correctly) that this is an update request (in fact, a SELECT query is implicitly triggered). The EntityManager also guess (not correctly) that CreatedAt property should be NULLified - it should instead keep the previous one.
How to fix this issue?
It's possible as well to do it with Symfony Serializer using object_to_populate option.
Example: I receive JSON request. If record exists in database I want to update fields received in body, if it does not exist I want to create new one.
/**
* #Route("/{id}", methods={"PUT"})
*/
public function upsert(string $id, Request $request, SerializerInterface $serializer)
{
$content = $request->getContent(); // Get json from request
$product = $this->getDoctrine()->getRepository(Product::class)->findOne($id); // Try to find product in database with provided id
if (!$product) { // If product does not exist, create fresh entity
$product = new Product();
}
$product = $serializer->deserialize(
$content,
Product::class,
'json',
['object_to_populate' => $product] // Populate deserialized JSON content into existing/new entity
);
// validation, etc...
$this->getDoctrine()->getManager()->persist($product); // Will produce update/instert statement
$this->getDoctrine()->getManager()->flush($product);
// (...)
using the JMSSerializerBundle follow the install instructions at
http://jmsyst.com/bundles/JMSSerializerBundle
either create your own serializer service or alter the JMSSerializerBundle to use the doctrine object constructor instead of the simple object constructor.
<service id="jms_serializer.object_constructor" alias="jms_serializer.doctrine_object_constructor" public="false"/>
This basically handles exactly what Ocramius solution does but using the JMSSerializerBundles deserialize.
I would use the Doctrine\ORM\Mapping\ClassMetadata API to discover existing fields in your entity.
You can do following (I don't know how JMSSerializerBundle works):
//Unserialize data into $data
$metadata = $em->getMetadataFactory()->getMetadataFor($FQCN);
$id = array();
foreach ($metadata->getIdentifierFieldNames() as $identifier) {
if (!isset($data[$identifier])) {
throw new InvalidArgumentException('Missing identifier');
}
$id[$identifier] = $data[$identifier];
unset($data[$identifier]);
}
$entity = $em->find($metadata->getName(), $id);
foreach ($metadata->getFieldNames() as $field) {
//add necessary checks about field read/write operation feasibility here
if (isset($data[$field])) {
//careful! setters are not being called! Inflection is up to you if you need it!
$metadata->setFieldValue($entity, $field, $data[$field]);
}
}
$em->flush();
I've created a simple module for displaying a flash game in a custom block by overwriting game_block_view() and game_block_info() in the sites/default/modules/game.module and it works ok.
I need however to pass user avatar and also gender and city (I've added the 2 mandatory fields to the registration form) through the FlashVars-parameter to the flash game in my block.
So I'm trying to overload the hook_user_load, because I suppose that this is the method where you add properties to the $user object after it has been initiated from the database (this probably happens when the user logins or alters his/her profile data?):
function game_user_load($users) {
global $user;
$uid = $user->uid;
$result = db_query('select filename from {file_managed} where uid=:uid', array(':uid' => array($uid)));
$avatar = $result->fetchField();
$users[$uid]->avatar = $avatar;
drupal_set_message("<pre>$uid: $avatar</pre>\n");
print_r($users);
}
Unfortunately I see no output produced by the last 2 lines above in the web page
What am I doing wrong?
Thank you!
Alex
The global user object does not go through hook_user_load(), see http://api.drupal.org/api/drupal/includes--session.inc/function/_drupal_session_read/7. Don't ask me why, that's just the way it is :)
When using user_load(), any added fields will automatically be loaded, you don't need custom code for that. You just need to know how to access them, which is a bit complicated.
Something like this should work:
global $user;
// $account is now a fully loaded user object.
$account = user_load($user->uid);
// Your field name is probably 'field_avatar'.
if ($avatar = field_get_items('user', $account, 'field_avatar')) {
dpm($avatar); // only works with devel.module, strongly suggested!
}