dppfxf909679
2015-01-01 22:52
浏览 62
已采纳

Symfony2 - 以一对一的关系更新实体

I am trying to update my SingleTask entity however no change is accepted. The relationship between my Event and SingleTask is a bi-directional one-to-one relationship.

The relevant portions of my Event entity are:

class Event
{
   // properties

   /**
    * @OneToOne(targetEntity="SingleTask", mappedBy="event", cascade={"persist", "remove"})
    */
   private $singletask;

   public function setSingleTask(SingleTask $singletask)
   {
      $this->singletask = $singletask;
      $singletask->setEvent($this);
   }

   public function getSingleTask()
   {
      return $this->singletask;
   }
}

The relevant portions of my SingleTask entity are:

/**
 * @Entity
 */
class SingleTask
{
   /**
    * @Column(type="datetime", nullable=true)
    */
   private $lastRun;

   /**
     * @OneToOne(targetEntity="Event", inversedBy="singletask")
     * @JoinColumn(name="event_id", referencedColumnName="id", nullable=true)
     **/
   private $event;

   public function setLastRun(\DateTime $lastRun = null)
   {
      $this->lastrun = $lastRun;
   }
}

In a separate command file:

$eventRepo = $this->em->getRepository('HeyRemy\HeyRemyBundle\Entity\Event');
$events = $eventRepo->findAll();

foreach ($events as $event)
{
   $singletask = $event->getSingleTask();

   if ($singletask != null)
   {
      $singletask->setLastRun(new \DateTime);
      $event->setSingleTask($singletask);

      $this->em->flush();
   }
}

Unfortunately, the lastRun property on the SingleTask is never updated in the database. What is not correct?

Edit: Code samples of how I create an event:

$event = new Event();
$userRepo = $this->em->getRepository('HeyRemyBundle:User');
$user = $userRepo->find($user_id);
$event->setUser($user);

$this->em->persist($event);
$this->em->flush();

$event_id = $event->getId();

// other code which uses $event_id (SingleTask does not)

$singletask = new SingleTask();
$singletask->setNextRun($event_start);

$this->em->persist($singletask);

$event->setSingleTask($singletask);

$this->em->flush();

图片转代码服务由CSDN问答提供 功能建议

我正在尝试更新我的SingleTask实体,但不接受任何更改。 我的Event和SingleTask之间的关系是双向的一对一关系。

我的Event实体的相关部分是:

   class Event 
 {
 // properties >> n 
 / ** 
 * @OneToOne(targetEntity =“SingleTask”,mappedBy =“event”,cascade = {“persist”,“remove”})  
 * / 
 private $ singletask; 
 
 public function setSingleTask(SingleTask $ singletask)
 {
 $ this-> singletask = $ singletask; 
 $ singletask-> setEvent($ this);  
 
 
 
公共函数getSingleTask()
 {
返回$ this-> singletask; 
} 
} 
   
 
 

相关 我的SingleTask实体的部分是:

  / ** 
 * @Entity 
 * / 
class SingleTask 
 {
 / ** 
 * @Column(  type =“datetime”,nullable = true)
 * / 
 private $ lastRun; 
 
 / ** 
 * @OneToOne(targetEntity =“Event”,inversedBy =“singletask”)
 * @JoinColumn  (name =“event_id”,referencedColumnName =“id”,nullable = true)
 ** / 
 private $ event; 
 
 public funct  ion setLastRun(\ DateTime $ lastRun = null)
 {
 $ this-> lastrun = $ lastRun; 
} 
} 
   
 
 

单独的命令文件:

  $ eventRepo = $ this-> em-> getRepository('HeyRemy \ HeyRemyBundle \ Entity \ Event'); 
 $ events = $ eventRepo  - > findAll(); 
 
foreach($ events as $ event)
 {
 $ singletask = $ event-> getSingleTask(); 
 
 if if($ singletask!= null)
 {  
 $ singletask-> setLastRun(new \ DateTime); 
 $ event-> setSingleTask($ singletask); 
 
 $ this-> em-> flush(); 
} 
}  
   
 
 

不幸的是,SingleTask上的lastRun属性永远不会在数据库中更新。 什么是不正确的?

编辑:我如何创建事件的代码示例:

  $ event = new Event(); \  n $ userRepo = $ this-> em-> getRepository('HeyRemyBundle:User'); 
 $ user = $ userRepo-> find($ user_id); 
 $ event-> setUser($ user)  ; 
 
 $ this-> em-> persist($ event); 
 $ this-> em-> flush(); 
 
 $ event_id = $ event-> getId()  ; 
 
 //使用$ event_id的其他代码(SingleTask不会)
 
 $ singletask = new SingleTask(); 
 $ singletask-> setNextRun($ event_start); 
 
 $ this-  > EM->坚持($ singletask); 
 
 $的事件 - > setSingleTask($ singletask); 
 
 $的这 - > EM->冲洗(); 
    
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题