使用组合键的Doctrine请求崩溃

时间:2017-04-27 12:20:44

标签: symfony doctrine

当我请求一个带有引用外键的复合主键的表时,我遇到了Doctrine的一个大问题。 我有3个实体: 图书馆( idLibrary ,地址),图书( idBook ,title,pageCount),容器( idLibrary idBook ,数量)。

生成的所有内容都没有任何错误,但是当我在我的Container存储库上执行findAll()请求时,我的浏览器会冻结,并且没有显示任何内容,因为它被卡在循环中(我的Container表中有3行)。 / p>

class Bibliotheque
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(name="name", type="string", length=60)
 */
private $name;

/**
 * @ORM\Column(name="adress", type="string", length=60)
 */
private $adress;
}
class Conteneur
{    
/**
 * Many Containers have One library.
 * @ORM\Id
 * @ORM\OneToOne(targetEntity="Bibliotheque")
 * @ORM\JoinColumn(name="libraryId", referencedColumnName="id")
 */
private $library;

/**
 * One Container has One book.
 * @ORM\Id
 * @ORM\OneToOne(targetEntity="Livre")
 * @ORM\JoinColumn(name="bookId", referencedColumnName="id")
 */
private $book;

/**
 * @ORM\Column(name="quantity", type="integer")
 */
private $quantity;
}
class Livre
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(name="title", type="string", length=255)
 */
private $title;

/**
 * @ORM\Column(name="pageCount", type="integer")
 */
private $pageCount;

/**
 * Un livre a plusieurs auteurs
 * @ORM\ManyToMany(targetEntity="Auteur")
 * @ORM\JoinTable(name="books_authors",
 *      joinColumns={@ORM\JoinColumn(name="book_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="author_id", referencedColumnName="id")}
 *      )
 */
private $authors; 
}
class Auteur
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(name="name", type="string", length=60)
 */
private $name;

/**
 * @ORM\Column(name="surname", type="string", length=60)
 */
private $prenom;

/**
 * @ORM\Column(name="birthDate", type="date")
 */
private $birthDate;
}

1 个答案:

答案 0 :(得分:0)

首先,请仅使用法语或英语作为班级名称和变量。

在课堂内添加id" Container"并删除注释@ORM \ id for attributes" library"和"预订"

定义属性"作者" with ArrayCollection和add方法getAuthors(),addAuthor(作者$ author),removeAuthor(作者$ author)

并重命名表名=> "名_ID"不是" nameId"

查看文档http://symfony.com/doc/current/doctrine.html

class Library
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
 * @var string $name
 *
 * @ORM\Column(name="name", type="string", length=60)
 */
private $name;

/**
 * @var string $address
 *
 * @ORM\Column(name="address", type="string", length=60)
 */
private $address;
}

class Container
{
/**
 * @var integer $id
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */

private $id;

/**
 * Many Containers have One library.
 *
 * @ORM\OneToOne(targetEntity="Library")
 * @ORM\JoinColumn(name="library_id", referencedColumnName="id")
 */
private $library;

/**
 * One Container has One book.
 *
 * @ORM\OneToOne(targetEntity="Book")
 * @ORM\JoinColumn(name="book_id", referencedColumnName="id")
 */
private $book;

/**
 * @ORM\Column(name="quantity", type="integer")
 */
private $quantity;
}

class Book
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string $title
 *
 * @ORM\Column(name="title", type="string", length=255)
 */
private $title;

/**
 * @var string $pageCount
 *
 * @ORM\Column(name="page_count", type="integer")
 */
private $pageCount;

/**
 * A book have many authors
 *
 * @var ArrayCollection $authors
 *
 * @ORM\ManyToMany(targetEntity="Auteur")
 * @ORM\JoinTable(name="books_authors",
 *      joinColumns={@ORM\JoinColumn(name="book_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="author_id", referencedColumnName="id")}
 *      )
 */
private $authors;

/**
 * @return ArrayCollection
 */
public function getAuthors()
{
    return $this->authors;
}

/**
 * @param Author $author
 *
 * @return $this
 */
public function addAuthor(Author $author)
{
    $this->authors->add($author);

    return $this;
}

/**
 * @param Author $author
 *
 * @return $this
 */
public function removeAuthor(Author $author)
{
    $this->authors->removeElement($author);

    return $this;
}
}

class Author
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
 * @var string $firstname
 *
 * @ORM\Column(name="firstname", type="string", length=60)
 */
private $firstname;

/**
 * @var string $lastname
 *
 * @ORM\Column(name="lastname", type="string", length=60)
 */
private $lastname;

/**
 * @var DateTime $birthDate
 *
 * @ORM\Column(name="birthDate", type="date")
 */
private $birthDate;
}