开发者

doctrine:build --model and --sql ok but doctrine:insert-sql is not. Do you know why?

开发者 https://www.devze.com 2023-02-03 16:35 出处:网络
here is my schema.yml files: issues: actAs: { Timestampable: ~ } columns: issueId: { type: integer(4), notnull: true, primary: true, autoincrement: true }

here is my schema.yml files:


issues:
  actAs: { Timestampable: ~ }
  columns:
    issueId: { type: integer(4), notnull: true, primary: true, autoincrement: true }
    issueDateForPublish: { type: timestamp, notnull: true }
    issueNam开发者_开发百科e: { type: string(255), notnull: true }
    issueCoverArticleId: { type: integer(4), notnull: true }
  relations:
    articles:
      class: articles
      foreignAlias: article
      local: articleId
      foreign: issueId
      type: many

articles:
  actAs: { Timestampable: ~ }
  columns:
    articleId: { type: integer(4), notnull: true, primary: true, autoincrement: true }
    articleTitle: { type: string(), notnull: true }
    articleText: { type: string(), notnull: true }
    articleDataForPublish: { type: timestamp, notnull: true }
    articleIsDraft: { type: boolean, notnull: true, default: 1 }
    articleIsOnHold: { type: boolean, notnull: true, default: 0  }
    articleIsModerate: { type: boolean, notnull: true, default: 0 }
    articleIsApprove: { type: boolean, notnull: true, default: 0 }
  relations:
    articles:
      local: issueId
      foreign: articleId
      onDelete: cascade
    comments:
      class: comments
      foreignAlias: comment
      local: commentId
      foreign: articleId
      type: many

comments:
  actAs: { Timestampable: ~ }
  columns:
    commentId: { type: integer(4), notnull: true, primary: true, autoincrement: true }
    commentName: { type: string(255), notnull: true }
    commentSurname: { type: string(255), notnull: true }
    commentMail: { type: string(255), notnull: true }
    commentDataForPublish: { type: timestamp }
    commentIsModerated: { type: boolean, notnull: true, default: 0 }
    commentIsApprove: { type: boolean, notnull: true, default: 0 }
  relations:
    articles:
      local: articleId
      foreign: commentId
      onDelete: cascade

Again, when i run php symfony doctrine:build --model and php symfony doctrine:build --sql nothing goes bad. "php symfony doctrine:insert-sql" makes this error:


SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'issueid' doesn't exist in table. Failing Query: "CREATE TABLE articles (articleid INT AUTO_INCREMENT, articletitle TEXT NOT NULL, articletext TEXT NOT NULL, articledataforpublish DATETIME NOT NULL, articleisdraft TINYINT(1) DEFAULT '1' NOT NULL, articleisonhold TINYINT(1) DEFAULT '0' NOT NULL, articleismoderate TINYINT(1) DEFAULT '0' NOT NULL, articleisapprove TINYINT(1) DEFAULT '0' NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, INDEX issueid_idx (issueid), PRIMARY KEY(articleid)) ENGINE = INNODB". Failing Query: CREATE TABLE articles (articleid INT AUTO_INCREMENT, articletitle TEXT NOT NULL, articletext TEXT NOT NULL, articledataforpublish DATETIME NOT NULL, articleisdraft TINYINT(1) DEFAULT '1' NOT NULL, articleisonhold TINYINT(1) DEFAULT '0' NOT NULL, articleismoderate TINYINT(1) DEFAULT '0' NOT NULL, articleisapprove TINYINT(1) DEFAULT '0' NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, INDEX issueid_idx (issueid), PRIMARY KEY(articleid)) ENGINE = INNODB

Thanks for your help, erman.


You've got your definition a bit wrong.

Local should be the name of the field in the current table, and foreign the field in the foreign table - you have these the wrong way round.

You normally only define one end of the relationship - doctrine auto adds the inverse to the other table, and gets it right 99% of the time. If you do it manually they have to match. In this case, remove the article relation from issues, and the comments relation from articles.

Comments has no article id field.

Foreign alias isn't needed, but you have it the wrong way round - its what the current table/object will be referred to as in the table/object at the far end of the relation. Defaults to the name of the current object, so can often ignore.

Few other minor niggles too. At a guess, I reckon this schema will do you:

issue:
  actAs: { Timestampable: ~ }
  columns:
    issueId: { type: integer(4), notnull: true, primary: true, autoincrement: true }
    issueDateForPublish: { type: timestamp, notnull: true }
    issueName: { type: string(255), notnull: true }
    issueCoverArticleId: { type: integer(4), notnull: true }

article:
  actAs: { Timestampable: ~ }
  columns:
    articleId: { type: integer(4), notnull: true, primary: true, autoincrement: true }
    articleTitle: { type: string(), notnull: true }
    articleText: { type: string(), notnull: true }
    articleDataForPublish: { type: timestamp, notnull: true }
    articleIsDraft: { type: boolean, notnull: true, default: 1 }
    articleIsOnHold: { type: boolean, notnull: true, default: 0  }
    articleIsModerate: { type: boolean, notnull: true, default: 0 }
    articleIsApprove: { type: boolean, notnull: true, default: 0 }
    issueId: { type: integer(4) }
  relations:
    issue:
      local: issueId
      foreign: issueId
      foreignAlias: articles
      onDelete: cascade

comment:
  actAs: { Timestampable: ~ }
  columns:
    commentId: { type: integer(4), notnull: true, primary: true, autoincrement: true }
    commentName: { type: string(255), notnull: true }
    commentSurname: { type: string(255), notnull: true }
    commentMail: { type: string(255), notnull: true }
    commentDataForPublish: { type: timestamp }
    commentIsModerated: { type: boolean, notnull: true, default: 0 }
    commentIsApprove: { type: boolean, notnull: true, default: 0 }
    articleId: { type: integer(4) }
  relations:
    article:
      local: articleId
      foreign: articleId
      onDelete: cascade
      foreignAlias: comments
0

精彩评论

暂无评论...
验证码 换一张
取 消