2017-11-25 14 views
0

私は私のプロジェクトでDoctrine2を使用していますが、私は次のエンティティを定義している:Doctrine2、多対多の関係=> SQLSTATE [42000]:構文エラーまたはアクセス違反

namespace Model; 

/** 
* @Entity() 
* @Table(name="author") 
**/ 
class Author { 

    /** 
    * @Id 
    * @GeneratedValue 
    * @Column(type="integer") 
    **/ 
    private $id; 

    /** @Column(type="string") **/ 
    private $firstName; 

    /** @Column(type="string") **/ 
    private $lastName; 

    /** @Column(type="string", nullable=true) **/ 
    private $titleBefore; 

    /** @Column(type="string", nullable=true) **/ 
    private $titleAfter; 

    /** @ManyToMany(targetEntity="Article", mappedBy="authors") **/ 
    private $articles; 

    public function __construct() { 
     $this->articles = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 

    public function getId() { 
     return $this->id; 
    } 

    public function getTitleBefore() { 
     return $this->titleBefore; 
    } 

    public function setTitleBefore($titleBefore) { 
     $this->titleBefore = $titleBefore; 
    } 

    public function getTitleAfter() { 
     return $this->titleAfter; 
    } 

    public function setTitleAfter($titleAfter) { 
     $this->titleAfter = $titleAfter; 
    } 

    public function getLastName() { 
     return $this->lastName; 
    } 

    public function setLastName($lastName) { 
     $this->lastName = $lastName; 
    } 

    public function getFirstName() { 
     return $this->firstName; 
    } 

    public function setFirstName($firstName) { 
     $this->firstName = $firstName; 
    } 

    public function getArticles() { 
     return $this->articles; 
    } 

    public function addArticle($article) { 
     $this->articles->add($article); 
    } 
} 

namespace Model; 

/** 
* @Entity() 
* @Table(name="article") 
**/ 
class Article { 

    /** 
    * @Id 
    * @GeneratedValue 
    * @Column(type="integer") 
    **/ 
    private $id; 

    /** @Column(type="string") **/ 
    private $name; 

    /** @OneToOne(targetEntity="Publication") **/ 
    private $publication; 

    /** 
    * @ManyToMany(targetEntity="Author", mappedBy="articles") 
    */ 
    private $authors; 

    public function __construct() { 
     $this->authors = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 

    public function getId() { 
     return $this->id; 
    } 

    public function getName() { 
     return $this->name; 
    } 

    public function setName($name) { 
     $this->name = $name; 
    } 

    public function getPublication() { 
     return $this->publication; 
    } 

    public function setPublication($publication) { 
     $this->publication = $publication; 
    } 

    public function getAuthors() { 
     return $this->authors; 
    } 

    public function addAuthor($author) { 
     $this->authors->add($author); 
     $author->addArticle($this); 
    } 

    public function setAuthors($authors) { 
     $this->authors = $authors; 
    } 
} 

に見えますそのような関係の著者< - >記事はうまく動作します。私は問題に遭遇しましたが。私はこのようなSmartyテンプレートで著者アクセスもしようとすると:{foreach from=$article->getAuthors() item=author}を、次の例外がスローされます。

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ON' at line 1 in /code/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:104 Stack trace: 
#0 /code/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php(104): PDO->query('SELECT t0.id AS...') 
#1 /code/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(852): Doctrine\DBAL\Driver\PDOConnection->query('SELECT t0.id AS...') 
#2 /code/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php(1030): Doctrine\DBAL\Connection->executeQuery('SELECT t0.id AS...', Array, Array) 
#3 /code/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php(954): Doctrine\ORM\Persisters\Entity\BasicEntityPersister->getManyToManyStatement(Array, Object(Model\Article)) 
#4 /code/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(2839): Doctrine\ORM\Persiste in /code/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php on line 90 

私はすでに何が悪かったのかを見つけるためにしようと、この日を過ごしました。私は予約されたMySQLの単語を使うかもしれないと疑っていましたが、私の変数には何も見つかりませんでした。

例外が見つかるまで、最終的に完全なクエリログを取得することができました。最も興味深いクエリが存在しないようです。

mysqld, Version: 5.7.20 (MySQL Community Server (GPL)). started with: 
Tcp port: 3306 Unix socket: /var/run/mysqld/mysqld.sock 
Time     Id Command Argument 
2017-11-25T23:46:52.327597Z  10 Connect [email protected]_php_1.articlerepository_default on article_repository using TCP/IP 
2017-11-25T23:46:52.334083Z  10 Query  SELECT t0.id AS id_1, t0.firstName AS firstName_2, t0.lastName AS lastName_3, t0.titleBefore AS titleBefore_4, t0.titleAfter AS titleAfter_5 FROM author t0 
2017-11-25T23:46:52.342077Z  10 Query  SELECT t0.id AS id_1, t0.name AS name_2, t0.publication_id AS publication_id_3 FROM article t0 
2017-11-25T23:46:52.348058Z  10 Quit 
+0

実行されている実際のクエリを共有できますか? –

+0

その1つのクエリにどのようにアクセスできますか分かりません。私はhttps://pastebin.com/bBQLpENVでデバッグロギングを有効にしました。この例外の直前に実行されたクエリは2つのみです。 'SELECT t0.id AS id_1、t0.firstName AS firstName_2、t0.lastName AS lastName_3、t0.titleBefore AS titleBefore_4、t0.titleAfter AS titleAfter_5 FROM作成者t0'および ' SELECT t0.id AS id_1、t0.name AS名前2、t0.publication_id AS publication_id_3記事t0' –

答えて

0

私はManyToMany関係に不一致があります。ここで私はこの問題を解決するために取った手順は次のとおりです。それはエラーなしで通過したまで、私は、bin/doctrine orm:validate数回の実行にbin/doctrine orm:schema-tool:drop --force

  • 検証済みの、現在のエンティティを実行して、現在のスキーマを落とし、事業体の変更を行っ
    1. 生成新しいスキーマ:

      class Author { 
          /** 
          * @ManyToMany(targetEntity="Article", inversedBy="authors") 
          * @JoinTable(name="authors_articles") 
          **/ 
          private $articles; 
      } 
      
      class Article { 
          /** 
          * @ManyToMany(targetEntity="Author", mappedBy="articles") 
          */ 
          private $authors; 
      } 
      
      bin/doctrine orm:schema-tool:update --force --dump-sql

    正しい関係は次のようになります

  • 関連する問題