0

我正在Symfony2使用Doctrine 2. 我的应用程序中有 5 个实体。

  1. 关键字(双向,应获取所有具有特定关键字的书籍)
  2. 作者(双向,应获取具有特定作者的所有书籍)
  3. 类别(双向,应获取属于特定类别的所有书籍)
  4. BookExtra(单向,在 Book 实体中,应该获取 BookExtra 数据)

    • 每本书可以有很多关键词
    • 每本书可以有多个作者
    • 每本书都可以归入一个类别
    • 每本书都有一个 BookExtra 记录。
    • 关键字和作者表应包含唯一值

添加新Book的时,如果Author(s)andKeyword(s)存在,则尊重的Author IDandKeyword ID应分配给Book,如果不存在,records则将创建新的并将尊重ID的分配给Book.

我有以下内容Entity Classes

Book.php

namespace Acme\StoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Book
 *
 * @ORM\Table(name="book")
 * @ORM\Entity
 */
class Book {

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

/**
 * @var string
 *
 * @ORM\Column(name="isbn", type="string", length=16)
 */
protected $isbn;

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

/*
 * @ORM\OneToOne(targetEntity="BookExtra")
 * @ORM\JoinColumn(name="extra_id", referencedColumnName="id")
 *
 *   *********         OR        *********
 *   *********What should go HERE*********
 *
 */
protected $bookextra;

/*
 * @ORM\ManyToMany(targetEntity="Author", inversedBy="books")
 * @ORM\JoinTable(name="authors_books")
 */
protected $author;

/*
 * @ORM\OneToOne(targetEntity="Category")
 * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
 */
protected $category;

}

BookExtra.php

namespace Acme\StoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * BookExtra
 *
 * @ORM\Table(name="book_extra")
 * @ORM\Entity
 */
class Detail {

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

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

}

Author.php

namespace Bookhut\BookBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

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

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

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

// **********What should go HERE*********
protected $books;

}

Keyword& CategoryEntity 类似于 Author Entity

问题是,当我生成schemacli,它永远不会生成Relationships/Associations

什么应该Relationships/Associations适合Book & Author实体

我搜索了这个问题并且正确Relationships/Associations

我找到了这个:

Symfony2 应用程序/控制台不为实体关系/关联生成属性或模式更新

保存一对一关系实体

教义2:在一对多的双向关系中,如何从反面保存?

但这对我没有帮助。

有人可以举一个这种类型Relationships/Associationsinsert操作的例子吗?

4

1 回答 1

0

For author->books:

/**
 * @ManyToMany(targetEntity="Author", inversedBy="books")
 * @JoinTable(name="authors_books",
 *     joinColumns={@JoinColumn(name="book_id", referencedColumnName="id")},
 *     inverseJoinColumns={@JoinColumn(name="author_id", referencedColumnName="id")}
 * )
 */
protected $author;

For books->authors

/**
 * @ManyToMany(targetEntity="Book", mappedBy="author")
 */
protected $books;

See http://docs.doctrine-project.org/en/latest/reference/association-mapping.html#many-to-many-bidirectional for documentation of many-to-many

See http://docs.doctrine-project.org/en/latest/reference/association-mapping.html for full documentation of associations

EDIT

Is assume all your entities will have setters and getters.

// Create new Author object
$Author = new Author();
// Use setters to set all attributes for this author

// Create new book
$Book = new Book();
// Use setters to set all attributes for book

// Attach book to author
$Author->addBook($Book);
$Book->addAuthor($Author);

The addBook and addAuthor will look like this:

// Inside author entity
public function addBook(Book $Book) {
    $this->books->add($Book);
}

// Inside book entity
public function addAuthor($Author) {
    $this->authors->add($Author);
}

And add a constructor to both the author and book entities:

public function __construct() {
    $this->books = new ArrayCollection();
}

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

Have a look at the documentation to see more examples: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html

于 2013-10-18T14:42:14.740 回答