2

我正在使用遗留数据库在 Symfony2 中开发一个项目,该项目使用一个字符串来生成一个 6 位数字“franchisee_number”,前面带有浮动 0 - 例如。000123. 当我尝试刷新实体时,我收到一个错误,显示它尝试执行的 SQL 语句,它甚至没有尝试插入以插入我的主键字段。在刷新实体之前,“franchisee_id”出现在实体中,所以我假设 Doctrine 存在问题,不想设置主键,而是生成它。

我的问题与这个问题非常相似:当 flush() 主键没有被插入时,但我已经尝试了列出的答案,但它们不起作用。

这是我的实体中“franchisee_number”字段的代码:

/**
 * @var string
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="NONE")
 * @ORM\Column(name="franchisee_number", type="string", length=255, nullable=false)
 *
 */
private $franchiseeNumber;

我认为这@ORM\GeneratedValue(strategy="NONE")会告诉学说我将自己的值插入到该字段中,而不是试图生成它。

还有我的控制器代码

    public function createAction(Request $request)
{
    $this->denyAccessUnlessGranted('ROLE_ADMIN');
    $entity = new Accounts();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);
    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager('inertia');
        $em->persist($entity);
        $em->flush();
        return $this->redirect($this->generateUrl('accounts_show', array('id' => $entity->getId())));
    }

    return $this->render('InertiaBundle:Accounts:new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    ));
}

我还确保 Bundle/Resources/config/doctrine 目录中没有 xml 配置文件。

在此先感谢您的帮助!

4

2 回答 2

0

我浪费了很多时间试图弄清楚这一点,并且无法真正找到一个很好的答案。我只是决定以艰难的方式去做,并用 Doctrine DBAL 编写了一个自定义查询,灵感来自这个问题/答案:https ://stackoverflow.com/a/15650290/1647183

它对我来说就像一个魅力!

于 2016-05-11T21:07:47.560 回答
0

如果使用,则必须在持久/刷新实体之前strategy="NONE"在或其他系统中创建标识符。__construct

例子:

class MyEntity
{
    /**
     * @var string
     *
     * @ORM\Id
     * @ORM\Column(type="string", name="id")
     * @ORM\GeneratedValue(strategy="NONE")
     */
    private $id;

    public function __construct()
    {
        $this->id = 123; // Inject you custom ID here!
    }
}
于 2016-05-12T10:07:47.690 回答