0

我在使用以下命令生成表时遇到问题:

php symfony doctrine:insert-sql

它返回我以下错误:

>> doctrine  creating tables


  Class "Complaint" must be a child class of Doctrine_Record


PHP Fatal error:  Call to a member function evictAll() on a non-object in D:\Internet\PHP\ijzerleeuw_bezoekrapporten\lib\vendor\symfony\lib\plugins\sfDoctri
nePlugin\lib\vendor\doctrine\Doctrine\Connection.php on line 1239
PHP Stack trace:
PHP   1. {main}() D:\Internet\PHP\ijzerleeuw_bezoekrapporten\symfony:0
PHP   2. include() D:\Internet\PHP\ijzerleeuw_bezoekrapporten\symfony:14
PHP   3. sfDatabaseManager->shutdown() D:\Internet\PHP\ijzerleeuw_bezoekrapporten\lib\vendor\symfony\lib\database\sfDatabaseManager.class.php:0
PHP   4. sfDoctrineDatabase->shutdown() D:\Internet\PHP\ijzerleeuw_bezoekrapporten\lib\vendor\symfony\lib\database\sfDatabaseManager.class.php:134
PHP   5. Doctrine_Manager->closeConnection() D:\Internet\PHP\ijzerleeuw_bezoekrapporten\lib\vendor\symfony\lib\plugins\sfDoctrinePlugin\lib\database\sfDoctr
ineDatabase.class.php:165
PHP   6. Doctrine_Connection->close() D:\Internet\PHP\ijzerleeuw_bezoekrapporten\lib\vendor\symfony\lib\plugins\sfDoctrinePlugin\lib\vendor\doctrine\Doctrin
e\Manager.php:580
PHP   7. Doctrine_Connection->clear() D:\Internet\PHP\ijzerleeuw_bezoekrapporten\lib\vendor\symfony\lib\plugins\sfDoctrinePlugin\lib\vendor\doctrine\Doctrin
e\Connection.php:1268

这是我用来生成模型的 schema.yml(php symfony 学说:build-mdeol):

InspectionReport:
  actAs: { Timestampable: ~ }
  columns:
    customer_id:    { type: integer(5), notnull: true }
    username:       { type: string(255), notnull: true }
    start_time:     { type: string(5) }
    end_time:       { type: string(5) }
    type:           { type: string(5) }
    reason:         { type: string(4) }
    contact:        { type: string(5) }
    subject:        { type: string(5) }
    remarks:        { type: string(5000) }
    emailaddresses: { type: string(5) }
  relations:
    Complaints:
      class: Complaint
      refClass: ComplaintInspectionReport
      local: id
      foreign: complaint_id
    Reviews:
      class: Review
      refClass: ReviewInspectionReport
      local: id
      foreign: review_id

ComplaintInspectionReport:
  columns:
    complaint_id:  { type: integer(4), primary: true }
    inspection_report_id: { type: integer, primary: true }
  relations:
    Complaint:  { onDelete: CASCADE, local: complaint_id, foreign: id }
    InspectionReport: { onDelete: CASCADE, local: inspection_report_id, foreign: id }

ReviewInspectionReport:
  columns:
    review_id:  { type: integer, primary: true }
    inspection_report_id: { type: integer, primary: true }
  relations:
    Review:  { onDelete: CASCADE, local: review_id, foreign: id }
    InspectionReport: { onDelete: CASCADE, local: inspection_report_id, foreign: id }

Complaint:
  columns:
    id:            { type: integer, notnull: true }
    description:   { type: string(32), notnull: true }
    contact:       { type: string(32) }
    remark:        { type: string(50) }

Review:
  columns:
    id:            { type: integer, notnull: true }
    description:   { type: string(32), notnull: true }
    contact:       { type: string(32) }
    remark:        { type: string(50) }
    grade:         { type: string(1) }
    email_address: { type: string(32) }

还有我从模型/模式生成的 schema.sql(php symfony 学说:build-sql):

