Symfony2 saves Doctrine\Common\Collections\ArrayCo

2019-08-09 18:26发布

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.

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

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..

1条回答
贼婆χ
2楼-- · 2019-08-09 18:39

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

Your Artist entity will look like

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

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

查看更多
登录 后发表回答