从数据库表

时间:2017-01-11 11:56:30

标签: php mysql symfony orm doctrine

嗨,拜托。我有这张表

Database table view

我需要在symfony中从这个表构建php数组。我有一个代码,但它只构建了2级...我如何编辑我的函数以获得完整的数组?谢谢!

我的功能

  function buildCategoryTree(Category $parentCategory = null, Eshop $eshop)
{
    $categories = $this->getEntityManager()->getRepository("AppBundle:Category")->findBy(array("parentCategory" => $parentCategory, "eshop" => $eshop));

    foreach ($categories as $category) {

        $url = $eshop->getHomepageOriginal() . $category->getLink();
        $arr = ["id" => $category->getId(), "name" => $category->getName(), "url" => $url, "active" => $category->getActive(), "last_http_status" => $category->getLastHttpStatusCode(), "child" => []];

        if (!$category->getParentCategory()) {

            $this->eshopCategoryTree[$category->getId()] = $arr;

        } else {

            $key = array_keys($this->eshopCategoryTree);
            $key = end($key);
            $this->eshopCategoryTree[$key]['children'][] = $arr;

        }

        $this->buildCategoryTree($category, $eshop);
    }
}

2 个答案:

答案 0 :(得分:0)

非常感谢。表转储

CREATE TABLE `category` (
  `id` int(11) NOT NULL,
  `parent_id` int(11) DEFAULT NULL,
  `eshop_id` int(11) DEFAULT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `link` longtext COLLATE utf8_unicode_ci NOT NULL,
  `created_at` datetime NOT NULL,
  `last_check_at` datetime NOT NULL,
  `last_http_status_code` int(11) NOT NULL DEFAULT '200',
  `active` tinyint(1) NOT NULL DEFAULT '1',
  `leaf` tinyint(1) NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO `category` (`id`, `parent_id`, `eshop_id`, `name`, `link`, `created_at`, `last_check_at`, `last_http_status_code`, `active`, `leaf`) VALUES
(1, NULL, 1, 'Potraviny', '/kategorie/430-Potraviny/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 0),
(2, 1, 1, 'Mléčné výrobky', '/kategorie/431-Potraviny-Mlecne-vyrobky/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 0),
(3, 2, 1, 'Mléko', '/kategorie/432-Potraviny-Mlecne-vyrobky-Mleko/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 0),
(4, 3, 1, 'Mléko čerstvé', '/kategorie/433-Potraviny-Mlecne-vyrobky-Mleko-Mleko-cerstve/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(5, 3, 1, 'Mléko trvanlivé', '/kategorie/434-Potraviny-Mlecne-vyrobky-Mleko-Mleko-trvanlive/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(6, 3, 1, 'Mléko kondenzované', '/kategorie/435-Potraviny-Mlecne-vyrobky-Mleko-Mleko-kondenzovane/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(7, 3, 1, 'Mléko sušené', '/kategorie/436-Potraviny-Mlecne-vyrobky-Mleko-Mleko-susene/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(8, 3, 1, 'Mléčné nápoje', '/kategorie/437-Potraviny-Mlecne-vyrobky-Mleko-Mlecne-napoje/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(9, 3, 1, 'Tvarohy', '/kategorie/438-Potraviny-Mlecne-vyrobky-Tvarohy/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 0),
(10, 9, 1, 'Tvarohy neochucené', '/kategorie/439-Potraviny-Mlecne-vyrobky-Tvarohy-Tvarohy-neochucene/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(11, 9, 1, 'Tvarohy ochucené', '/kategorie/440-Potraviny-Mlecne-vyrobky-Tvarohy-Tvarohy-ochucene/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(12, 9, 1, 'Tvarohové dezerty', '/kategorie/441-Potraviny-Mlecne-vyrobky-Tvarohy-Tvarohove-dezerty/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(13, 9, 1, 'Tvarohové krémy a pomazánky', '/kategorie/442-Potraviny-Mlecne-vyrobky-Tvarohy-Tvarohove-kremy-a-pomazanky/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(14, 9, 1, 'Mléčné dezerty pudinky,rýže', '/kategorie/443-Potraviny-Mlecne-vyrobky-Mlecne-dezerty-pudinky-ryze/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 0),
(15, 14, 1, 'Mléčné dezerty', '/kategorie/444-Potraviny-Mlecne-vyrobky-Mlecne-dezerty-pudinky-ryze-Mlecne-dezerty/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(16, 14, 1, 'Mléčné pudinky', '/kategorie/445-Potraviny-Mlecne-vyrobky-Mlecne-dezerty-pudinky-ryze-Mlecne-pudinky/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(17, 14, 1, 'Mléčné rýže a krupice', '/kategorie/446-Potraviny-Mlecne-vyrobky-Mlecne-dezerty-pudinky-ryze-Mlecne-ryze-a-krupice/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(18, 14, 1, 'Mléčné výrobky vaření a pečení', '/kategorie/46059-Potraviny-Mlecne-vyrobky-Mlecne-dezerty-pudinky-ryze-Mlecne-vyrobky-vareni-a-peceni/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(19, 14, 1, 'Jogurty', '/kategorie/447-Potraviny-Mlecne-vyrobky-Jogurty/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 0),
(20, 19, 1, 'Jogurty bílé', '/kategorie/448-Potraviny-Mlecne-vyrobky-Jogurty-Jogurty-bile/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(21, 19, 1, 'Jogurty ovocné', '/kategorie/449-Potraviny-Mlecne-vyrobky-Jogurty-Jogurty-ovocne/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(22, 19, 1, 'Smetany živočišné a rostlinné', '/kategorie/451-Potraviny-Mlecne-vyrobky-Smetany-zivocisne-a-rostlinne/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 0),
(23, 22, 1, 'Smetany a šlehačky čerstvé a zakysané', '/kategorie/452-Potraviny-Mlecne-vyrobky-Smetany-zivocisne-a-rostlinne-Smetany-a-slehacky-cerstve-a-zakysane/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(24, 22, 1, 'Smetany a šlehačky trvanlivé', '/kategorie/453-Potraviny-Mlecne-vyrobky-Smetany-zivocisne-a-rostlinne-Smetany-a-slehacky-trvanlive/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(25, 22, 1, 'Smetany sušené', '/kategorie/454-Potraviny-Mlecne-vyrobky-Smetany-zivocisne-a-rostlinne-Smetany-susene/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(26, 22, 1, 'Sýry tavené a čerstvé', '/kategorie/455-Potraviny-Mlecne-vyrobky-Syry-tavene-a-cerstve/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 0),
(27, 26, 1, 'Sýry tavené', '/kategorie/456-Potraviny-Mlecne-vyrobky-Syry-tavene-a-cerstve-Syry-tavene/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(28, 26, 1, 'Sýry tavené plátky', '/kategorie/457-Potraviny-Mlecne-vyrobky-Syry-tavene-a-cerstve-Syry-tavene-platky/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(29, 26, 1, 'Sýry pomazánky', '/kategorie/458-Potraviny-Mlecne-vyrobky-Syry-tavene-a-cerstve-Syry-pomazanky/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(30, 26, 1, 'Sýry čerstvé', '/kategorie/459-Potraviny-Mlecne-vyrobky-Syry-tavene-a-cerstve-Syry-cerstve/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(31, 26, 1, 'Sýry termizované', '/kategorie/460-Potraviny-Mlecne-vyrobky-Syry-tavene-a-cerstve-Syry-termizovane/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(32, 26, 1, 'Sýry přírodní', '/kategorie/461-Potraviny-Mlecne-vyrobky-Syry-prirodni/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 0),
(33, 32, 1, 'Sýry přírodní-cihly,výkroje a bochníky', '/kategorie/462-Potraviny-Mlecne-vyrobky-Syry-prirodni-Syry-prirodni-cihly-vykroje-a-bochniky/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(34, 32, 1, 'Sýry přírodní- plátky a bločky', '/kategorie/463-Potraviny-Mlecne-vyrobky-Syry-prirodni-Syry-prirodni-platky-a-blocky/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(35, 32, 1, 'Sýry strouhané', '/kategorie/464-Potraviny-Mlecne-vyrobky-Syry-prirodni-Syry-strouhane/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(36, 32, 1, 'Sýry zrající', '/kategorie/465-Potraviny-Mlecne-vyrobky-Syry-prirodni-Syry-zrajici/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(37, 32, 1, 'Sýry balkánského typu', '/kategorie/466-Potraviny-Mlecne-vyrobky-Syry-prirodni-Syry-balkanskeho-typu/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(38, 32, 1, 'Sýry plísňové', '/kategorie/467-Potraviny-Mlecne-vyrobky-Syry-prirodni-Syry-plisnove/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(39, 32, 1, 'Sýrové speciality', '/kategorie/468-Potraviny-Mlecne-vyrobky-Syry-prirodni-Syrove-speciality/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 1),
(40, 32, 1, 'Másla,margariny,tuky a oleje', '/kategorie/471-Potraviny-Masla-margariny-tuky-a-oleje/index.htm', '2017-01-11 11:21:47', '2017-01-11 11:21:47', 200, 1, 0);

答案 1 :(得分:0)

这是对您的代码的修复,使其正常工作。

function main()
{
    // This is just a dummy function showing how you should use the buildCategoryTree() method
    $this->eshopCategoryTree = $this->buildCategoryTree(null, $this->eshop);
}

private function buildCategoryTree(Category $parentCategory = null, Eshop $eshop)
{
    $categories = $this->getEntityManager()->getRepository("AppBundle:Category")->findBy(array("parentCategory" => $parentCategory, "eshop" => $eshop));

    $arr = [];

    foreach ($categories as $category) {

        $url = $eshop->getHomepageOriginal() . $category->getLink();

        $arr[$category->getId()] = [
            "id" => $category->getId(),
            "name" => $category->getName(),
            "url" => $url,
            "active" => $category->getActive(),
            "last_http_status" => $category->getLastHttpStatusCode(),
            "child" => $this->buildCategoryTree($category->getId(), $eshop)
        ];
    }

    return $arr;
}

您尝试构建树的方式有很多问题。

对于每个父类别,您都会对数据库执行SQL请求。那是浪费资源。做一个请求然后在内存中迭代结果会更合理。只要这是一个类别列表,它就不会太大。

您需要迭代所有内容的另一个问题。如果您使用例如https://github.com/Atlantic18/DoctrineExtensions/blob/v2.4.x/doc/tree.md并重新组织您的数据库,您将能够更快地构建树。

另一个小问题是您生成网址的方式。最好将其移至EshopCategory,方法如下(如果您将方法放入Category

public function getUrl(Eshop $eshop) { 
    return $eshop->getHomepageOriginal() . $this->getLink();
}