douzhi3105 2013-03-14 12:09
浏览 14
已采纳

如何在extbase模型中重置具有1:1关系的属性

I've got a problem regarding a model in extbase with a 1:1 relation. My model "Beast" has a link to another Model "MissingBeast" which is realized through a 1:1 relation. I can successfully add add the MissingBeast object to the Beast object. But I don't know how the reset it.

The following code shows the property definition in the Model Beast

/**
 * missingBeast
 *
 * @var Tx_Hobeast_Domain_Model_MissingBeast
 * @lazy 
 */
 protected $missingBeast;

/**
 * Returns the missingBeast
 *
 * @return Tx_Hobeast_Domain_Model_MissingBeast $missingBeast
 */
public function getMissingBeast() {
    return $this->missingBeast;
}

/**
 * Sets the missingBeast
 *
 * @param Tx_Hobeast_Domain_Model_MissingBeast $missingBeast
 * @return void
 */
public function setMissingBeast(Tx_Hobeast_Domain_Model_MissingBeast $missingBeast) {
    $this->missingBeast = $missingBeast;
}

the value for missingBeast in the database table of the Model Beast is just the id of the missingBeast. Which by default is 0.

Ho can I reset this id to 0 after a MissingBeast has been set? I've tried to just delete the missing beast like so:

   $missingBeast = $this->service->missingBeastRepository->findByBeast($beast);
   $this->service->missingBeastRepository->remove($missingBeast);

But when I do that the following query gets stuck in an infinite loop.

$query = $this->createQuery();
return $query->matching($query->equals("uid", $beast->getMissingBeast()))->execute()->count();

I'm using Typo version 4.7.8

  • 写回答

1条回答 默认 最新

  • douqiao2008 2013-03-14 18:08
    关注

    Simply set it to NULL, this will remove the relation and don't forget to update the Model in the repository.

    $beast = $this->beastRepository->findByUid(345)->setMissingBeast(NULL);
    $this->beastRepository->update($beast);
    

    In order to be able to set the property to NULL, you must adjust your model by setting the property to NULL by default and remove the type hint in the setter:

    /**
     * missingBeast
     *
     * @var Tx_Hobeast_Domain_Model_MissingBeast
     * @lazy 
     */
     protected $missingBeast = NULL;
    
    /**
     * Sets the missingBeast
     *
     * @param Tx_Hobeast_Domain_Model_MissingBeast $missingBeast
     * @return void
     */
    public function setMissingBeast($missingBeast) {
        $this->missingBeast = $missingBeast;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)