0
votes

I have a problem generating my tables using the command:

php symfony doctrine:insert-sql

It returns me the following error:

>> 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

This is my schema.yml, which I used to generate the model (php symfony doctrine: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) }

And the schema.sql I generated from the model/schema (php symfony doctrine: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;

If I run the SQL manually it works fine, but when using doctrine:insert-sql it returns me errors.

I'm thinking of an error in my schema, but can't seem to get a hold of it. Or maybe it's something totally different.

1
Your schema is ok. Can we see model class for Complaint ? (lib/model/doctrine/Complaint.class.php & lib/model/doctrine/ComplaintTable.class.php)j0k

1 Answers

1
votes

I was wrong, I just generated classes and didn't try to load the sql.

In fact you have few errors:

  • you shouldn't define a column id for a table, only if you don't need it to the primary key, not incremented, etc .. otherwise it will cause trouble to Doctrine
  • for each intermidate table (ComplaintInspectionReport & ReviewInspectionReport) you don't have to redefine relations. If they are properly define in the parent class, you don't need them here
  • be careful about integer type. Doctrine will always use integer but if you define a foreign with integer(4) that leads to an id auto generated by Doctrine, the relation will fail when insert sql. Because on one side you have integer and on the other integer(4).
  • finally, be careful about your local key defined in InspectionReport. You use id twice but you have to define the local key for the intermediate table, not the current one. So you will have to set inspection_report_id for both.

Here is the fixed schema:

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) }

Do not forget to clear your cache before trying to re-insert sql.