所以我正在处理应用程序以处理库维护。在我的数据库中,我有三个表:Book,Category和BookCategory。在 Book 表格中,我有两个字段: BookID 和 BookTitle ,在类别中我有 CategoryID 和 CategoryName 以及 BookCategory 我有 BookID (Book(BookID)的外键)和 CategoryID (类别的外键(CategoryID)。我通过使用自动生成控制器和表单
php app/console generate:doctrine:crud --entity=AppBundle:EntityName
关键是,当我尝试将新书添加到数据库时,我可以按类别ID(图片相关)输入书名和类别,但我想使用类别名称而不是类别ID添加新书,它仍应按类别ID映射到 BookCategory 表。怎么做? How it looks like and how I want it to look like
book.php中
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Book
*
* @ORM\Table(name="book")
* @ORM\Entity
*/
class Book
{
/**
* @var integer
*
* @ORM\Column(name="book_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $bookId;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=50, nullable=false)
*/
private $title;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Category", inversedBy="bookId")
* @ORM\JoinTable(name="book_category",
* joinColumns={
* @ORM\JoinColumn(name="book_id", referencedColumnName="book_id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="category_id", referencedColumnName="category_id")
* }
* )
*/
private $categoryId;
/**
* Constructor
*/
public function __construct()
{
$this->categoryId = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get bookId
*
* @return integer
*/
public function getBookId()
{
return $this->bookId;
}
/**
* Set title
*
* @param string $title
* @return Book
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Add categoryId
*
* @param \AppBundle\Entity\Category $categoryId
* @return Book
*/
public function addCategoryId(\AppBundle\Entity\Category $categoryId)
{
$this->categoryId[] = $categoryId;
return $this;
}
/**
* Remove categoryId
*
* @param \AppBundle\Entity\Category $categoryId
*/
public function removeCategoryId(\AppBundle\Entity\Category $categoryId)
{
$this->categoryId->removeElement($categoryId);
}
/**
* Get categoryId
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCategoryId()
{
return $this->categoryId;
}
public function __toString()
{
return (string)$this->bookId;
}
}
Category.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Category
*
* @ORM\Table(name="category")
* @ORM\Entity
*/
class Category
{
/**
* @var integer
*
* @ORM\Column(name="category_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $categoryId;
/**
* @var string
*
* @ORM\Column(name="category_name", type="string", length=50, nullable=false)
*/
private $categoryName;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Book", mappedBy="categoryId")
*/
private $bookId;
/**
* Constructor
*/
public function __construct()
{
$this->bookId = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get categoryId
*
* @return integer
*/
public function getCategoryId()
{
return $this->categoryId;
}
/**
* Set categoryName
*
* @param string $categoryName
* @return Category
*/
public function setCategoryName($categoryName)
{
$this->categoryName = $categoryName;
return $this;
}
/**
* Get categoryName
*
* @return string
*/
public function getCategoryName()
{
return $this->categoryName;
}
/**
* Add bookId
*
* @param \AppBundle\Entity\Book $bookId
* @return Category
*/
public function addBookId(\AppBundle\Entity\Book $bookId)
{
$this->bookId[] = $bookId;
return $this;
}
/**
* Remove bookId
*
* @param \AppBundle\Entity\Book $bookId
*/
public function removeBookId(\AppBundle\Entity\Book $bookId)
{
$this->bookId->removeElement($bookId);
}
/**
* Get bookId
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getBookId()
{
return $this->bookId;
}
public function __toString()
{
return (string)$this->categoryId;
}
}
BookType.php
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class BookType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('categoryId')
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Book'
));
}
}
答案 0 :(得分:2)
这应该这样做:
$builder->add('categoryId', EntityType::class, array(
'class' => 'AppBundle:Category',
'choice_label' => 'categoryName',
'expanded' => true,
));
答案 1 :(得分:1)
将category
作为实体添加到表单:
$builder
->add('title')
->add('category', 'entity', [
'class' => Category::class,
'choice_label' => 'categoryName'
])
;