There’s a solution via RFE for this in RFE; solve problem with bin commands and php versions
I’m dealing with a new app in symfony 5.4 and php 7.4 to test the new additions and changes in symfony 6. I’ve used the entity maker from the console to create the entity and the crud, and the db was created perfectly. However, the generator uses the new "attributes" (according to the convention in https://symfony.com/doc/5.4/routing.html) instead of the "classic" annotations. Debugging via console to see the resulting paths, none of the routes defined in the controllers is shown (of course, a 404 error was shown when accessing the url in dev mode). I decided to replace the attributes with classic annotations, and the paths are shown and the 404 error is gone. But now, I find that the logic the generator uses is via the repository to use the Entity Manager, and when accessing to the index to start from scratch, I get:
Could not find the entity manager for class "AppEntityRoom". Check your Doctrine configuration to make sure it is configured to load this entity’s metadata.
The portion of code shown in the debugger is this:
class RoomRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Room::class); // Here is the error
}
And the entity starts with this:
namespace AppEntity;
use AppRepositoryRoomRepository;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
#[ORMEntity(repositoryClass: RoomRepository::class)]
class Room
{
#[ORMId]
#[ORMGeneratedValue]
#[ORMColumn()]
private ?int $id = null;
...
My biggest concern is that I guess I can’t rewrite the full crud reverting to annotations, which is a lot of work (just what I wanted to avoid by using the generator), so there must be something about the attributes that I’m missing. Here’s a controller whose crud I haven’t modified yet, so anyone can take a look and find out why the router can’t find the defined paths with this kind of annotation.
namespace AppController;
use AppEntityRoomFeature;
use AppFormRoomFeatureType;
use AppRepositoryRoomFeatureRepository;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
#[Route('/admin/feature')]
class RoomFeatureController extends AbstractController
{
#[Route('/', name: 'admin_room_feature_index', methods: ['GET'])]
public function index(RoomFeatureRepository $roomFeatureRepository): Response
{
return $this->render('room_feature/index.html.twig', [
'room_features' => $roomFeatureRepository->findAll(),
]);
}
...
What is the problem with all this? Thanks in advance.
3
Answers
As stated in the first comment, forcing every bin/console (for make: commnds) and composer commands to be run especifically by prepending php7.4 to the command, everything works but with "classic" annotations, I've found no way to use attributes in controllers with php7.4. The .php-version file seems to be used only by the symfony-cli to launch the dev webserver. I hope this helps anyone who can face this scenario in the future.
I had a similar situation and on my case it was related to an incorrect namespace on a Trait (under src/Entity/Traits).
The trait wasn’t even being used but apparently it will still cause this error.
Regarding your second comment:
This assumption is wrong, since symfony 5.4 supports php 7.2.5 and up. Although 7.2 is sunsetted officially, there are plenty of installations out there or with extended support from distros so that’s besides the point: Symfony will happilly run in 7.2 or 7.3.
In any case, it’s maker bundle who determines whether or not to use attributes based on the php version that is used to execute the maker commands, not the available installed versions. You can force it to use specific version features by executing
composer config platform.php 7.4.x
.And be aware that maker bundle 1.44 has dropped annotation support altogether, so pin your version to an older one.