symfony4

Best practice to return custom JSON on API-platform

流过昼夜 提交于 2019-12-13 08:49:01
问题 I want to update my existing API (created with FOSRest). I have lots of routes that return custom JSON objects, different from my entities. For example, I have an Offer entity <?php // api/src/Entity/Offer.php namespace App\Entity; use ApiPlatform\Core\Annotation\ApiResource; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * An offer from my shop - this description will be automatically extracted form the PHPDoc to document the API. * * @ApiResource

Symfony 4. Why does submitted Form just partially populate the Model?

这一生的挚爱 提交于 2019-12-13 05:32:44
问题 A bit in the panic - I am generating Symfony form for a complex search, i.e. mapped data to the entity will be used just for a search query building. I create simple form, model, some extended types from ChoiceType for prepopulation choices by some logic. The form is submitted with GET method. In the model you find maker and model fields for example. The latter populated on the frontend with AJAX, after maker has been selected. When I do submit the form, and maker and model have non-default

Request validator : validate an IRI

こ雲淡風輕ζ 提交于 2019-12-13 05:09:43
问题 I'm currently working with api-plateform which has been a great tool so far. I'd like to add some validator before my datas are stored into my database. Api-platform works with IRI instead of plain id (even if you can change this behavior). I was wondering what would be the best way to validate an IRI. Let's say I receive for instance "api/users/1". I would like to make sure that this IRI is actually valid (i.e the id exists in the table "user"). I could create a custom validator rule which

API Platform Did not show First Name And Last Name Upon normalizationContext and denormalizationContext

好久不见. 提交于 2019-12-13 04:27:29
问题 I am new to API Platform and I have some problem with normalizationContext and denormalizationContext for my User Entity on the API platform. When I added the normalizationContext and denormalizationContext to ApiResources, and yes I did add in as @Groups({"user:read", "user:write"}) above my first_name and last_name variable. But when trying to post data from the api platform, I didn't see my first_name and last_name variable option. This is my User entity <?php namespace App\Entity; use

Symfony 4, doctrine, getResult and getArrayResult and getScalarResult return same structure results

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 04:01:29
问题 From symfony 4, I create a sample repository class. From this class, I created a method for get the list of all email's users. I would like get a array structure like this : array( "email1", "email2", "email3", ... ) But with 'getResult' I get a multidimensional array. Then, I tested with getArrayResult and getScalarResult and I obtain each time exactly the same array structure result ! Below, my Service Class : <?php class UserRepository extends ServiceEntityRepository { public function _

Symfony 4, get root path directory from data fixture Class

梦想的初衷 提交于 2019-12-13 03:55:52
问题 From a Symfony 4 project, Is there a properly/symfony way for get the root path directory from a Fixture class ? The final goal : I use fixtures class for load some "default" datas in my database when I deploy my website. (May be its not a good practice) Among theses defaults datas, I need to store the root path directory of the project because I need this information from custom utils and services class in the project. I know there are multiple ways for that. Currently, I hard coded the

How to get the current session array count in twig?

南笙酒味 提交于 2019-12-12 22:31:19
问题 In my bootstrap file where I instantiate my twig I have: $twig->addGlobal('cart', $session->get('cart')); and in top navbar of my twig I have a badge to show how many items are in added in cart as below: {{ cart|length }} and my main file that is called after bootstrap file I said above, I have: if (!empty($_getvars['id'])) { $data = $session->get('cart'); if(!isset($data[$_getvars['id']])) { $data[$_getvars['id']] = 0; } $data[$_getvars['id']] += 1; $session->set('cart', $data); } print_r(

symfony 4 form collection entity with file type create

梦想与她 提交于 2019-12-12 22:28:02
问题 How to create and upload the document using entity where fileType field is embedded in parent form via collectionType. I did read the documentation Symfony Upload. But didn't manage to accomplish this. Always get this error "Type error: Argument 1 passed to App\Service\FileUploader::upload() must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile, instance of App\Entity\Attachment given". Below is my Invoice entity class Invoice { /** * @ORM\Id() * @ORM\GeneratedValue() *

Symfony AppExtension not loaded

耗尽温柔 提交于 2019-12-12 19:13:42
问题 I am using Symfony 4 with the preconfigured "App"-Bundle (it does not have "Bundle" in its name) and added an extension as below. When accessing routes, the debug output from the extension is not outputted so the extension logic is not running. Why? I did not register the extension anywhere, just followed the manual at http://symfony.com/doc/current/bundles/extension.html. <?php namespace App\DependencyInjection; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony

Symfony 4: “Autowire: you should configure its value explicitly.”

人盡茶涼 提交于 2019-12-12 10:50:22
问题 I'm starting to working with Symfony4 and I meet the following error when I try to run my server: Cannot autowire service "App\Controller\CharacterInformation": argument "$region" of method "__construct()" is type-hinted "string", you should configure its value explicitly. How I instantiate my class: /** * @Route("/") * @return Response */ function mainPage() { $characterInformation = new CharacterInformation('eu'); return new Response($characterInformation->getCharacter()); } The constructor