简体   繁体   English

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: 从我有演员,女演员的演员表中,我使用以下代码:

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 根据您的情况,我建议您在CastArtist实体之间建立一个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.

相关问题 Symfony2错误:尝试在类“ Doctrine \\ Common \\ Collections \\ ArrayCollection”上调用方法 - Symfony2 Error: Attempted to call method on class “Doctrine\Common\Collections\ArrayCollection” Symfony2,Doctrine2找到关于sth#category的Doctrine \\ Common \\ Collections \\ ArrayCollection类型的实体,但是期待...... - Symfony2, Doctrine2 Found entity of type Doctrine\Common\Collections\ArrayCollection on association sth#category, but expecting sth Symfony 3异常:类型错误:参数1传递给Doctrine \\ Common \\ Collections \\ ArrayCollection - Symfony 3 Exception: Type error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection 使用OneToMany时,Symfony 3预期会给出“ Doctrine \\ Common \\ Collections \\ ArrayCollection” - Symfony 3 expected “Doctrine\Common\Collections\ArrayCollection” given … when using OneToMany "usort" 一个 Doctrine\\Common\\Collections\\ArrayCollection? - "usort" a Doctrine\Common\Collections\ArrayCollection? Symfony:$images 必须是 Doctrine\Common\Collections\ArrayCollection 的实例,使用 Doctrine\ORM\PersistentCollection - Symfony: $images must be an instance of Doctrine\Common\Collections\ArrayCollection, Doctrine\ORM\PersistentCollection used symfony2原则findBy在arrayCollection中的ID - symfony2 doctrine findBy id in arrayCollection Symfony 在设置旧项目“vendor/doctrine/collections/lib/Doctrine/Common/Collections/ArrayCollection.php on line 243”时显示错误 - Symfony showing error when setting Up an old Project “vendor/doctrine/collections/lib/Doctrine/Common/Collections/ArrayCollection.php on line 243” Symfony2 doctrine过滤器集合 - Symfony2 doctrine filter collections Doctrine \\ Common \\ Collections \\ ArrayCollection类不是有效实体或映射的超类 - Class Doctrine\Common\Collections\ArrayCollection is not a valid entity or mapped super class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM