Symfony2 - 以形式显示一个 属性,由另一个 属性 映射
Symfony2 - show one property in form, mapping by another property
所以我正在开发处理图书馆维护的应用程序。在我的数据库中,我有三个 table:Book、Category 和 BookCategory。在 Book table 我有两个字段:BookID 和 BookTitle,在 Category 我有 CategoryID 和 CategoryName 并且在 BookCategory 我有 BookID(Book(BookID) 的外键)和 CategoryID(Category(CategoryID) 的外键)。我通过使用自动生成控制器和表单
php app/console generate:doctrine:crud --entity=AppBundle:EntityName
关键是,当我尝试将新书添加到数据库时,我可以输入书名和类别 ID(与图片相关),但我想使用类别名称而不是类别 ID 添加新书,尽管它仍应按类别 ID 映射到 BookCategory table。怎么做? 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'
));
}
}
将 category
添加到表单作为实体:
$builder
->add('title')
->add('category', 'entity', [
'class' => Category::class,
'choice_label' => 'categoryName'
])
;
应该这样做:
$builder->add('categoryId', EntityType::class, array(
'class' => 'AppBundle:Category',
'choice_label' => 'categoryName',
'expanded' => true,
));
所以我正在开发处理图书馆维护的应用程序。在我的数据库中,我有三个 table:Book、Category 和 BookCategory。在 Book table 我有两个字段:BookID 和 BookTitle,在 Category 我有 CategoryID 和 CategoryName 并且在 BookCategory 我有 BookID(Book(BookID) 的外键)和 CategoryID(Category(CategoryID) 的外键)。我通过使用自动生成控制器和表单
php app/console generate:doctrine:crud --entity=AppBundle:EntityName
关键是,当我尝试将新书添加到数据库时,我可以输入书名和类别 ID(与图片相关),但我想使用类别名称而不是类别 ID 添加新书,尽管它仍应按类别 ID 映射到 BookCategory table。怎么做? 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'
));
}
}
将 category
添加到表单作为实体:
$builder
->add('title')
->add('category', 'entity', [
'class' => Category::class,
'choice_label' => 'categoryName'
])
;
应该这样做:
$builder->add('categoryId', EntityType::class, array(
'class' => 'AppBundle:Category',
'choice_label' => 'categoryName',
'expanded' => true,
));