我正在尝试使用嵌套三个级别的更多根位置填充表集:Region,Province和City。
从我正在阅读的excel文件中保存数据。
我写这个剧本:
function importLocations()
{
$objPHPExcel = PHPExcel_IOFactory::load("./data/elenco_comuni_italiani_30_giugno_2011.xls");
$locationTable = LocationTable::getInstance();
/* @var $tree Doctrine_Tree_NestedSet */
$tree = $locationTable->getTree();
$startRow = 2;
$indexRegion = 0;
$indexProvince = 2;
$indexCity = 1;
$objPHPExcel->setActiveSheetIndex($indexRegion);
$objWorksheetRegion = $objPHPExcel->getActiveSheet();
$highestRowRegion = $objWorksheetRegion->getHighestRow();
$highestColumnRegion = $objWorksheetRegion->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumnRegion);
for ($rowRegion = $startRow; $rowRegion <= $highestRowRegion; ++$rowRegion) {
$region_name = $objWorksheetRegion->getCellByColumnAndRow(0, $rowRegion)->getValue();
$region_id = $objWorksheetRegion->getCellByColumnAndRow(1, $rowRegion)->getValue();
// create a new root in the tree
$tree->createRoot($nodeRegion = $locationTable->createNode($region_name));
echo "\n\Process to Region: $region_name \n";
//province
$objPHPExcel->setActiveSheetIndex($indexProvince);
$objWorksheetProvince = $objPHPExcel->getActiveSheet();
$highestRowProvince = $objWorksheetProvince->getHighestRow();
$highestColumnProvince = $objWorksheetProvince->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumnProvince);
for ($rowProvince = $startRow; $rowProvince <= $highestRowProvince; ++$rowProvince) {
$province_name = $objWorksheetProvince->getCellByColumnAndRow(0, $rowProvince)->getValue();
$province_code = $objWorksheetProvince->getCellByColumnAndRow(1, $rowProvince)->getValue();
$province_region_id = $objWorksheetProvince->getCellByColumnAndRow(2, $rowProvince)->getValue();
$province_id = $objWorksheetProvince->getCellByColumnAndRow(3, $rowProvince)->getValue();
if ($province_region_id == $region_id) {
//create province node and append it to the region
$nodeProvince = $locationTable->createNode($province_name, $province_code);
$locationTable->appendNode($nodeProvince, $nodeRegion);
echo "Process to Province: $province_name \n";
//city
$objPHPExcel->setActiveSheetIndex($indexCity);
$objWorksheetCity = $objPHPExcel->getActiveSheet();
$highestRowCity = $objWorksheetCity->getHighestRow();
$highestColumnCity = $objWorksheetCity->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumnCity);
for ($rowCity = $startRow; $rowCity <= $highestRowCity; ++$rowCity) {
$city_name = $objWorksheetCity->getCellByColumnAndRow(0, $rowCity)->getValue();
$city_province_id = $objWorksheetCity->getCellByColumnAndRow(1, $rowCity)->getValue();
$city_region_id = $objWorksheetCity->getCellByColumnAndRow(2, $rowCity)->getValue();
if ($city_region_id == $province_region_id && $city_province_id == $province_id) {
//create province node and append it to the region
$nodeCity = $locationTable->createNode($city_name);
$locationTable->appendNode($nodeCity, $nodeProvince);
echo "Process to City: $city_name \n";
}
}
}
}
}
echo "Locations Salvate con Successo! \n\n\n";
}
和我的位置表:
/**
* LocationTable
*
* This class has been auto-generated by the Doctrine ORM Framework
*/
class LocationTable extends Doctrine_Table
{
const LEVEL_REGION = '0';
const LEVEL_PROVINCE = '1';
const LEVEL_CITY = '2';
/**
* Returns an instance of this class.
*
* @return object LocationTable
*/
public static function getInstance()
{
return Doctrine_Core::getTable('Location');
}
/**
* Developer MUST CALL $parent->refresh() before insert a new node, otherwise
* following insertions are wrong
*
* @param Location $son
* @param Location $parent
* @link http://trac.doctrine-project.org/ticket/928
*/
public function appendNode(Location $son, Location $parent)
{
// internally calls save both on parent and son
$son->getNode()->insertAsLastChildOf($parent);
// load new parent lft and rgt values
$parent->refresh();
}
/**
* Build a tree node, save it and return it
*
* @param string $label
* @param string $code
*
* @return Location
*/
public function createNode($label, $code = null)
{
$node = $this->create(array('name' => $label, 'code' => $code));
$node->save();
return $node;
}
}
我的学说模式:
Location:
tableName: locations
actAs:
NestedSet:
hasManyRoots: true
rootColumnName: root_id
columns:
id:
type: integer(4)
primary: true
autoincrement: true
name:
type: string(255)
notnull: true
code:
type: string(2)
问题在于,当我去检查给定节点时,我们检查一个省,如果我执行如下查询:
SELECT * FROM locations WHERE lft >= x AND rgt <= y
我找不到我希望找到的城市,但我发现了更多。
读取excel文件的scritp它完美运行。我打印并阅读所有输出并更正。 我认为lft和rgt值存在问题。 你知道你在文档中发现了一些不是prensa的错误吗?
答案 0 :(得分:0)
您的定义中设置了hasManyRoots
。这意味着,您的嵌套集表包含多个树。在每个树lft
和rgt
中构建独立于其他树,因此使用SELECT * FROM locations WHERE lft >= x AND rgt <= y
查询从多个树中检索节点。
您有两种方法可以解决它:快速和正确。
快速的一个:
每条记录都有一个root_id
(在rootColumnName
属性中设置,它也是默认值)列。此列包含树的根节点的id
。因此,如果您必须知道所需节点的root_id
并将其用作
SELECT * FROM locations WHERE lft >= x AND rgt <= y and root_id = rid
正确的:
不要直接查询数据库。从数据库中获取所需的记录作为对象(例如,使用DQL
),然后使用getChildren
或getDescendants
遍历它:
// Get the node into $provincevar
$all_cities_in_province = $province->getDescendants();
有关详细说明,请参阅NestedSet documentation。
还有一点注意:这可能是巧合,缺少缓存或其他东西,但看起来Doctrine会立即刷新NestedSet
节点操作函数所做的更改(例如createRoot
,{{1 },delete
等。但这是我在具有开发环境的单机上所做的观察。