嗨,我正面临一个我无法找到解决方案,所以求助。
我有两个实体:演员和艺术家。在演员阵容中我有演员,女演员,这将是艺术家表的领域,我使用这个代码:
为此:
namespace Bbd\MyAppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class CastType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('actor', 'entity', array(
'class' => 'BbdMyAppBundle:Artist',
'property' => 'name',
'multiple' => true,
'label' => 'Artist',
'required' => false,
))
->add('actress')
->add('content')
;
}
可以有多个演员或女演员。所以在db中它保存如下:
Doctrine\Common\Collections\ArrayCollection@000000006f69bd7b000000001772666a
在演员字段中。我不知道为什么,它应该保存id或名称。
这是施法者:
Bbd\MyAppBundle\Entity\Cast:
type: entity
repositoryClass: Bbd\MyAppBundle\Repository\CastRepository
table: cast
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
actor:
type: text
nullable: true
actress:
type: text
nullable: true
oneToOne:
content:
targetEntity: Content
inversedBy: cast
joinColumn:
name: content_id
referencedColumnName: id
onDelete: CASCADE
艺术家ORM
Bbd\MyAppBundle\Entity\Artist:
type: entity
repositoryClass: Bbd\MyAppBundle\Repository\ArtistRepository
table: artist
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
name:
type: string
length: 255
unique: true
bangla_name:
type: string
length: 255
unique: true
priority:
type: integer
birth:
type: date
sex:
type: string
length: 6
bio_english:
type: text
bio_bangla:
type: text
感谢您的帮助..
答案 0 :(得分:0)
根据您的情况,我可以建议您在ManyToMany
和Cast
实体之间建立Artist
关联,每个演员都有很多艺术家,每个艺术家都可以出现超过一个演员
您的Artist
实体看起来像
use Doctrine\ORM\Mapping as ORM;
/** @Entity **/
class Artist
{
/**
* @ORM\ManyToMany(targetEntity="Cast", inversedBy="artists")
* @JORM\oinTable(name="cast_artists")
**/
private $cast;
public function __construct() {
$this->cast = new \Doctrine\Common\Collections\ArrayCollection();
}
}
并且Cast
实体将具有类似
use Doctrine\ORM\Mapping as ORM;
/** @Entity **/
class Cast
{
/**
* @ORM\ManyToMany(targetEntity="Artist", mappedBy="cast")
**/
private $artists;
public function __construct() {
$this->artists = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addArtist($artists) {
$this->artists[] = $artists;
return $this;
}
public function removeArtist($artists) {
$this->artists->removeElement($artists);
}
public function getArtists() {
return $this->artists;
}
}
添加完所有艺术家记录后,您可以通过选择多位艺术家来创建演员记录,无论其演员是谁/