douyouchou1085 2015-07-14 13:17
浏览 114

$ em-> find(..)和$ em-> getRepository(..) - > find(..)之间的区别

There is Entity

/**
 * @ORM\Entity(repositoryClass="Some\Namspace\CustomRepository")
 * @ORM\Table(name="image_type")
 */
class MyEntity{...}

and CustomRepository extends EntityRepository to override some methods like find or findAll

documentation says:

// $em instanceof EntityManager
$user = $em->find('MyProject\Domain\User', $id);

Essentially, EntityManager#find() is just a shortcut for the following:

$user = $em->getRepository('MyProject\Domain\User')->find($id);

link:doctrine-orm.readthedocs.org

but my CustomRepository works only with $em->getRepository('Entities\MyEntity')->find($id)

using $em->find('Entities\MyEntity',$id); ignoring my overrided methods in CustomRepository


  • so is this a bug?
  • or there's a difference between this construcions?
  • how can i overide find,finAll,... methods for my entity without overriding EntityManager?

Edit (1)

using composer:

"require": {
    "doctrine/orm": "~2.4"
},

find code:

public function find($entityName, $id, $lockMode = null, $lockVersion = null)
{
    $class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\'));

    if ( ! is_array($id)) {
        if ($class->isIdentifierComposite) {
            throw ORMInvalidArgumentException::invalidCompositeIdentifier();
        }

        $id = array($class->identifier[0] => $id);
    }
    ........... other ~100 lines
}
  • 写回答

2条回答 默认 最新

  • duannuan0074 2015-07-14 13:25
    关注

    In doctrine 2.2 it doesn't seem to be any different - literally just a pass-through/shortcut for getRepository()->find()

    http://www.doctrine-project.org/api/orm/2.2/source-class-Doctrine.ORM.EntityManager.html#348

    public function find($entityName, $identifier, $lockMode = LockMode::NONE, $lockVersion = null)
    {
        return $this->getRepository($entityName)->find($identifier, $lockMode, $lockVersion);
    }
    

    Thoughts:

    • What version of doctrine?
    • Any chance you have a customized EntityManager?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大