Symfony 4 + Doctrine:将实体类归类至子文件夹
Hey there! As someone who’s been where you are—figuring out Symfony and Doctrine organization as a newbie—I totally get wanting to keep your code clean and separated. Let’s walk through exactly how to split your entities into src/Entity/Database (for Doctrine-mapped database entities) and src/Entity/Forms (for form-specific classes) in Symfony 4.
Step 1: Move Files & Update Namespaces
First, physically relocate your files:
- Drag all Doctrine-mapped database entities into
src/Entity/Database - Move form-related classes (like form DTOs or non-persisted form entities) into
src/Entity/Forms
Then update the namespace at the top of each file to match their new location:
- For database entities (e.g.,
User.php):namespace App\Entity\Database; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass="App\Repository\Database\UserRepository") */ class User { // ... your existing entity code } - For form classes (e.g.,
ContactFormData.php):namespace App\Entity\Forms; class ContactFormData { // ... your form properties and validation rules }
Don’t forget to fix any use statements in controllers, repositories, or form types that reference these classes!
Step 2: Update Doctrine’s Mapping Configuration
We need to tell Doctrine to only look for database entities in the Database subfolder. Open config/packages/doctrine.yaml and adjust the orm section like this:
doctrine: dbal: # ... keep your existing DBAL configuration (database url, etc.) orm: auto_generate_proxy_classes: true naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware auto_mapping: false # Disable default auto-mapping to define our own mappings: App: is_bundle: false type: annotation # Use xml/yaml if you don't use annotation mapping dir: '%kernel.project_dir%/src/Entity/Database' prefix: 'App\Entity\Database' alias: App
Key changes here:
auto_mapping: falseturns off the default behavior of scanning the entireEntityfolderdirpoints directly to yourDatabasesubfolderprefixmatches the new namespace of your database entities
Step 3: Use Form Classes Normally
Your form-related classes in src/Entity/Forms don’t need any special configuration—Symfony will automatically resolve them as long as their namespaces are correct. When building forms, just reference them with their full namespace:
// src/Form/ContactFormType.php namespace App\Form; use App\Entity\Forms\ContactFormData; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; class ContactFormType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('name') ->add('email') ->add('message'); } public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'data_class' => ContactFormData::class, ]); } }
Step 4: Clear Cache & Verify
Finally, clear your Symfony cache to apply all configuration changes:
php bin/console cache:clear
If you already have existing database migrations, regenerate them to reflect the new entity namespaces:
php bin/console make:migration php bin/console doctrine:migrations:migrate
That’s all! Your database entities will still work with Doctrine as before, and your form classes will be neatly organized without cluttering your core entity folder.
内容的提问来源于stack exchange,提问作者J. Smith

