【问题标题】:Symfony manyToOne on flush errorSymfony manyToOne 刷新错误
【发布时间】:2017-12-03 12:30:09
【问题描述】:

我在使用 CaseCollectionsCaseMap 创建 SingleCase 时遇到问题。

这是我的数据库结构:

CREATE TABLE cases (
    ca_id serial NOT NULL,
    ca_parent_id INTEGER,
    ca_urlcode VARCHAR(255) NOT NULL,
    ca_defaultcollection_id INTEGER,
    ca_active SMALLINT NOT NULL DEFAULT 1,
    ca_author_id INTEGER,
    ca_author_name VARCHAR(255) NOT NULL,
    ca_modified timestamp without time zone NOT NULL DEFAULT now(),
    ca_created timestamp without time zone NOT NULL DEFAULT now(),
    CONSTRAINT cases_pk PRIMARY KEY (ca_id),
    CONSTRAINT cases_ca_parent_id FOREIGN KEY (ca_parent_id)
        REFERENCES cases (ca_id) MATCH SIMPLE
        ON UPDATE NO ACTION ON DELETE SET NULL
)
WITH (
  OIDS=FALSE
);

CREATE TABLE case_collections (
    cc_id serial NOT NULL,
    cc_parent_id INTEGER,
    cc_urlcode VARCHAR(255) NOT NULL,
    cc_active smallint NOT NULL DEFAULT 1,
    cc_priority INTEGER DEFAULT 0 NOT NULL,
    cc_author_id INTEGER,
    cc_author_name VARCHAR(255) NOT NULL,
    cc_modified timestamp without time zone NOT NULL DEFAULT now(),
    cc_created timestamp without time zone NOT NULL DEFAULT now(),
    CONSTRAINT case_collections_pk PRIMARY KEY (cc_id),
    CONSTRAINT case_collections_cc_parent_id FOREIGN KEY (cc_parent_id)
        REFERENCES case_collections (cc_id) MATCH SIMPLE
        ON UPDATE NO ACTION ON DELETE SET NULL
)
WITH (
  OIDS=FALSE
);

CREATE TABLE case_collections_cases (
    cc_id INTEGER NOT NULL,
    ca_id INTEGER NOT NULL,
    ccca_priority INTEGER NOT NULL,
    CONSTRAINT case_collections_cases_pk PRIMARY KEY (cc_id, ca_id),
    CONSTRAINT case_collections_cases_cc_id_fk FOREIGN KEY (cc_id)
        REFERENCES case_collections (cc_id) MATCH SIMPLE
        ON UPDATE NO ACTION ON DELETE CASCADE,
    CONSTRAINT case_collections_cases_ca_id_fk FOREIGN KEY (ca_id)
        REFERENCES cases (ca_id) MATCH SIMPLE
        ON UPDATE NO ACTION ON DELETE CASCADE
)
WITH (
  OIDS=FALSE
);

这是我的 Doctrine ORM 文件:

PPK\Domain\Entity\SingleCase\SingleCase:
  type: entity
  repositoryClass: PPK\Infrastructure\Repository\SingleCase\DoctrineSingleCaseRepository
  table: cases

  id:
    caId:
      type: integer
      column: ca_id
      generator:
        strategy: SEQUENCE
      sequence-generator:
        allocationSize: 1
        initialValue: 1
        sequenceName: cases_case_id_seq

  fields:
    caUrlcode:
      type: string
      length: 255
      column: ca_urlcode
    caActive:
      type: smallint
      column: ca_active
    caAuthorId:
      type: integer
      column: ca_author_id
    caAuthorName:
      type: string
      column: ca_author_name
    caModified:
      type: datetime
      column: ca_modified
    caCreated:
      type: datetime
      column: ca_created

  manyToOne:
    defaultCollection:
      targetEntity: PPK\Domain\Entity\SingleCase\CaseCollectionsCaseMap
      joinColumns:
        ca_defaultcollection_id:
          referencedColumnName: cc_id
        ca_id:
          referencedColumnName: ca_id

