public static function updateRelationCache($parentId = null, $manualLock = false) { //$m = memory_get_usage(); if (!$manualLock) { self::checkRelationCache('wait'); self::checkRelationCache('lock'); } try { $relationsFile = sfConfig::get('sf_root_dir') . "/cache/objcache/childrenRelations.php"; $found = false; if ($parentId && is_readable($relationsFile)) { $handle = fopen($relationsFile, "r+"); if ($handle) { $content = fread($handle, filesize($relationsFile)); $rels = explode("\$_Rel[" . $parentId . "]", $content); $cnt = count($rels); if ($cnt > 1) { $prevContent = $rels[0]; $p = strpos($rels[$cnt - 1], ";"); $nextContent = substr($rels[$cnt - 1], $p + 2); $found = true; } } } if (!$found) { // if not found - create relations for ALL documents $prevContent = "<?php \n"; $nextContent = "\n?>"; $parentId = null; } $c = new Criteria(); if ($parentId) { $c->add(RelationPeer::ID1, $parentId); } $c->addAscendingOrderByColumn('id1'); $c->addAscendingOrderByColumn('document_model2'); $c->addAscendingOrderByColumn('sort_order'); $rs = RelationPeer::doSelectRs($c); $i = 0; $content = $prevContent; // "<?php \n"; $oldIDModel = ''; $currIDModel = ''; $idStr = ''; while ($rs->next()) { $id1 = $rs->getInt(1); $id2 = $rs->getInt(2); $model1 = $rs->getString(3); $model2 = $rs->getString(4); $sort = $rs->getString(5); $currIDModel = $id1 . ':' . $model2; if ($i == 0) { $oldIDModel = $currIDModel; } $i++; if ($currIDModel == $oldIDModel) { $idStr .= "," . $id2; } else { $idStr = substr($idStr, 1); $content .= "\$_Rel[" . $oldId1 . "][\"" . $oldModel2 . "\"] = explode(\",\", \"" . $idStr . "\");\n"; $idStr = "," . $id2; } $oldIDModel = $currIDModel; $oldId1 = $id1; $oldModel2 = $model2; } if ($idStr) { $idStr = substr($idStr, 1); $content .= "\$_Rel[" . $oldId1 . "][\"" . $oldModel2 . "\"] = explode(\",\", \"" . $idStr . "\");\n"; } $content .= $nextContent; /* "\n?>"; */ FileHelper::writeFile($relationsFile, $content); $_SESSION['childrenRelations'] = null; BackendService::loadChildrenRelations(); } catch (Exception $e) { } if (!$manualLock) { self::checkRelationCache('unlock'); } //$m2 = memory_get_usage(); //FileHelper::Log(round(($m2-$m)/1024/1024,2)." Mo"); }
public static function isChildOf($parentId, $childId) { if ($parentId == $childId) { return true; } if (BackendService::loadChildrenRelations()) { if (isset($_SESSION['childrenRelations'][$parentId])) { $parentItems = $_SESSION['childrenRelations'][$parentId]; foreach ($parentItems as $model => $items) { if (in_array($childId, $items)) { return true; } } return false; } else { return false; } } else { $parent = self::getParentOf($childId, null, false); while ($parent) { if ($parent == $parentId) { return true; } $parent = self::getParentOf($parent, null, false); } } return false; }