[英]Symfony2 saves Doctrine\Common\Collections\ArrayCollection in database not the name
Hi I am facing an that i cant not find the solution of it, so for a help. 嗨,我面临着我找不到解决方案的问题,因此寻求帮助。
I have two entities: Cast and Artists. 我有两个实体:演员和艺术家。 In cast from i have actor, actress which will be field by Artist table, I used this code: 从我有演员,女演员的演员表中,我使用以下代码:
for that : 为了那个原因 :
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')
;
}
there can be multiple actor or actress. 可以有多个演员。 so in db it saves like: 所以在数据库中保存如下:
Doctrine\Common\Collections\ArrayCollection@000000006f69bd7b000000001772666a
in the actor field. 在演员领域。 i dont why, it should save the id or name. 我不为什么,它应该保存ID或名称。
here is the cast orm: 这是演员表:
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
Artist ORM 艺术家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
Thanks for help.. 感谢帮助..
According to your scenario i can suggest you have a ManyToMany
association between your Cast
and Artist
entity each cast have many artists and each artist can appear in morethan one cast 根据您的情况,我建议您在Cast
和Artist
实体之间建立一个ManyToMany
关联,每个演员都有很多艺术家,而且每个艺术家可以出现在一个以上的演员中
Your Artist
entity will look like 您的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();
}
}
And Cast
entity will have a mapping like 和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;
}
}
Once you have added all artists record you can create a cast record by selecting multiple artists whether its actor/actress 添加所有艺术家记录后,您可以通过选择多个艺术家(无论是演员)来创建演员表记录
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.