嗨,我遇到了一个问题,我找不到解决方案,所以寻求帮助。
我有两个实体:演员和艺术家。在演员阵容中,我有演员,女演员,这将由艺术家表字段,我使用了这个代码:
为此:
namespace BbdMyAppBundleForm;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolverInterface;
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')
;
}
可以有多个演员。所以在数据库中,它保存为:
DoctrineCommonCollectionsArrayCollection@000000006f69bd7b000000001772666a
在演员领域。我不知道为什么,它应该保存id或名称。
这是铸造模板:
BbdMyAppBundleEntityCast:
type: entity
repositoryClass: BbdMyAppBundleRepositoryCastRepository
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
BbdMyAppBundleEntityArtist:
type: entity
repositoryClass: BbdMyAppBundleRepositoryArtistRepository
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
谢谢你的帮助。。
根据您的场景,我可以建议您在Cast
和Artist
实体之间建立ManyToMany
关联,每个演员阵容都有许多艺术家,每个艺术家可以出现在多个演员阵容中
您的Artist
实体将看起来像
use DoctrineORMMapping as ORM;
/** @Entity **/
class Artist
{
/**
* @ORMManyToMany(targetEntity="Cast", inversedBy="artists")
* @JORMoinTable(name="cast_artists")
**/
private $cast;
public function __construct() {
$this->cast = new DoctrineCommonCollectionsArrayCollection();
}
}
Cast
实体将具有类似的映射
use DoctrineORMMapping as ORM;
/** @Entity **/
class Cast
{
/**
* @ORMManyToMany(targetEntity="Artist", mappedBy="cast")
**/
private $artists;
public function __construct() {
$this->artists = new DoctrineCommonCollectionsArrayCollection();
}
public function addArtist($artists) {
$this->artists[] = $artists;
return $this;
}
public function removeArtist($artists) {
$this->artists->removeElement($artists);
}
public function getArtists() {
return $this->artists;
}
}
一旦你添加了所有的艺人记录,你就可以通过选择多个艺人来创建演员记录,无论是演员还是女演员