gpt4 book ai didi

php - Doctrine2 + ZF2 - 迭代子类别

转载 作者:搜寻专家 更新时间:2023-10-31 22:06:22 24 4
gpt4 key购买 nike

我目前正在开发 ZF2 的博客模块,我找到了一种遍历我的类别并检查它们是否有子项的方法。

问题是我总是得到这个错误:

Fatal error: Call to a member function isEmpty() on a non-object in /var/www/microweb2/module/Blog/src/Blog/Entity/CategoryIterator.php on line 19

这是第 19 行:return $this->_data->current()->getSubCategories()->isEmpty();

嗯,目前,我的 CategoryIterator 是一个实现 \RecursiveIterator 的类,但它不起作用......我从这个博客中获取了这段代码:http://wildlyinaccurate.com/simple-nested-sets-in-doctrine-2

这是我的代码:

我的实体\类别:

<?php

namespace Blog\Entity;

use Doctrine\ORM\Mapping as ORM;
use \Application\Entity\AbstractEntity;
use Doctrine\Common\Collections\ArrayCollection;

/**
* @ORM\Entity
* @ORM\Table(name="blog_categories")
*/
class Category extends AbstractEntity {

// OTHER USELESS CODE

/**
* Set parent
*
* @param \Blog\Entity\Category $parent
* @return Category
*/
public function setParent(\Blog\Entity\Category $parent = null)
{
$this->parent = $parent;

return $this;
}

/**
* Get parent
*
* @return \Blog\Entity\Category
*/
public function getParent()
{
return $this->parent;
}


/**
* Add subcategories
*
* @param \Blog\Entity\Category $subcategories
* @return Category
*/
public function addSubCategory(\Blog\Entity\Category $subcategories)
{
$this->subcategories[] = $subcategories;

return $this;
}

/**
* Remove subcategories
*
* @param \Blog\Entity\Category $subcategories
*/
public function removeSubCategory(\Blog\Entity\Category $subcategories)
{
$this->subcategories->removeElement($subcategories);
}

/**
* Get subcategories
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getSubCategories()
{
return $this->subcategories;
}
}

我的CategoryIterator:

<?php

namespace Blog\Entity;

use Doctrine\Common\Collections\Collection;

class CategoryIterator implements \RecursiveIterator
{

private $_data;

public function __construct(Collection $data)
{
$this->_data = $data;
}

public function hasChildren()
{
return $this->_data->current()->getSubCategories()->isEmpty();
}

public function getChildren()
{
return new CategoryIterator($this->_data->current()->getSubCategories());
}

public function current()
{
return $this->_data->current();
}

public function next()
{
$this->_data->next();
}

public function key()
{
return $this->_data->key();
}

public function valid()
{
return $this->_data->current() instanceof \Blog\Entity\Category;
}

public function rewind()
{
$this->_data->first();
}

}

还有我的IndexController::indexAction():

public function indexAction()
{
$adapter = new SelectableAdapter($this->doctrine()->getRepository('Blog\Entity\Article'));
$paginator = new Paginator($adapter);
$paginator->setCurrentPageNumber($this->params()->fromQuery('page', 1))
->setItemCountPerPage(10);


$categories = $this->doctrine()->getRepository('Blog\Entity\Category')->findBy(array(), array('id' => 'ASC'));

$iterator = new RecursiveIteratorIterator(
new CategoryIterator(
new ArrayCollection($categories)
),
RecursiveIteratorIterator::SELF_FIRST
);

return new ViewModel(array(
'articles' => $paginator,
'categories' => $iterator,
));
}

别生气,我所有的use陈述都是正确的!

这是我使用 categories 变量的局部 View (一切都顺利通过,因为它在没有子类别的情况下工作......):

<h3>Catégories</h3>
<ul class="unstyled side-links">
<?php foreach ($this->categories as $index => $cat) : ?>
<?php
if ($cat->getParent() !== null) {
continue;
}

$class = '';
if (isset($this->current)) {
if ($this->current === $cat->getId()) {
$class = 'class="active"';
}
}
?>
<li <?= $class; ?>>
<a href="<?= $this->url('blog/category', array('alias' => $cat->getAlias())); ?>"><?= $cat->getName(); ?></a>
<?php if ($this->categories->hasChildren()) : ?>
<ul class="unstyled side-links">
<?php foreach ($this->categories->getChildren() as $scat) : ?>
<?php $class2 = ($scat->getId() === $this->current ? 'class="active"' : ''); ?>
<li <?= $class2; ?>>
<a href="<?= $this->url('blog/category', array('alias' => $scat->getAlias())); ?>"><?= $scat->getName(); ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>

因此,如果有人能告诉我为什么它对这个人有效但对我无效,那么,欢迎提出任何想法!

最佳答案

对于遇到相同问题的任何人 - 链接的帖子显示函数 hasChildren 略有不同。

public function hasChildren()
{
return ( ! $this->_data->current()->getChildCategories()->isEmpty());
}

添加后,迭代器会显示子元素。

关于php - Doctrine2 + ZF2 - 迭代子类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17893300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com