Silverstripe DataObjects作为页面第2部分教程侧边栏问题 [英] Silverstripe DataObjects as Pages Part 2 tutorial- sidebar issue
问题描述
我一直在关注 Silverstripe DataObjects作为页面 - 第2部分:使用模型管理和URL段在我的本地主机上创建产品目录教程并运行到侧边栏问题。
I've been following Silverstripe DataObjects as Pages - Part 2: Using Model Admin and URL Segments to create a product catalogue tutorial on my localhost and running into a sidebar problem.
当我使用相同的方法创建侧边栏 __ call():方法'categoritages '不存在于'Product'上
When I use the same method to create a sidebar as tutorial one, an error message shows on my site [User Error] Uncaught Exception: Object->__call(): the method 'categorypages' does not exist on 'Product'
这是我添加到Product.php中用于显示侧边栏的代码。
This is the code I added to Product.php for sidebar to appear.
//Return the Title as a menu title
public function MenuTitle()
{
return $this->Title;
}
//确保DO显示在菜单中未登录时不显示)
//Ensure that the DO shows up in menu (it is needed otherwise sidebar doesn't show when not logged in)
function canView()
{
return $this->CategoryPages()->canView();
}
有人知道如何解决这个问题吗?非常感谢。
Does anyone know how to fix this problem? Thanks very much.
推荐答案
您尝试过 $ this-> Categories () - > canView()
?
阅读下面的注释在我看来你正试图在所有相关的CategoryPage对象(ComponentSet)的列表中调用canView
have you tried $this->Categories()->First()->canView()
?
reading the comments below it seems to me you're trying to call canView on the list of all of your related CategoryPage objects (ComponentSet)
如你在下面的注释中提到的,你会得到一个错误现在在cms调用canView非对象。我的猜测是你还没有附加任何类别到某些产品,因此Categories() - > First()返回NULL。请尝试:
as you mentioned in the comments below, you get an error now in the cms calling canView on a non-object. my guess is you haven't attached any Categories yet to some Product, therefore Categories()->First() returns NULL. please try:
function canView() {
//always show this product for users with full administrative rights (see tab 'Security' in CMS
if(Permission::check('ADMIN')) return true;
//go and get all categories this product belongs to
$categories = $this->Categories();
//are there any categories?
if($categories->Count() > 0) {
//get the first category to see wheter it's viewable by the current user
return $categories->First()->canView();
} else {
//product doesn't belong to any categories, so don't render it
return false;
}
}
i你真的不知道为什么你实现了这个canView检查是真的很重要的一个产品已经与一个类别相关联,否则,只要 return true;
在你的canView方法。
i don't really get though why you implemented this canView check. does it really matter wheter a Product is already related to a category? otherwise, just return true;
in your canView method.
这篇关于Silverstripe DataObjects作为页面第2部分教程侧边栏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!