,因此我正在努力处理库维护。在我的数据库中,我有三张表:书籍,类别和书籍法。在 book 表中我有两个字段: bookid 和 booktitle ,in category i em> and categoryname 和in bookCategory 我有 bookid (book of book(bookid))和 categoryid (类别的外键(类别ID)。i自动化控制器并通过使用 php app/console generate:doctrine:crud --entity=AppBundle:EntityName
重点是,当我尝试将新书添加到数据库中时,我可以输入书籍标题,它是类别ID(相关图片)的类别,但是我想使用类别名称而不是类别ID添加新书它仍然应按类别ID映射到 bookCategory 表。怎么做?它的外观以及我希望它看起来像
book.php
<?php
namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
/**
* Book
*
* @ORMTable(name="book")
* @ORMEntity
*/
class Book
{
/**
* @var integer
*
* @ORMColumn(name="book_id", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $bookId;
/**
* @var string
*
* @ORMColumn(name="title", type="string", length=50, nullable=false)
*/
private $title;
/**
* @var DoctrineCommonCollectionsCollection
*
* @ORMManyToMany(targetEntity="Category", inversedBy="bookId")
* @ORMJoinTable(name="book_category",
* joinColumns={
* @ORMJoinColumn(name="book_id", referencedColumnName="book_id")
* },
* inverseJoinColumns={
* @ORMJoinColumn(name="category_id", referencedColumnName="category_id")
* }
* )
*/
private $categoryId;
/**
* Constructor
*/
public function __construct()
{
$this->categoryId = new DoctrineCommonCollectionsArrayCollection();
}
/**
* 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 AppBundleEntityCategory $categoryId
* @return Book
*/
public function addCategoryId(AppBundleEntityCategory $categoryId)
{
$this->categoryId[] = $categoryId;
return $this;
}
/**
* Remove categoryId
*
* @param AppBundleEntityCategory $categoryId
*/
public function removeCategoryId(AppBundleEntityCategory $categoryId)
{
$this->categoryId->removeElement($categoryId);
}
/**
* Get categoryId
*
* @return DoctrineCommonCollectionsCollection
*/
public function getCategoryId()
{
return $this->categoryId;
}
public function __toString()
{
return (string)$this->bookId;
}
}
category.php
<?php
namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
/**
* Category
*
* @ORMTable(name="category")
* @ORMEntity
*/
class Category
{
/**
* @var integer
*
* @ORMColumn(name="category_id", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $categoryId;
/**
* @var string
*
* @ORMColumn(name="category_name", type="string", length=50, nullable=false)
*/
private $categoryName;
/**
* @var DoctrineCommonCollectionsCollection
*
* @ORMManyToMany(targetEntity="Book", mappedBy="categoryId")
*/
private $bookId;
/**
* Constructor
*/
public function __construct()
{
$this->bookId = new DoctrineCommonCollectionsArrayCollection();
}
/**
* 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 AppBundleEntityBook $bookId
* @return Category
*/
public function addBookId(AppBundleEntityBook $bookId)
{
$this->bookId[] = $bookId;
return $this;
}
/**
* Remove bookId
*
* @param AppBundleEntityBook $bookId
*/
public function removeBookId(AppBundleEntityBook $bookId)
{
$this->bookId->removeElement($bookId);
}
/**
* Get bookId
*
* @return DoctrineCommonCollectionsCollection
*/
public function getBookId()
{
return $this->bookId;
}
public function __toString()
{
return (string)$this->categoryId;
}
}
booktype.php
<?php
namespace AppBundleForm;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
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' => 'AppBundleEntityBook'
));
}
}
这应该做到:
$builder->add('categoryId', EntityType::class, array(
'class' => 'AppBundle:Category',
'choice_label' => 'categoryName',
'expanded' => true,
));
以实体的形式添加 category
:
$builder
->add('title')
->add('category', 'entity', [
'class' => Category::class,
'choice_label' => 'categoryName'
])
;