doujia6503 2016-02-01 19:02
浏览 21
已采纳

教条中的标准和缓存

I have a doctrine entity called Site with the following field:

/**
 * @ORM\OneToMany(targetEntity="SiteAsset", mappedBy="site")
 */
protected $assets;

I also have a function for getting an element of $assets with a given value of its url field:

public function getAssetByUrl($url) {
    $c = Criteria::create()->
    where(Criteria::expr()->eq('url',$url));
    $matching = $this->assets->matching($c);
    return $matching[0];
}

This function behaves very strangely. It appears to work if I run it immediately after fetching the entity from the database. But after a few database operations have been queued up, it begins to fail. I know it is failing, as I can find the asset I want as follows:

public function getAssetByUrl($url) {
    foreach($this->assets as $asset) {
        if($asset->getUrl() === $url) {
            return $asset;
        }
    }
}

Furthermore, if I combine the two functions into:

public function getAssetByUrl($url) {
    foreach($this->assets as $asset) {
        if($asset->getUrl() === $url) {
            error_log('found');
        }
    }

    $c = Criteria::create()->
    where(Criteria::expr()->eq('url',$url));
    $matching = $this->assets->matching($c);
    error_log($matching[0] ? 'found' : 'not found');
    return $matching[0];
}

Then the Criteria always fails to find a match (i.e., when the function is called it always prints out 'found' followed by 'not found'). This would suggest that Doctrine is failing to find the entity I want in the case when it has already cached the entities in memory.

How can I ensure that matches are always found, while still making use of Doctrine's Criteria filtering system?

  • 写回答

1条回答 默认 最新

  • dongliang1893 2016-02-09 10:30
    关注

    The issue was with the following line:

    return $matching[0];
    

    This returns the right answer if the PersistentCollection has not yet been fully fetched from the database. This is because the criteria is converted to an SQL query that returns an array containing only matching items; therefore getting the item at index 0 succeeds.

    This returns the wrong answer if the PersistentCollection has already been fully fetched. This is because, internally, doctrine calls array_filter on the in-memory collection and returns the result. This means that the first element in the result is not necessarily at index 0, as array_filter preserves keys.

    The solution was to wrap $matching in array_values($matching).

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

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)