我制作了一个包含4个不同孩子的抽象上传课程。
类UploadCoanyPic,UploadCompanyLogo和UploadUserPic是一对一关系,但UploadPostFile必须是多对一(一个帖子有很多上传的文件),我想使用alle抽象的函数上课,但我不能让多对一的关系奏效。 这就是我现在所拥有的:
<?php
namespace AppBundle\Entity\Upload;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Util\Registry;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Abstract Class Upload
*
* @package AppBundle\Entity\Upload
*
* @ORM\MappedSuperclass()
* @ORM\Entity(repositoryClass="AppBundle\Repository\Upload\UploadRepository")
* @ORM\HasLifecycleCallbacks()
* @ORM\Table(name="upload", indexes={
* @ORM\Index(name="idx_cat_id", columns={"category","foreign_id"})
* }
* )
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="category", type="string")
* @ORM\DiscriminatorMap({"company-pic" = "UploadCompanyPic", "user-pic" = "UploadUserPic", "company-logo" = "UploadCompanyLogo", "post-file" = "UploadPostFile"})
*/
abstract class Upload
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(name="id", type="integer", length=255, nullable=false)
*/
protected $id;
protected $foreignId;
/**
some more code
* @return mixed
*/
public function getForeignId()
{
return $this->foreignId;
}
/**
* @param mixed $foreignId
*/
public function setForeignId($foreignId)
{
$this->foreignId = $foreignId;
}
UploadCompanyPic:
<?php
namespace AppBundle\Entity\Upload;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Upload\Upload as Upload;
/**
*
* @ORM\Entity
*
*/
class UploadCompanyPic extends Upload
{
protected $category="company-pic";
/**
* this is the foreign id of the mapped entity
* @ORM\Column(name="foreign_id", type="integer", length=255, nullable=false)
*/
protected $foreignId;
}
(上传CompanyLogo和UploadUserPic看起来一样)
<?php
namespace AppBundle\Entity\Upload;
use AppBundle\Entity\Blog\Post;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Upload\Upload as Upload;
/**
*
* @ORM\Entity
*
*/
class UploadPostFile extends Upload
{
protected $category="post-file";
/**
* ManyFiles have one Post
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Blog\Post", inversedBy="files")
* @ORM\JoinColumn(name="foreign_id", referencedColumnName="id")
*/
protected $foreignId;
/**
* @return ArrayCollection
*/
public function getForeignId()
{
return $this->foreignId;
}
/**
* @param Post $foreignId
*/
public function setForeignId($foreignId)
{
$this->foreignId = $foreignId;
}
}
对我有什么建议吗?谢谢!
答案 0 :(得分:0)
我的一位同事找到了答案:
/**
* Abstract Class Upload
*
* @package AppBundle\Entity\Upload
*
* @ORM\MappedSuperclass()
* @ORM\Entity(repositoryClass="AppBundle\Repository\Upload\UploadRepository")
* @ORM\HasLifecycleCallbacks()
* @ORM\Table(name="upload", indexes={
* @ORM\Index(name="idx_cat_id", columns={"category","foreign_id"})
* }
* )
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="category", type="string")
* @ORM\DiscriminatorMap({"company-pic" = "UploadCompanyPic", "user-pic" = "UploadUserPic", "company-logo" = "UploadCompanyLogo", "post-file" = "UploadPostFile", "partner-logo" = "UploadPartnerLogo"})
*/
abstract class Upload