maker-bundle icon indicating copy to clipboard operation
maker-bundle copied to clipboard

make:entity ManyToOne breaks if entities use Property Hooks

Open tacman opened this issue 8 months ago • 6 comments

I'm getting an error with make:entity when an existing entity uses property hooks (note: Doctrine ORM 3.4 now supports them).

When I remove the property hook it works as expected.

        private(set) ?array $extra = null,
bin/console make:entity Image -v
Linting Generated Files With:
Bundled PHP-CS-Fixer & Bundled PHP-CS-Fixer Configuration

 Your entity already exists! So let's add some new fields!

 New property name (press <return> to stop adding fields):
 > product

 Field type (enter ? to see all types) [string]:
 > ManyToOne

 What class should this entity be related to?:
 > Product

 Is the Image.product property allowed to be null (nullable)? (yes/no) [yes]:
 > no 

 Do you want to add a new property to Product so that you can access/update Image objects from it - e.g. $product->getImages()? (yes/no) [yes]:
 > 

 A new property will also be added to the Product class so that you can access the related Image objects from it.

 New field name inside Product [images]:
 > 

 Do you want to activate orphanRemoval on your relationship?
 A Image is "orphaned" when it is removed from its related Product.
 e.g. $product->removeImage($image)
 
 NOTE: If a Image may *change* from one Product to another, answer "no".

 Do you want to automatically delete orphaned App\Entity\Image objects (orphanRemoval)? (yes/no) [no]:
 > yes


In ParserAbstract.php line 363:
                                                                                                
  [PhpParser\Error]                                                                             
  Syntax error, unexpected ')', expecting T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG on line 23  
                                                                                                

Exception trace:

The stack trace:

Exception trace:
  at /home/tac/sites/dummy/vendor/nikic/php-parser/lib/PhpParser/ParserAbstract.php:363
 PhpParser\ParserAbstract->doParse() at /home/tac/sites/dummy/vendor/nikic/php-parser/lib/PhpParser/ParserAbstract.php:187
 PhpParser\ParserAbstract->parse() at /home/tac/sites/dummy/vendor/symfony/maker-bundle/src/Util/ClassSourceManipulator.php:953
 Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator->setSourceCode() at /home/tac/sites/dummy/vendor/symfony/maker-bundle/src/Util/ClassSourceManipulator.php:76
 Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator->__construct() at /home/tac/sites/dummy/vendor/symfony/maker-bundle/src/Maker/MakeEntity.php:849
 Symfony\Bundle\MakerBundle\Maker\MakeEntity->createClassManipulator() at /home/tac/sites/dummy/vendor/symfony/maker-bundle/src/Maker/MakeEntity.php:254
 Symfony\Bundle\MakerBundle\Maker\MakeEntity->generate() at /home/tac/sites/dummy/vendor/symfony/maker-bundle/src/Command/MakerCommand.php:107
 Symfony\Bundle\MakerBundle\Command\MakerCommand->execute() at /home/tac/sites/dummy/vendor/symfony/console/Command/Command.php:318
 Symfony\Component\Console\Command\Command->run() at /home/tac/sites/dummy/vendor/symfony/console/Application.php:1092
 Symfony\Component\Console\Application->doRunCommand() at /home/tac/sites/dummy/vendor/symfony/framework-bundle/Console/Application.php:123
 Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand() at /home/tac/sites/dummy/vendor/symfony/console/Application.php:341
 Symfony\Component\Console\Application->doRun() at /home/tac/sites/dummy/vendor/symfony/framework-bundle/Console/Application.php:77
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /home/tac/sites/dummy/vendor/symfony/console/Application.php:192
 Symfony\Component\Console\Application->run() at /home/tac/sites/dummy/vendor/symfony/runtime/Runner/Symfony/ConsoleApplicationRunner.php:49
 Symfony\Component\Runtime\Runner\Symfony\ConsoleApplicationRunner->run() at /home/tac/sites/dummy/vendor/autoload_runtime.php:29
 require_once() at /home/tac/sites/dummy/bin/console:11

make:entity [-a|--api-resource] [-b|--broadcast] [--regenerate] [--overwrite] [--with-uuid] [--with-ulid] [--] []

tac@system76-pc:~/sites/dummy$ c make:entity Image -v

Image

tacman avatar Jun 24 '25 11:06 tacman

Hi ! Got the same issue. I thought it was on nikic/PHP-Parser's side, but the issue is on MakerBundle's side (https://github.com/nikic/PHP-Parser/issues/1095).

I guess the parser used should be changed to use instead PHP8. Also, the PHP version is not the right one neither. I use PHP 8.4 and it's hardcoded "8.1".

Image

kira0269 avatar Jul 20 '25 13:07 kira0269

Symfony 8 will require PHP 8.4. In the meantime, I'm not sure how easy it is to support earlier versions at the same time.

I've had the idea of re-writing this to only support 8.4 and getting rid of the boilerplate getters/setters.

tacman avatar Jul 20 '25 14:07 tacman

Maybe instead of 8.1, it can detect the user's version of PHP? Or add a version string to the maker-bundle config?

tacman avatar Jul 20 '25 14:07 tacman

I think it's weird to configure the lexer with PHP 8.1 and the parser with PHP 7.

Shouldn't be better to:

  • Detect PHP version based on the composer.json.
  • Use the lexer and the parser of the correct PHP version

kira0269 avatar Jul 20 '25 14:07 kira0269

Same here, I'm having issues with multiple entities. It breaks even on $id

giosal avatar Sep 16 '25 08:09 giosal

same issues

langziyang avatar Oct 24 '25 07:10 langziyang

Same here. Since, to save almost two thirds of code in my entities, I have completely switched to CPP and asymmetric visibility for properties (e.g. private(set) for id), it renders the maker bundle unusable for me when it comes to entities.

martattacks avatar Jan 15 '26 13:01 martattacks