dsf8897 2013-08-22 15:58
浏览 35
已采纳

在Doctrine 1.2.4中记录同步问题

I have run through an unexpected behaviour while using Doctrine 1.2.4 with Symfony: accessing the records via the getters did not lead to the same result as shooting a query to retrieve these same records. Here is how it goes:

I am working with two tables: a tpTrip table, on which we perform actions(= status changes), and a tpTripActivity table, that keeps track of all the actions that have been performed on the trip. tpTrip has a one to many relationship pointing to tpTripActivity.

At some point, I wanted to know if a "CheckIn" action had been performed on a tpTrip record. So in my Doctrine tpTrip class, I wrote the (naive) method:

public function has_been_checked_in()
{               
    foreach($this->getActivities() as $activity)
    {           
        if($activity->getAction() == "CheckIn") return true;
    }
    return false;
}

Now after some frustrations, observing that a recently performed CheckIn action was not turning up in my results, I wrote the following function, which I would expect to do the same:

public function has_been_checked_in2()
{               
        $total = Doctrine_Core::getTable("tpTripActivity")->createQuery("a")
        ->select("count(*)")
        ->where("trip_id = ?", $this->getId())
        ->andWhere("action = ?", "CheckIn")
        ->fetchOne(array(), Doctrine_Core::HYDRATE_SINGLE_SCALAR);

    return ($total > 0);
}

In this second version, I could get consistent results.

So my question is: Why do I not get the same results with the two functions ? How can I make sure that my records are up to date when I use my foreign key getters ?

  • 写回答

1条回答 默认 最新

  • douciping4283 2014-05-07 14:50
    关注

    Doctrine permits to refresh records' relashionships, as mentionned in the doc. This would go as follow:

    $this->refreshRelated();
    foreach($this->getActivities() as $activity){
        ....
    }
    

    See also this post on stack overflow.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch