Doctrine PHP 8 Attributes - php

I use PHP8, Symfony 5.2 and Doctrine 3.0 in my project,
But the PHP 8 attributes, allowed since Doctrine 2.9, doesn't seem to work.
use Doctrine\ORM\Mapping\Entity;
**
* #Entity(repositoryClass="App\Repository\MyClassRepository")
*/
class MyClass
{
works fine.
use Doctrine\ORM\Mapping\Entity;
#[Entity(repositoryClass: MyClassRepository::class)]
class MyClass
{
Return [critical] Uncaught PHP Exception Doctrine\ORM\Mapping\MappingException: "Class "App\Entity\MyClass" is not a valid entity or mapped super class." at .../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php line 378
here is my composer.json :
"composer/package-versions-deprecated": "1.11.99.1",
"doctrine/doctrine-bundle": "^2.3",
"doctrine/doctrine-migrations-bundle": "^3.1",
"doctrine/orm": "^3.0",
"symfony/console": "5.2.*",
"symfony/dotenv": "5.2.*",
"symfony/flex": "^1.3.1",
"symfony/framework-bundle": "5.2.*",
"symfony/proxy-manager-bridge": "5.2.*",
"symfony/yaml": "5.2.*"

This is because of doctrine bundle configuration. If all entities within the bundle use attributes just switch the metadata driver from "annotation" to "attribute"
doctrine:
orm:
auto_generate_proxy_classes: true
entity_managers:
default:
...
mappings:
MyBundle:
type: attribute
If some entities within a bundles use attributes and some others annotations - than it is better either choose only one format for metadata or implement a custom metadata driver.

The solution is use a custom AnnotationDriver.
Sample fully works implementation: https://github.com/baraja-core/doctrine/blob/master/src/Orm/Mapping/AnnotationDriver.php

Related

symfony 3.4 data fixture

I upgraded symfony 3.3 to 3.4 and now having problem with datafixture bundle , trying to seed data for testing but got this error :
Attempted to call an undefined method named "addClass" of class
"Doctrine\ORM\Internal\CommitOrderCalculator"
this is part of my ccomposer.json file :
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0",
"sensio/generator-bundle": "^3.0",
"symfony/phpunit-bridge": "^3.0"
}
any help will be appreciated
You have to upgrade the data-fixtures manually in your composer.json file. Try to upgrade it to version 1.3 like this:
"doctrine/data-fixtures": "^1.3"

Adding a composer package to Laravel 5

"require": {
"infusionsoft/php-sdk": "^1.2",
"laravel/lumen-framework": "5.1.*",
"vlucas/phpdotenv": "~1.0",
"barryvdh/laravel-debugbar": "^2.2",
"goaop/framework": "^2.0",
"php-http/httplug": "^1.1",
"infusionsoft/old-php-isdk": "*"
},
I'm trying to use the "infusionsoft/old-php-isdk" which is in the packagist composer repository but it's not specific to Laravel. I'm using an order controller and it's code that I picked up from another developer so he references the current (and working) "infusionsoft/php-sdk" as the following
"use infusionsoft\infusionsoft;"
What namespace do I call it if it has no namespace from the package?
Here's the original link to the composer package.
https://packagist.org/packages/infusionsoft/old-php-isdk
Am I missing something? Do I need to assign it a namespace? What and where would I put it?

Could not load type "sonata_simple_formatter_type"

I'm using Symfony framework and SonataBundle to build the admin application.
When I'm trying to setup a field with type blob and using the "sonata_simple_formatter_type" of SonataFormatterBundle, i was got the error:
Could not load type "sonata_simple_formatter_type"
Here is my composer.json file:
"symfony/symfony": "2.8.*",
"doctrine/orm": "^2.4.8",
//*...*/
"sonata-project/admin-bundle": "2.3.10",
"sonata-project/doctrine-orm-admin-bundle": "2.3.3",
"sonata-project/user-bundle": "2.2.4",
"sonata-project/formatter-bundle": "2.3.3",
I was prefer to this link to making the admin app: sonata formatter
To fix the error, you also need to configure SonataFormatterBundle: https://sonata-project.org/bundles/formatter/master/doc/reference/installation.html
For the SonataAdminBundle you also need to do some extra configuration: https://sonata-project.org/bundles/formatter/master/doc/reference/formatter_widget.html#sonata-admin-integration

Symfony 3 / APCu + PHP7

I have a problem with APCu and PHP7 again.
Here is my conf.
framework:
validation:
cache: validator.mapping.cache.doctrine.apc
serializer:
cache: serializer.mapping.cache.apc
doctrine:
orm:
metadata_cache_driver: apcu
result_cache_driver: apcu
query_cache_driver: apcu
Website works ok, but when I send a form, I always get this error:
[2016-06-29 09:17:12] request.CRITICAL: Uncaught PHP Exception
Symfony\Component\Debug\Exception\UndefinedFunctionException:
"Attempted to call function "apc_fetch" from namespace
"Doctrine\Common\Cache"." at
ROUTE_TO_PROJECT/vendor/doctrine/cache/lib/Doctrine/Common/Cache/ApcCache.php
line 41 {"exception":"[object]
(Symfony\Component\Debug\Exception\UndefinedFunctionException(code:
0): Attempted to call function \"apc_fetch\" from namespace
\"Doctrine\Common\Cache\". at
ROUTE_TO_PROJECT/vendor/doctrine/cache/lib/Doctrine/Common/Cache/ApcCache.php:41)"}
[]
here is my composer:
"require": {
"php": ">=5.5.9",
"symfony/symfony": "3.0.*",
"doctrine/orm": "^2.5",
"doctrine/doctrine-bundle": "^1.6",
"doctrine/doctrine-cache-bundle": "^1.2",
"symfony/swiftmailer-bundle": "^2.3",
"symfony/monolog-bundle": "^2.8",
"sensio/distribution-bundle": "^5.0",
"sensio/framework-extra-bundle": "^3.0.2",
"incenteev/composer-parameter-handler": "^2.0",
"knplabs/knp-paginator-bundle": "^2.5",
"liip/imagine-bundle": "^1.5",
"jms/serializer-bundle": "^1.1",
"symfony/assetic-bundle": "^2.8",
"gopay/payments-sdk-php": "^1.1",
"knplabs/knp-snappy-bundle": "~1.4",
"friendsofsymfony/user-bundle": "~2.0#dev"
Why is Symfony call apc_fetch instead apcu_fatch?
In previous versions of APCu, the APC module and functions were provided as part of the library.
In the most recent (PHP 7) versions of APCu, backward compatible APC is a separate extension.
This is my solution:
services.yml
serializer.mapping.cache.apcu:
class: Doctrine\Common\Cache\ApcuCache
confing_prod.yml
serializer:
cache: serializer.mapping.cache.apcu
Idk if this solution is ok, but looks like it works. So if you know better solution, I would like to use it.
You can get apcu working with symfony validation using the doctrine cache wrapper
config.yml
validation:
cache: validator.mapping.cache
services.yml
doctrine.apcu.cache:
class: Doctrine\Common\Cache\ApcuCache
validator.mapping.cache:
class: Symfony\Component\Validator\Mapping\Cache\DoctrineCache
arguments: ['#doctrine.apcu.cache']

How rename namespace Application in SonataUserBundle?

My composer file:
"php": ">=5.3.3",
"symfony/symfony": "2.6.*",
"doctrine/orm": "~2.2,>=2.2.3",
"doctrine/doctrine-bundle": "~1.2",
"twig/extensions": "~1.0",
"symfony/assetic-bundle": "~2.3",
"symfony/swiftmailer-bundle": "~2.3",
"symfony/monolog-bundle": "~2.4",
"sensio/distribution-bundle": "~3.0",
"sensio/framework-extra-bundle": "~3.0",
"incenteev/composer-parameter-handler": "~2.0",
"friendsofsymfony/user-bundle": "1.3.3",
"sonata-project/core-bundle": "2.3.*#dev",
"sonata-project/admin-bundle": "2.4.*#dev",
"sonata-project/doctrine-orm-admin-bundle" : "*",
"sonata-project/easy-extends-bundle": "2.1.*#dev",
"sonata-project/user-bundle": "~2.2",
"knplabs/knp-menu-bundle": "~1.1",
"hwi/oauth-bundle": "~0.4#dev",
"sonata-project/cache-bundle": "~2.2"
If I use "php app/console sonata:easy-extends:generate -d src/ SonataUserBundle" - command generate namespace "Application", all my Bundles in namespace "WPE"...
I want user bundle in my namespace.
User entity renamed to "WPE\UserBundle\Entity\User", login, admin all work, but
users list in sonata send error "Class Application\Sonata\UserBundle\Entity\User does not exist"
I find (Find in path... in IDE PhpStorm) "Application\Sonata\UserBundle" in cache files and sonata documentation.
How I can use my namespace "WPE"?
Rename Application\Sonata\UserBundle to your YourVendor\UserBundle\Entity\User.
Rename Application\Sonata\UserBundle... namespace to YourVendor\UserBundle\... in all files.
Remove new Application\Sonata\UserBundle\ApplicationSonataUserBundle() from Appkernel.php and add YourVendor\UserBundle() in AppKernel.php.
Change config:
sonata_user:
#security_acl: true # Uncomment for ACL support
manager_type: orm # can be orm or mongodb
class:
user: YourVendor\UserBundle\Entity\User
group: YourVendor\UserBundle\Entity\Group
fos_user:
db_driver: orm # can be orm or odm
firewall_name: main
user_class: YourVendor\UserBundle\Entity\User
#user_class: Application\Sonata\UserBundle\Entity\User
group:
group_class: YourVendor\UserBundle\Entity\Group
#group_class: Application\Sonata\UserBundle\Entity\Group
group_manager: sonata.user.orm.group_manager
service:
user_manager: sonata.user.orm.user_manager
If your are using orm mapping:
mappings:
YourVendorUserBundle: ~
#ApplicationSonataUserBundle: ~
A PRAGMATIC WAY TO SOLVE THIS ISSUE
The easiest way to solve this problem is to use global namespace inside your VB, the global namespace is the only namespace allowed Application\YourBundle\Entity.
So, inside your mapping definition or inside your VB code, you will use one final namespace: problem solved. How to achieve this:
Declare only SuperClass inside a VB, don’t use final entity,
Call your entity BaseXXXX and make it abstract, change the properties from private to protected,
The same goes for a repository,
Always use Application\YourBundle\Entity\XXXX inside your code.
Of course, you need to create for each VB bundle:
a valid structure inside the Application directory,
a valid entity mapping definition,
a model inside the entity folder.
The last part is quite inefficient without an efficient tool to generate for you this structure: EasyExtendsBundle to the rescue.
HOW TO MAKE YOUR BUNDLE EASY EXTENDABLE?
Mainly all you need is to follow instructions in previous paragraph:
Declare you entity/repository as described above,
Use your entity/repository as described above,
Before generation you also need “skeleton” file that will describe AB entity. Skeleton file can either xml or yml. For fully working example see SonataMediaBundle.
At last you can run:
php app/console sonata:easy-extends:generate YourVBBundleName
Note that the –dest option allows you to choose the target directory, such as src. Default destination is app/.
If you want to use :
php app/console sonata:easy-extends:generate YourVBBundleName
You can modify some SonataAdmin fiels , it's not the best way but that can help you to run at least the commande and generate your bundle:
1 - In Sonata\EasyExtendsBundle\Bundle\BundleMetadata (ligne 95) make change like this:
$this->extendedNamespace = sprintf('WPE\\%s\\%s', $this->vendor, $information[1]);
2 - In Sonata\EasyExtendsBundle\Command\GenerateCommand (ligne 67) :
'application_dir' => sprintf("%s/WPE", $dest)
3 - in Sonata\EasyExtendsBundle\Generator\BundleGenerator (ligne 70 ) :
$file = sprintf('%s/WPE%s.php', $bundleMetadata->getExtendedDirectory(), $bundleMetadata->getName());
4 - In sonata-project / Resources / skeleton / bundle / bundle.mustache :
class WPE{{ bundle }} extends Bundle
{
/.../
}
5 - In Sonata\UserBundle\DependencyInjection\SonataUserExtension (ligne 162/163):
$defaultConfig['class']['user'] = sprintf('WPE\\Sonata\\UserBundle\\%s\\User', $modelType);
$defaultConfig['class']['group'] = sprintf('WPE\\Sonata\\UserBundle\\%s\\Group', $modelType);
Then Run php app/console sonata:easy-extends:generate.
Other way you can copy the UserBundle under WPE and make your change :
In config.yml
Your name spaces
You entities
your service
and Your class admin

Categories