学说中两个表之间的关系。数据不会保存在数据库中

时间:2017-07-31 11:55:05

标签: doctrine-orm entities

我在Database中保存数据时遇到问题。我有两个用Doctrine Entities创建的表。我的实体是:

<?php

namespace App\Http\Entities\Cic;
use \Doctrine\ORM\Mapping AS ORM;
use \Doctrine\Common\Collections\ArrayCollection;
/**
 * @ORM\Entity
 * @ORM\Table(name="cic_case_files")
 */
class CicCaseFile {

    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="CicCase", inversedBy="cicFiles")
     * @ORM\JoinColumn(name="case_id", referencedColumnName="id")
     */
    protected $case;

    /**
     * One Product has One Shipment.
     * @ORM\OneToOne(targetEntity="File")
     * @ORM\JoinColumn(name="file_id", referencedColumnName="id")
     */
    protected $file;
    /**
     * @ORM\Column(name="case_id", type="integer", nullable=false)
     */
    protected $case_id;
    /**
     * @ORM\Column(name="file_id", type="integer", nullable=false)
     */
    protected $file_id;
    public function __construct() {

        //$this->mailboxes = new ArrayCollection();
    }
    public function setFileId($fileId){

        $this->file_id = $fileId;
    }
    public function getFileId(){

        return $this->file_id;
    }
    public function setCaseId($caseId){

        $this->case_id = $caseId;
    }
    public function getCaseId(){

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

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

        return $this->case;
    }
    public function setCase($case){

        $this->case = $case;
    }
    public function getFile(){

        return $this->file;
    }
    public function setFile($file){

        $this->file = $file;
    }



}



<?php

namespace App\Http\Entities\Cic;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="\App\Http\Repositories\Cic\FileRepository")
 * @ORM\Table(name="files")
 */
class File
{

    /**
     * @ORM\Id()
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @var int
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     * @var string
     */
    protected $path;

    /**
     * @ORM\Column(type="string")
     * @var string
     */
    protected $originalName;

    /**
     * @ORM\Column(type="string")
     * @var string
     */
    protected $hashName;


    /**
     * @ORM\Column(type="string")
     * @var string
     */
    protected $extension;

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

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

    public function getPath(){
        return $this->path;
    }

    public function setPath($path){
        $this->path = $path;
    }

    public function getOriginalName(){
        return $this->originalName;
    }

    public function setOriginalName($originalName){
        $this->originalName = $originalName;
    }

    public function getHashName(){
        return $this->hashName;
    }

    public function setHashName($hashName){
        $this->hashName = $hashName;
    }

    public function getExtension(){
        return $this->extension;
    }

    public function setExtension($extension){
        $this->extension = $extension;
    }
}

我的桌子:

文件: files table

cic_case_file: cic_case_files

我想通过代码保存表CicCaseFile中的数据:

$cicFile =  new CicCaseFile();
cicFile->setCaseId($caseId);
$cicFile->setFileId($fileId);
$this->entityManager->persist($cicFile);
$this->entityManager->flush();

我的实体出了点问题,但我不知道是什么。有人可以帮助我吗?我会非常感激的。最好的问候。

1 个答案:

答案 0 :(得分:0)

如果我在你的位置,这就是我过去常常遵循的。

您的文件实体必须只有 id,path,extension,original_name,hash_name 属性和方法,您可以在内部使用任何转换方法

您的 CicCaseFile 实体必须只有 id,case_id,file_id 属性和方法,您可以在内部使用任何转换方法

那么在插入数据时你的方法是什么,首先你需要为你的父表插入数据,即文件表,然后是其子表,即 cic_case_file

$em = $this->getDoctrine()->getManager(); /* In Symfony Framework */

$fileObj = new File();
$fileObj->setPath($path);
$fileObj->setExtension($extension);
$fileObj->setOriginalName,($originalName);
$fileObj->setHashName($hashName);
$em->persist($fileObj);
$em->flush(); 

/* So once you flush here it fileObj will be having the last inserted object which you can use to store in child table */

$cicCaseFileObj = new CicCaseFile();
$cicCaseFileObj->setFile($fileObj); /* $fileObj which was inserted above */
$cicCaseFileObj->setCaseId($caseObj); /* Same as $fileObj as inserted above in case if this is from different table */

$ EM-&GT;坚持($ cicCaseFileObj); $ EM-&GT;冲洗();

如果在不同的saveMethod中插入了caseId,那么首先需要获取该对象,然后将其提供给上述对象,如下所示

$caseId = 10; /* This you may get from else where */
$caseObj = $em->getRepository('BundleName:Case')->find($caseId);

/* Make sure your case_id is nullable or not */
$cicCaseFileObj = new CicCaseFile();
$cicCaseFileObj->setFile($fileObj);
if($caseObj){
    $cicCaseFileObj->setCaseId($caseObj); 
}
$em->persist($cicCaseFileObj);
$em->flush();

快乐的编码!