我有两个实体Article
和Image
与ManytoMany关系相关,使用名为ArticlesHaveImages
的第三个实体。
我没有使用经典的ManytoMany关系,因为我在ArticlesHaveImages
中有第三个字段称为序列。
class Article
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
...
...
/**
* One Article has Many articleHaveImage.
* @ORM\OneToMany(targetEntity="ArticlesHaveImages", mappedBy="article")
*/
private $images;
class Image
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
...
...
/**
* One Image has Many articleHaveImage.
* @ORM\OneToMany(targetEntity="ArticlesHaveImages", mappedBy="image")
*/
private $articles;
public function __construct() {
$this->articles = new ArrayCollection();
}
class ArticlesHaveImages
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* Many ArticlesHaveImages have One Article.
* @ORM\ManyToOne(targetEntity="Article", inversedBy="images")
* @ORM\JoinColumn(name="article_id", referencedColumnName="id")
*/
private $article;
/**
* Many ArticlesHaveImages have One Image.
* @ORM\ManyToOne(targetEntity="Image", inversedBy="articles")
* @ORM\JoinColumn(name="image_id", referencedColumnName="id")
*/
private $image;
/**
* @var int
*
* @ORM\Column(name="sequence", type="integer")
*/
private $sequence;
如何创建表单? 我想要一个多选项,所有图像都是名称。
我试过了:
class ArticleType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$images = $options['images'];
$builder
->add('translations', CollectionType::class, array(
'entry_type' => ArticleTranslationType::class,
'entry_options' => array('label' => false)))
->add('images', ChoiceType::class, array(
"required" => false,
"choices" => $images,
'multiple' => true,
但我明白了:
无法确定属性"图像"的访问类型在班上 "的appbundle \实体\第"
非常感谢!