CREATE TABLE complaint (id BIGINT AUTO_INCREMENT, description VARCHAR(32) NOT NULL, contact VARCHAR(32), remark VARCHAR(50), PRIMARY KEY(id)) ENGINE = INNODB;
CREATE TABLE complaint_inspection_report (complaint_id INT, inspection_report_id BIGINT, PRIMARY KEY(complaint_id, inspection_report_id)) ENGINE = INNODB;
CREATE TABLE inspection_report (id BIGINT AUTO_INCREMENT, customer_id BIGINT NOT NULL, username VARCHAR(255) NOT NULL, start_time VARCHAR(5), end_time VARCHAR(5), type VARCHAR(5), reason VARCHAR(4), contact VARCHAR(5), subject VARCHAR(5), remarks TEXT, emailaddresses VARCHAR(5), created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, PRIMARY KEY(id)) ENGINE = INNODB;
CREATE TABLE review (id BIGINT AUTO_INCREMENT, description VARCHAR(32) NOT NULL, contact VARCHAR(32), remark VARCHAR(50), grade VARCHAR(1), email_address VARCHAR(32), PRIMARY KEY(id)) ENGINE = INNODB;
CREATE TABLE review_inspection_report (review_id BIGINT, inspection_report_id BIGINT, PRIMARY KEY(review_id, inspection_report_id)) ENGINE = INNODB;
ALTER TABLE complaint_inspection_report ADD CONSTRAINT complaint_inspection_report_complaint_id_complaint_id FOREIGN KEY (complaint_id) REFERENCES complaint(id) ON DELETE CASCADE;
ALTER TABLE complaint_inspection_report ADD CONSTRAINT ciii FOREIGN KEY (inspection_report_id) REFERENCES inspection_report(id) ON DELETE CASCADE;
ALTER TABLE review_inspection_report ADD CONSTRAINT riii FOREIGN KEY (inspection_report_id) REFERENCES inspection_report(id) ON DELETE CASCADE;
ALTER TABLE review_inspection_report ADD CONSTRAINT review_inspection_report_review_id_review_id FOREIGN KEY (review_id) REFERENCES review(id) ON DELETE CASCADE;

如果我手动运行 SQL,它可以正常工作,但是当使用学说:insert-sql 时,它会返回错误。

我在想我的架构中有一个错误,但似乎无法掌握它。或者也许是完全不同的东西。

4

1 回答 1

1

我错了,我只是生成了类,并没有尝试加载 sql。

事实上,你有几个错误:

  • 你不应该为一个表定义一个列id,只有当你不需要它的主键,而不是递增等..否则它会给Doctrine带来麻烦
  • 对于每个中间表(ComplaintInspectionReport& ReviewInspectionReport),您不必重新定义relations. 如果它们在父类中正确定义,则此处不需要它们
  • 注意integer类型。Doctrine 将始终使用integer,但如果您定义一个integer(4)导致由 Doctrine 自动生成的 id 的外部变量,则插入 sql 时关系将失败。因为一方面你有integer,另一方面integer(4)
  • 最后,请注意您localInspectionReport. 您使用id了两次,但您必须为中间表定义本地键,而不是当前表。所以你必须同时设置inspection_report_id

这是固定模式:

InspectionReport:
  actAs: { Timestampable: ~ }
  columns:
    customer_id:    { type: integer(5), notnull: true }
    username:       { type: string(255), notnull: true }
    start_time:     { type: string(5) }
    end_time:       { type: string(5) }
    type:           { type: string(5) }
    reason:         { type: string(4) }
    contact:        { type: string(5) }
    subject:        { type: string(5) }
    remarks:        { type: string(5000) }
    emailaddresses: { type: string(5) }
  relations:
    Complaints:
      class: Complaint
      refClass: ComplaintInspectionReport
      local: inspection_report_id
      foreign: complaint_id
    Reviews:
      class: Review
      refClass: ReviewInspectionReport
      local: inspection_report_id
      foreign: review_id

ComplaintInspectionReport:
  columns:
    complaint_id:  { type: integer, primary: true }
    inspection_report_id: { type: integer, primary: true }

ReviewInspectionReport:
  columns:
    review_id:  { type: integer, primary: true }
    inspection_report_id: { type: integer, primary: true }

Complaint:
  columns:
    description:   { type: string(32), notnull: true }
    contact:       { type: string(32) }
    remark:        { type: string(50) }

Review:
  columns:
    description:   { type: string(32), notnull: true }
    contact:       { type: string(32) }
    remark:        { type: string(50) }
    grade:         { type: string(1) }
    email_address: { type: string(32) }

在尝试重新插入 sql 之前不要忘记清除缓存。

于 2012-10-30T12:50:43.707 回答