简体   繁体   中英

Do I really need to move/write entities myself with Doctrine 2?

I write my entities in let's say models/ folder.

something like:

namespace Organisation\User;
/**
 @Entity
*/
class Customer {

/**
* @Column(type="integer") @GeneratedValue
*/
protected $_id;
}

}

So I'll instantiate my entity with $customer = new \\Organisation\\User\\Customer();

Ok, but if I use doctrine orm:generate-entities library/ it will generate it under the following directory :

library/Organisation/User/Customer.php

ANd that's ok, but if I look at the code, there aren't any of my annotation, and therefore when I try to use it, I get doctrine\\ORM\\Mapping\\MappingException: Class Organisation\\User\\Customer is not a valid entity or mapped super class. because there aren't any annotation.

So what I need to do is to remove the namespace, generate into the same directory as the entities with metadata inforations are, move to my library folder, and add the namespace to work with.

It looks ugly, do I have missed something?

edit : I forgot to tell that orm:generate-entities doesn't work recursively, then, I can't even use my actual structure within my entities metadata

If you've written your entity classes already, why are you trying to generate them?

They typical way to drive a new project is to write your annotated entity classes, and use orm:schema-tool:create to generate your database schema.

Most examples I've seen do stick the Entities under library/, in some nested directory based on the namespaced name of the class, just as you've described. This is generally a good thing, as it works with the default Doctrine2 autoloader setup.

If you're not trying to fit Doctrine2 onto an already existing database schema, I'd recommend that you simply stick all your entity classfiles someplace like library//Entity/.php, and use orm:schema-tool:update and orm:schematool:update to manage the database for you.

使用arg --generate-annotations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM