2015-04-19 27 views
1

嗨,我面臨一個我無法找到它的解決方案,所以尋求幫助。Symfony2在數據庫中保存Doctrine Common Collections ArrayCollection而不是名稱

我有兩個實體:演員和藝術家。在投從我有演員,女演員,這將是由藝術家表中的字段,我用這個代碼:

爲:

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') 
    ; 
} 

可以有多個演員或女演員。所以在數據庫它保存像:

Doctrine\Common\Collections\[email protected]  

在演員領域。我不知道爲什麼,它應該保存身份證或姓名。

這裏是投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 

藝術家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

您正在使用文本類型存儲藝術家到演員實體中,這真的不是一個好主意,更好地將演員和藝術家之間建立關聯,如「OneToMany ','ManyToMany'這是最適合你的結構建設之前,我建議你閱讀官方文檔['數據庫和Doctrine'](http://symfony.com/doc/current/book/doctrine.html),它也有例子有很好的解釋 –

+0

我總共有20個投射場,所以如果我設置了關係,那麼會有這麼多表嗎?謝謝你的建議.. –

+0

不,你的演員和藝術家實體之間會存在一個關係,而這個關係表將會與演員和藝術家相關,只是看看['學說的多到多](http:// doctrine- orm.readthedocs.org/en/latest/reference/association-mapping.html#many-to-many-unidirectional) –

回答

0

根據您的情況,我可以建議你有你的Cast之間的關聯ManyToManyArtist實體每個演員有許多藝術家,每個藝術家可以出現在多於一個演員陣容

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; 
    } 
} 

映射一旦你添加了所有的藝術家錄製,您可以通過選擇多個藝術家是否創造鑄記錄其演員/女演員

相關問題