两个实体之间的关系

时间:2015-10-14 23:27:46

标签: entity-framework symfony doctrine-orm

我遇到麻烦,我需要你的帮助。现在我创建了两个实体:

  • 合作伙伴
  • PartnerMedia

合作伙伴实体包含有关我的合作伙伴和其他实体的所有信息,与合作伙伴和索纳塔媒体实体有关系。

包含我的合作伙伴实体:

    <?php

namespace AppBundle\Entity;

use AppBundle\Entity\CMS\Block;
use AppBundle\Entity\SuperClass\SortableTranslatableEntity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Sonata\MediaBundle\Model\Media;
use Doctrine\Common\Collections\ArrayCollection;
use Sonata\TranslationBundle\Model\Gedmo\TranslatableInterface;

/**
 * Partner.
 *
 * @ORM\Table("partner")
 * @ORM\Entity
 */
class Partner extends SortableTranslatableEntity implements TranslatableInterface
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="libelle", type="string", length=60)
     * @Gedmo\Translatable
     */
    private $libelle;

    /**
     * @var string
     *
     * @ORM\Column(name="mea", type="boolean")
     */
    private $mea;


    /**
     * @ORM\OneToMany(targetEntity="\AppBundle\Entity\Media\PartnerMedia", mappedBy="partner", cascade={"persist"}, orphanRemoval=true)
     * @ORM\OrderBy({"position" = "ASC"})
     */
    private $medias;

    /**
     * @ORM\Column(type="string", name="url", nullable=true)
     * @Gedmo\Translatable
     */
    private $url;

    /**
     * @ORM\ManyToOne(targetEntity="\AppBundle\Entity\CMS\Block", inversedBy="block")
     */
    private $block;


    public function __construct()
    {
        $this->medias = new ArrayCollection();
    }

    public function setTranslatableLocale($locale)
    {
        $this->locale = $locale;
    }

    public function getContextName()
    {
        return 'partner';
    }

    public function __toString()
    {
        return $this->getId() ? (string) $this->getLibelle() : '-';
    }

    public function firstPhoto()
    {
        if ($this->getMedias() && $this->getMedias()->count()) {
            return $this->getMedias()->first()->getMedia();
        }
    }

    public function getId()
    {
        return $this->id;
    }

    public function getLibelle()
    {
        return $this->libelle;
    }

    /**
     * Add medias.
     *
     * @param PartnerMedia $media
     *
     * @return PartnerMedia
     */
    public function addMedias(PartnerMedia $media)
    {
        $media->setPartner($this);
        $this->medias[] = $media;

        return $this;
    }

    /**
     * Remove medias.
     *
     * @param PartnerMedia $media
     */
    public function removeMedia(PartnerMedia $media)
    {
        $this->medias->removeElement($media);
    }

    public function getMedias()
    {
        return $this->medias;
    }

    public function getUrl()
    {
        return $this->url;
    }

    /**
     * Get page.
     *
     * @return \AppBundle\Entity\CMS\Block
     */
    public function getBlock()
    {
        return $this->block;
    }

    public function setId($id)
    {
        $this->id = $id;
    }

    public function setLibelle($libelle)
    {
        $this->libelle = $libelle;
    }

    public function setUrl($url)
    {
        $this->url = $url;
    }

    /**
     * Set page.
     *
     * @param \AppBundle\Entity\CMS\Block $block
     * @return Partner
     */
    public function setBlock(Block $block = null)
    {
        $this->block = $block;

        return $this;
    }


    /**
     * Set mea
     *
     * @param boolean $mea
     * @return Partner
     */
    public function setMea($mea)
    {
        $this->mea = $mea;

        return $this;
    }

    /**
     * Get mea
     *
     * @return boolean 
     */
    public function getMea()
    {
        return $this->mea;
    }

    /**
     * @param mixed $medias
     */
    public function setMedias($medias)
    {
        $this->medias = $medias;
    }
}

以及包含PartnerMedia实体的内容:

    <?php
/**
 * PartnerMedia
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class PartnerMedia extends SortableEntity
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="\Application\Sonata\MediaBundle\Entity\Media")
     */
    private $medias;

    /**
     * @ORM\ManyToOne(targetEntity="\AppBundle\Entity\Partner", inversedBy="partner")
     *
     * @Gedmo\SortableGroup
     */
    private $partner;


    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set medias
     *
     * @param Media $medias
     * @return PartnerMedia
     */
    public function setMedias(Media $medias = null)
    {
        $this->medias = $medias;

        return $this;
    }

    /**
     * Get medias
     *
     * @return Media
     */
    public function getMedias()
    {
        return $this->medias;
    }

    /**
     * Set Partner
     *
     * @param Partner $partner
     * @return PartnerMedia
     */
    public function setPartner(Partner $partner = null)
    {
        $this->partner = $partner;

        return $this;
    }

    /**
     * Get Partner
     *
     * @return Partner
     */
    public function getPartner()
    {
        return $this->partner;
    }

    public function getContextName()
    {
        return 'partner_media';
    }

    public function __construct()
    {
        $this->medias = new ArrayCollection();
    }
}

现在,当我尝试创建新合作伙伴时,我收到了异常:

  

当前字段medias未链接到管理员。请为目标实体创建一个:AppBundle\Entity\Media\PartnerMedia

我需要你的帮助,谢谢

1 个答案:

答案 0 :(得分:1)

您描述的错误消息听起来更像是 Sonata Admin 错误。

有关奏鸣曲管理员的一个问题是,当您在实体之间创建链接时,您需要为两个实体类构建管理类。

很可能,在完全实现必要的管理员之前,您正尝试测试代码。