php - Doctrine Repository getEntityManager() 确实返回空

标签 php symfony doctrine-orm

我有一个正在使用 Symfony2 编码的应用程序。我创建了一个 Account 实体并使用注解创建了一个名为 AccountRepository 的存储库 在 AccountRepository 对象内 我创建了一个函数来运行一些业务逻辑,该业务逻辑发送给外部供应商并在他们的站点创建一个用户,然后返回信息给我们,这样我就可以将他们的用户与我们的帐户实体相关联。创建该用户的一部分包括发送信用卡 token 。然后他们返回一些我想存储并与我们的帐户关联的有限信用卡数据,因此在创建对象并插入适当的数据后我有一个实体 AccountCard,我无法从存储库请求实体管理器并坚持 AccountCard 做$em 变量上的 print_r 什么也没显示,但我读过的所有内容都告诉我我应该能够做到这一点。我做错了什么?

<?php

namespace Openbridge\CommonBundle\Entity\Repository;

use Doctrine\ORM\EntityRepository;

use Openbridge\CommonBundle\Entity\Account as Account;
use Openbridge\CommonBundle\Entity\AccountCard as AccountCard;
use Openbridge\CommonBundle\Entity\AccountAddress as AccountAddress;

/**
 * AccountRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class AccountRepository extends EntityRepository
{
    public function __construct()
    {

    }

    function createVendorUser(Account $account, $userData = array())
    {
        // Request new user from vendor code here. (this works)

        $ac = new AccountCard();
        // setters for $ac data here.

        // Get entity manager
        $em = $this->getEntityManager();
        $em->persist($ac);
        $em->flush();
    }

最佳答案

你必须删除构造函数,因为EntityRepository使用它来将依赖项绑定(bind)到类。

关于php - Doctrine Repository getEntityManager() 确实返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18067599/

相关文章:

php - Affected_rows 返回负数 -1 但插入

Symfony2 在包和 Controller 之间传递数据

php - XXX 不是有效的实体或映射的父类(super class)//和配置选项

doctrine-orm - Symfony2 形成 : How do I persist an entity with a nullable association?

php - 简单的 MySQL 查询 16 秒长

php - proc_open : Extending file descriptor numbers to enable "status" feedback from a Perl script

php - Laravel 中使用 sortby 时无法使用分页

symfony - 在 Symfony2 中组织业务逻辑

symfony - elasticsearch距离排序和计算不正确和不准确

php - Symfony onFlush 学说监听器