I just upgraded API platform to version 3.0.
After the few classical modifications linked to the version upgrade, and despite the use of: php bin/console api:upgrade-resource
I notice that my entities are not exposed anymore when I go to the API documentation and if I try to access an endpoint, I get a route error:
No route found for "GET https://127.0.0.1:9000/api/XXX
I replaced all the ApiResource uses in my entities and rewrote my annotations.
example of an entity:
<?php
namespace App\Entity\Test;
use ApiPlatform\Metadata\ApiResource;
#[ApiResource(
collectionOperations: [
'get',
'post' => [
'denormalization_context' => ['groups' => ['create:xxx']]
]
],
itemOperations: [
'get' => [
'normalization_context' => ['groups' => ['read:fully:xxx']]
],
'put' => [
'denormalization_context' => ['groups' => ['update:xxx']]
],
'delete'
],
normalizationContext: ['groups' => ['read:xxx']]
)]
class Departement
{
....
}
Thanks in advance!
ok so, i update a little entity manually and now she is exposed !
<?php
namespace App\Entity\Agorha;
//use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use App\Entity\ChoixEcole;
use App\Repository\Agorha\EcoleRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
#[ORM\Entity(repositoryClass: EcoleRepository::class)]
#[ORM\Table(name: "agorha_ecole")]
#[ORM\HasLifecycleCallbacks()]
#[ApiResource(operations: [
new Get(),
new GetCollection()
])]
#[
UniqueEntity('code')
]
class Ecole
{
#[ORM\Id()]
#[ORM\GeneratedValue()]
#[ORM\Column(type: "integer")]
private $id;
i didn't see the results of the upgrade command, which ended up being an error and therefore did nothing.
in fact, it does not seem to exist
Command "api:upgrade-resource" is not defined.
anyone would know why ?
It looks like your entities are still in the <= v2.6 format despite your call to the api:upgrade-resource command.
See the migration documentation: instead of
'get', 'post', etc. you should use new Metadata classes Get(), Post(), etc.
Are you sure that the migration command returned no error?
In my case (migration from 2.6 to 3.0) the migration command was not available for unknwon reason (not found from the console).
Try migrating manually one entity to the new format and watch your openApi documentation to see if your endpoints are back.
Edit: Why didn't api:upgrade-resource work ?
As far as I understand, the migration command is provided in v2.7 to prepare migration to 3.0 but has been dropped from v3.0. So the proper way to migrate according to the doc is:
migrate to 2.7
call api:upgrade-resource and check all is working
THEN migrate to 3.0
like #MendelYev says i should run the api upgrade command before upgrade.
now i ve to upgrade manually my entities using PHP attributes and new Metadata classes from Doctrine
Related
I'm using Translatable and EasyAdmin in a Symfony 5 project and I have configured 2 languages.
The issue is I need to be able to edit the different languages of a record in EasyAdmin, I have checked the docs of Translatable, EasyAdmin and Symfony, There is very little information about how to integrate database translations into EasyAdmin.
Therefore, I'm a bit stuck in terms of code, I have tried configuring setTranslationParameters() inside the entity CRUD controller and changing some configuration in the DashboardController however, I don't think this is the right approach.
Any suggestions of how to solve this issue?
Thank you for your effort and time.
as of writing, this feature doesn't exist in EasyAdmin, please see the link to the answer to the issue on Github.
https://github.com/EasyCorp/EasyAdminBundle/issues/4982
However, a work around is possible with a different package:
remove doctrine-extensions/DoctrineExtensions and then install KnpLabs/DoctrineBehaviors
install a2lix/translation-form-bundle
Create a translation field:
<?php
declare(strict_types=1);
namespace App\Controller\Admin\Field;
use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
use EasyCorp\Bundle\EasyAdminBundle\Field\FieldTrait;
final class TranslationField implements FieldInterface
{
use FieldTrait;
public static function new(string $propertyName, ?string $label = null, array $fieldsConfig = []): self
{
return (new self())
->setProperty($propertyName)
->setLabel($label)
->setFormType(TranslationsType::class)
->setFormTypeOptions([
'default_locale' => 'cz',
'fields' => $fieldsConfig,
]);
}
}
Use the TranslationField inside your admin CRUD controller:
public function configureFields(string $pageName): iterable
{
return [
TextField::new('title', 'title')->hideOnForm(),
TranslationField::new('translations', 'translations', [
'title' => [
'field_type' => TextType::class,
'required' => true,
]
// add more translatable properties into the array
])->setRequired(true)
->hideOnIndex()
];
}
I'm currently working on an OroPlatform project and I need to add a custom field on the BusinessUnit core entity.
I have read the Oro documentation section about the way to extend core entities : https://doc.oroinc.com/backend/entities/extend-entities/#id1
<?php
namespace MyBundle\Bundle\AppBundle\Migrations\Schema\v1_0;
use Doctrine\DBAL\Schema\Schema;
use Oro\Bundle\EntityExtendBundle\EntityConfig\ExtendScope;
use Oro\Bundle\MigrationBundle\Migration\Migration;
use Oro\Bundle\MigrationBundle\Migration\QueryBag;
class AddColumnsToBusinessUnit implements Migration
{
public function up(Schema $schema, QueryBag $queries)
{
$table = $schema->getTable('oro_business_unit');
$table->addColumn('siret', 'string', [
'oro_options' => [
'extend' => ['owner' => ExtendScope::OWNER_CUSTOM],
'entity' => ['label' => 'siret'],
],
]);
}
}
When I run the command symfony console oro:migration:load --force, it works and the migration is applied to my database.
Now, I want a required field. I have seen the instruction 'notnull' => true to setup a non nullable field on the database.
Everything works well, but my field hasn't any JavaScript validation on the organization/business_unit/create route. Any ideas ?
You can validate the new field by extending the validation metadata that is already defined for the core entity you are extending.
To do this, please follow the official Symfony documentation and use the YML format:
https://symfony.com/doc/4.4/validation.html#constraint-configuration
The constraint that you can use for the field is "not blank."
Here is an example:
# src/<YourBundlePath>/Resources/config/validation.yml
Oro\Bundle\OrganizationBundle\Entity\BusinessUnit:
properties:
siret:
- NotBlank: ~
I try to set up a symfony project with the microcontroller trait. But instead of use a config.yml I want to use a config.php file.
return [
'framework' => [
'secret' => 'secret_'
]
];
What is the best practice to achieve this?
when using microkernel trait, you can use the configureContainer method in your front controller (app.php) to load configuration directly from an array, like this:
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
{
// PHP equivalent of config.yml
$c->loadFromExtension('framework', array(
'secret' => 'S0ME_SECRET'
));
}
docs here
You should use the container to set the parameters like
$container->setParameter('framework.secret', 'secret_');
as explained in the Symfony Docs
How do you configure Symfony2 Validator to use annotations outside of Core?
In core you would do the following:
$container->loadFromExtension('framework', array(
'validation' => array(
'enable_annotations' => true,
),
));
Taken from: http://symfony.com/doc/2.0/book/validation.html#configuration
For now to make validation work the rules are set within the method loadValidatorMetadata(ClassMetadata $metadata), it works but I prefer annotations.
Example Entity with validation annotations and alternative php method to set validation rules:
<?php
namespace Foo\BarBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity(repositoryClass="Foo\BarBundle\Entity\Repository\FooRepository")
* #ORM\Table(name="foo")
*/
class Foo {
/**
* #ORM\Column(type="integer", name="bar")
* #Assert\Type(
* type="integer",
* message="The value {{ value }} is not a valid {{ type }}."
* )
*/
protected $bar;
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('bar', new Assert\Type(array(
'type' => 'integer',
'message' => 'The value {{ value }} is not a valid {{ type }}.',
)));
}
}
Update 1
The issue now seems to be that the annotations are not being autoloaded correctly.
I load the annotations in to the namespace with:
\Doctrine\Common\Annotations\AnnotationRegistry
::registerAutoloadNamespace("Symfony\Component\Validator\Constraints\\", __DIR__.'/vendor/symfony/validator');
Then when it tries to autoload the annotations it looks for /vendor/symfony/validator/Symfony/Component/Validator/Constraints/Length.php which does not exist. The file is actually located at /vendor/symfony/validator/Constraints/Length.php
I could create a registerLoader() but would rather fix the code. When using Validator within Symfony2 Core that file location would be correct.
How do I make it autoload correctly or get composer to install Symfony2 components to the same location as core?
You need to register the Autoloader with AnnotationRegistry, so where ever you require vendor/autoload, for example bootstrap.php add the registerLoader().
//Composer libraries
$loader = require_once 'vendor/autoload.php';
\Doctrine\Common\Annotations\AnnotationRegistry::registerLoader([$loader, 'loadClass']);
Turns out the solution is quite straight forward.
The accepted answer provides a solution without giving any explaination regarding the failure.
The reason is simple. The default annotation loader which is provided by the Doctrine\Common\Annotations\AnnotationRegistry only handle PSR-0 namespaces, while the Symfony\Component\Validator\Constraints is a PSR-4 namespace. Thus, the loader fail to load the class. Registering the composer auloader with the AnnotationRegistry::registerLoader method solves the problem because that autoloader handle the PSR-4 namespaces.
You can refer to this question to get more detaits about PSR-0 and PSR-4 differences: What is the difference between PSR-0 and PSR-4?
I know that using the Doctrinebundle in Symfony2 it is possible to instantiate multiple DB connections under Doctrine...
$connectionFactory = $this->container->get('doctrine.dbal.connection_factory');
$connection = $connectionFactory->createConnection(array(
'driver' => 'pdo_mysql',
'user' => 'foo_user',
'password' => 'foo_pass',
'host' => 'foo_host',
'dbname' => 'foo_db',
));
I'm curious if this is the case if you are using PURELY Doctrine though?, I've set up Doctrine via Composer like so...
{
"config": {
"vendor-dir": "lib/"
},
"require": {
"doctrine/orm": "2.3.4",
"doctrine/dbal": "2.3.4"
}
}
And have been looking for my ConnectionFactory class but am not seeing it anywhere? Am I required to use Symfony2 to do this?
Should I just download ConnectionFactory.php from the DoctrineBundle and include it in my DBAL folder?? idk?
A bundle is only in the context of symfony needed, it wraps the orm into symfony infrastructure (services, etc.). For pure use of the orm you should read the ORM: Installation and Configuration. As you see you must create an entity manager by yourself with EntityManager::create($dbParams, $config), so simply create different entity managers for your different databases.
For DBAL use you should read DBAL: Configuration and see, a connection can simply obtained trough DriverManager::getConnection($connectionParams, $config); But if you are sure the ConnectionFactory has no dependency to symfony stuff and you really need it, you can try copy it to your code and construct a new factory to obtain a DBAL connection.
$connectionFactory = new ConnectionFactory(array());
$connection = $connectionFactory->createConnection(array(
'driver' => 'pdo_mysql',
'user' => 'foo_user',
'password' => 'foo_pass',
'host' => 'foo_host',
'dbname' => 'foo_db',
));
But take care, this is a DBAL connection, i.e. it's a abstraction layer which sits on top of PDO and only for pure SQL queries. If you need a entity manager you have to initialize it as mentioned in the docs above, or maybe you find another entity manager factory class, which you can "copy".