0

我一直在关注Silverstripe DataObjects as Pages - Part 2: Using Model Admin and URL Segments to create a product catalog tutorial on my localhost and running into a sidebar problem。

当我使用与教程一相同的方法创建侧边栏时,我的网站上显示一条错误消息 [User Error] Uncaught Exception: Object->__call(): the method 'categorypages' does not exist on 'Product'

这是我添加到 Product.php 以显示侧边栏的代码。

//Return the Title as a menu title
public function MenuTitle()
{
  return $this->Title;
}  

//确保DO显示在菜单中(需要它,否则未登录时侧边栏不显示)

function canView()
{
 return $this->CategoryPages()->canView();
}

有谁知道如何解决这个问题?非常感谢。

4

3 回答 3

2

你试过$this->Categories()->First()->canView()吗?阅读下面的评论,在我看来,您正在尝试在所有相关 CategoryPage 对象(ComponentSet)的列表中调用 canView

[编辑] 正如您在下面的评论中提到的,您现在在对非对象调用 canView 的 cms 中遇到错误。我的猜测是您尚未将任何类别附加到某些产品,因此 Categories()->First() 返回 NULL。请试试:

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;
  }
}

我真的不明白你为什么要实施这个 canView 检查。产品是否已经与某个类别相关真的很重要吗?否则,就return true;在你的 canView 方法中。

于 2012-01-23T19:56:13.567 回答
0

我自己没有尝试过,但是看看你应该改变的$Category = $this->CategoryPages()->First();评论$Category = $this->Categories()->First();

于 2012-01-23T00:13:50.643 回答
0

该错误会向我表明您的 Product 类上没有名为“CategoryPages”的 has_one 关系。本教程中的示例在 StaffMember 类上具有以下内容(注意 StaffPage 关系):

//Relations
static $has_one = array (
    'StaffPage' => 'StaffPage',
    'Photo' => 'Image'
);

这就是示例 canView 函数 ($this-> StaffPage ()) 中引用的内容:

function canView()
{
    return $this->StaffPage()->canView();
}

您的产品上是否有名为“CategoryPages”的等效关系?您需要正确指定与父级的关系。

于 2012-01-23T04:01:19.067 回答