PPK\Domain\Entity\SingleCase\CaseCollectionsCaseMap:
  type: entity
  repositoryClass: PPK\Infrastructure\Repository\SingleCase\DoctrineCaseCollectionsCaseMapMapRepository
  table: case_collections_cases

  id:
    collection:
      type: integer
      column: cc_id
      associationKey: true
    singleCase:
      type: integer
      column: ca_id
      associationKey: true

  fields:
    cccaPriority:
      type: integer
      column: ccca_priority

  manyToOne:
    singleCase:
      targetEntity: PPK\Domain\Entity\SingleCase\SingleCase
      joinColumns:
          ca_id:
              referencedColumnName: ca_id
      orphanRemoval: false

    collection:
      targetEntity: PPK\Domain\Entity\CaseCollection\CaseCollection
      joinColumns:
          cc_id:
              referencedColumnName: cc_id
      orphanRemoval: false


PPK\Domain\Entity\CaseCollection\CaseCollection:
  type: entity
  repositoryClass: PPK\Infrastructure\Repository\CaseCollection\DoctrineCaseCollectionRepository
  table: case_collections

  id:
    ccId:
      type: integer
      column: cc_id
      generator: { strategy: AUTO }

  fields:
    ccUrlcode:
      type: string
      length: 255
      column: cc_urlcode
    ccActive:
      type: smallint
      column: cc_active
    ccPriority:
      type: integer
      column: cc_priority
    ccAuthorId:
      type: integer
      column: cc_author_id
    ccAuthorName:
      type: string
      column: cc_author_name
    ccModified:
      type: datetime
      column: cc_modified
    ccCreated:
      type: datetime
      column: cc_created

在我的 CommandHandler 中,我注入了带有 CaseCollectionsCase 存储库的 SingleCase 存储库。

当我只创建 SingleCase 时:

$singleCase = new SingleCase();
$singleCase->setCaActive($singleCaseDTO->getStatus());
$singleCase->setCaUrlcode($singleCaseDTO->getUrl());
$singleCase->setCaAuthorId($user->getUsrId());
$singleCase->setCaAuthorName($user->getUsrName());
$singleCase->setCaCreated();
$singleCase->setCaModified();
$this->singleCaseRepository->create($singleCase);

教义抛出错误:

SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "ca_id" violates not-null constraint
DETAIL: Failing row contains (null, null, /new-question, null, 0, 1, XX, 2017-12-03 13:22:18, 2017-12-03 13:22:18).

添加 CaseCollectionsCaseMap 仍然没有帮助:

$defaultCollection = new CaseCollectionsCaseMap();
$defaultCollection->setCollection($cc);
$defaultCollection->setCccaPriority(0);

$singleCase->setDefaultCollection($defaultCollection);
$this->singleCaseRepository->create($singleCase);

教义抛出:

A new entity was found through the relationship 'PPK\Domain\Entity\SingleCase\SingleCase#defaultCollection' that was not configured to cascade persist operations for entity

当我添加 cascade: ["persist", "merge"] 到 defaultCollection 学说时会抛出:

An exception occurred while executing 'INSERT INTO cases (ca_id, ca_urlcode, ca_active, ca_author_id, ca_author_name, ca_modified, ca_created, ca_parent_id, ca_defaultcollection_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)' with params [null, "\/new-question", 0, 1, "XX", "2017-12-03 13:26:26", "2017-12-03 13:26:26", null, null]

调用persist 会生成$caId,但刷新会使它归零。

任何帮助将不胜感激。

【问题讨论】:

    标签: php postgresql symfony doctrine-orm


    【解决方案1】:

    你用的是什么数据库?如果不是 MySQL,我建议您使用 "AUTO" 生成策略作为您的 id。要查看哪些 dbs 不支持 "AUTO" 策略,请参阅 http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#identifier-generation-strategies


    更新:抱歉弄错了。显然,如果您指定 AUTO,则学说仍然使用 SEQUENCE 策略,即使它说 postgres 不支持 SEQUENCE。我能够从这个问题https://github.com/doctrine/doctrine2/issues/6429 中弄清楚这一点。由此判断,您应该将 id strategy 显式设置为IDENTITY。很抱歉之前误导了您。

    【讨论】:

    • 我使用的是 PostgreSQL 9.6。将生成器更改为 AUTO 无效。
    • 抱歉弄错了。您需要使用IDENTITY。现在将更新我的答案
    • 还是没有效果。
    • 太糟糕了。你能告诉我最后一次架构更新或迁移的转储吗?
    • 我不使用架构更新和迁移。数据库结构已经创建,我只将 sql 映射到实体。 Clear SQL with tables 在我的第一篇文章中。
    猜你喜欢
    • 1970-01-01
    • 2017-01-19
    • 2016-02-28
    • 1970-01-01
    • 2015-10-05
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 2016-12-21
    相关资源
    最近更新 更多