简体   繁体   English

Symfony2-以表格形式显示一个属性,由另一个属性映射

[英]Symfony2 - show one property in form, mapping by another property

So I'm working on application to handle library maintenance. 因此,我正在研究处理库维护的应用程序。 In my database I have three tables: Book, Category and BookCategory. 在我的数据库中,我有三个表:Book,Category和BookCategory。 In Book table I have two fields: BookID and BookTitle , in Category I have CategoryID and CategoryName and in BookCategory I have BookID (foreign key of Book(BookID)) and CategoryID (foreign key of Category(CategoryID). I autogenerated controllers and form by using php app/console generate:doctrine:crud --entity=AppBundle:EntityName 书籍表我有两个字段: 的BookIDBOOKTITLE,在I 具有类别ID类别名称BookCategory的BookID类别ID(类别(类别ID)的外键(图书(的BookID)的外键),我自动生成控制器和形式通过使用php app/console generate:doctrine:crud --entity=AppBundle:EntityName

The point is, when I try to add new book to database, I can enter title of book and it's category by category ID (picture related), but I want to add new book using category name, not category ID, though it still should be mapped by category ID to BookCategory table. 关键是,当我尝试将新书添加到数据库时,我可以按类别ID(与图片相关)输入书籍的标题及其类别,但是我想使用类别名称(而不是类别ID)添加新书,尽管它仍然应该通过类别ID映射到BookCategory表。 How to do it? 怎么做? How it looks like and how I want it to look like 它看起来如何,我希望它看起来如何

Book.php 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 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 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'
    ));
}
}

This should do it: 应该这样做:

$builder->add('categoryId', EntityType::class, array(
    'class' => 'AppBundle:Category',
    'choice_label' => 'categoryName',
    'expanded' => true,
));

Add category to the form as entity: category作为实体添加到表单:

$builder
    ->add('title')
    ->add('category', 'entity', [
        'class' => Category::class,
        'choice_label' => 'categoryName'
    ])
;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM