gpt4 book ai didi

php - 设置实体 OneToOne

转载 作者:行者123 更新时间:2023-11-30 22:39:59 28 4
gpt4 key购买 nike

我创建实体 Developer 和 TalentFile,我希望一个开发人员有一个文件 (Cv),当我设置实体时,我已经提交了 null

/**
* 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;

这个 Action :

  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');
}
}

在我的数据库中我有 user_talnet_file=Null 为什么?必须是 Id 开发者?并且希望,如果开发人员有 Cv 并且希望数据库中的另一个 Cv 不创建新实体 Talent_files,请更新它。请帮忙

最佳答案

我这样解决了我的问题:

                $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();

关于php - 设置实体 OneToOne,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31382254/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com