2015-07-13 111 views
1

創建實體開發和TalentFile,我想對於一個開發人員有一個文件(CV),當我設置實體我已經向空設置實體OneToOne

/** 
* Developers 
* 
* @ORM\Table(name="developers") 
* @ORM\Entity(repositoryClass="Artel\ProfileBundle\Entity\DeveloperRepository") 
*/ 
class Developer 

/** 
* @var integer 
* 
* @ORM\Column(name="id", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 
/** 
* @ORM\OneToOne(targetEntity="TalentFiles", mappedBy="developer", cascade={"persist"}) 
*/ 
protected $talent_file; 

和實體:

/** 
* Talent_files 
* 
* @ORM\Table(name="Talent_files") 
* @ORM\Entity 
*/ 
class TalentFiles 
{ 
/** 
* @var integer 
* 
* @ORM\Column(name="id", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
private $id; 
/** 
* @ORM\OneToOne(targetEntity="Developer", inversedBy="talent_file") 
* @ORM\JoinColumn(name="user_talnet_file", nullable = true, referencedColumnName="id") 
* */ 
protected $developer; 

這個動作:

public function cvUploadAllAction($id) 
{ 

    $em = $this->getDoctrine()->getManager(); 
    $request = $this->get('request'); 
    $developer = $em->getRepository('ArtelProfileBundle:Developer')->findOneById($id); 

    if (! $developer) { 
     throw $this->createNotFoundException('Unable to find a profile.'); 
    } 

    $cv = $developer->getCvDirUri(); 

    if ($cv && file_exists($cv)) { 
     unlink($cv); 
    } 

    $form = $this->createForm(new DeveloperAllCvType(), array()); 

    if ($request->isMethod('POST')) { 

     $form->bind($request); 
     if ($form->isValid()) { 

      $data = $form->getData(); 

      $uploader = $this->get('artel.profile.file_uploader'); 
      $path = $uploader->uploadFile($data['photo']); 
      $developer->setCvDirUri($path['url']); 

      $content = shell_exec('/usr/bin/antiword '.'chmod o+r /var/www/aog-code/web/'.$path['url']); 

      if ($data['photo']->getClientMimeType() == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') { 

       $content = $this->get('parse_docx')->read_file_docx('/var/www/aog-code/web/'.$path['url']); 

      } elseif ($data['photo']->getClientMimeType() == 'application/pdf') { 

       $content = $this->get('parse_pdf')->parse('/var/www/aog-code/web/'.$path['url']); 

      } else { 
       $content = $this->get('parse_doc')->parse('/var/www/aog-code/web/'.$path['url']); 

      } 

      $url = sprintf(
       '%s%s', 
       $this->container->getParameter('acme_storage.amazon_s3.base_url'), 
       $this->getPhotoUploader()->uploadFromUrl($path['url']) 
      ); 

      $talent_files = new TalentFiles(); 
      $talent_files->setActive('yes') 
         ->setType($data['photo']->getClientMimeType()) 
         ->setContent($content) 
         ->setUserId($developer->getId()) 
         ->setFilePath($url) 
         ->setTalentId($developer->getId()); 

      $em->persist($talent_files); 
      $em->flush(); 

      $developer->setTextCv($content); 
      $developer->setCvUri($url); 
      $em->flush(); 

      return $this->redirect($this->generateUrl('artel_profile_homepage', array('id' => $id)).'#cv'); 
     } 
    } 

而且inmy DB我有user_talnet_file = NULL,爲什麼?必須是Id開發人員?如果開發人員擁有Cv並希望DB中的另一個Cv不會創建新實體Talent_files,請更新它。請幫助

回答

0

我解決我的pdoblem這樣的:

   $talent_file = $developer->getTalentFile(); 
      if (!empty($talent_file)) 
      { 
       $talent_file 
        ->setActive('yes') 
        ->setType($data['photo']->getClientMimeType()) 
        ->setContent($content) 
        ->setUserId($developer->getId()) 
        ->setFilePath($url) 
        ->setTalentId($developer->getId()) 
        ->setDeveloper($developer); 
       $em->persist($talent_file); 
       $em->flush(); 
      } 
      else 
      { 
       $talent_files = new TalentFiles(); 
       $talent_files->setActive('yes') 
        ->setType($data['photo']->getClientMimeType()) 
        ->setContent($content) 
        ->setUserId($developer->getId()) 
        ->setFilePath($url) 
        ->setTalentId($developer->getId()) 
        ->setDeveloper($developer); 
       $em->persist($talent_files); 
       $em->flush(); 

      } 

      $developer->setTextCv($content); 
      $developer->setCvUri($url); 
      $em->flush();