我可以从实体的类方法中获取EntityManager吗?

Is there an easy way to get Doctrine's entity manager from within an entity's class method?

<?php

/** @Entity */
class MyEntity {
    /** @Id @GeneratedValue @Column(type="integer") */
    protected $id;

   [...]

   public function someFunction() {
     // Is there any way to get Doctrine's EntityManager in here?
   }
}

You're really not supposed to. The idea behind a datamapper ORM like Doctrine is that your entities are just plain-old objects that know nothing about the persistence layer. If you find yourself wanting an EntityManager inside your entity, that's a signal that you ought to be creating a service class of some sort.

That said, Doctrine is quite flexible. For example, if you were so inclined, you could use Doctrine as the foundation for an ActiveRecord-style ORM.

However, outside of very specific use-cases, I wouldn't recommend it.