Symfony2将Doctrine\Common\Collections\ArrayCollection保存在数据库中,而不是名称 [英] 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:
为此:
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中保存如下:
there can be multiple actor or actress. so in db it saves like:
Doctrine\Common\Collections\ArrayCollection@000000006f69bd7b000000001772666a
。我不知道为什么,它应该保存ID或名称。
in the actor field. i dont why, it should save the id or name.
这是演员表:
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
Artist 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
感谢帮助。.
推荐答案
您的方案,我建议您在 Cast
和 Artist $之间建立一个
ManyToMany
关联c $ c>实体每个演员都有很多艺术家,每个艺术家可以出现在一个以上的演员中
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
您的 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;
}
}
添加所有艺术家记录后,您可以创建通过选择多位演员(无论演员是演员)来投射唱片
Once you have added all artists record you can create a cast record by selecting multiple artists whether its actor/actress
这篇关于Symfony2将Doctrine\Common\Collections\ArrayCollection保存在数据库中,而不是名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!