【问题标题】:Silverstripe DataObjects as Pages Part 2 tutorial- sidebar issueSilverstripe DataObjects as Pages Part 2 tutorial-sidebar issue
【发布时间】:2012-01-22 22:22:19
【问题描述】:

我一直在我的本地主机上关注 Silverstripe DataObjects as Pages - Part 2: Using Model Admin and URL Segments to create a product catalogue tutorial 并遇到了侧边栏问题。

当我使用与tutorial one 相同的方法创建侧边栏时,我的网站上显示一条错误消息 [用户错误] 未捕获的异常:对象->__call():方法 'categorypages' 不存在于 'Pr​​oduct '

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

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

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

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

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

【问题讨论】:

    标签: php content-management-system silverstripe


    【解决方案1】:

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

    [编辑] 正如您在下面的 cmets 中提到的,您现在在对非对象调用 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 检查。产品是否已经与某个类别相关真的很重要吗?否则,只需在您的 canView 方法中使用 return true;

    【讨论】:

    • 您好 Schellmax,感谢您的快速响应。你可以看到我正在试图找出这个 SS。我将行更改为 $this->Categories->First()->canView() 该页面现在可以正常工作,但是在 CMS 管理员中,我单击产品,然后单击搜索(显示所有现有产品)它出现了致命的错误:在非对象上调用成员函数 canView() 对此有什么建议吗?多谢!山姆:)
    • 那么很可能您还没有附加任何类别页面。我将在上面的答案中添加一些解决方法
    • 谢谢 Schellmax,你是个传奇!我希望有一天我的编码技能会像你一样。我实现 canView 功能的原因是如果我在没有它的情况下退出 SS 管理部分,侧边栏就会消失。我在管理员中检查了每个产品,它们都附加了一个或多个类别,产品是否与某个类别相关并不重要。您能否将您的代码解释得更像它在做什么?对不起,我对 SS 很陌生。根据我的目的和你上面所说的,我想我应该只使用 function canView() { return true;非常感谢!!
    • 如果没有附加类别,调用 $this->Categories() 将返回 NULL,这可以解释为什么 $this->Categories()->canView 会产生错误(你不能调用 canView NULL 方法)。奇怪的是,您没有找到任何没有类别的产品,但您很好地使用了“return true”。
    • 谢谢!我将使用 return true,但只是出于兴趣,你能像每行在做什么一样为我解释上面的函数吗?我想了解和学习它。非常感谢您的所有帮助。 S:)
    【解决方案2】:

    我自己没试过,但是看一下cmets你应该把$Category = $this->CategoryPages()->First();改成$Category = $this->Categories()->First();

    【讨论】:

    • 您好 Xeraa,非常感谢您的回复。我看过评论,我使用的代码是 $Category = $this->Categories()->First();还有其他建议吗?多谢! :)
    【解决方案3】:

    该错误会向我表明您的 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”的等效关系?您需要正确指定与父级的关系。

    【讨论】:

    • 嗨 Shane,非常感谢您的回复,我已经在 Product.php 上定义了关系: static $belongs_many_many = array( 'Categories' => 'CategoryPage');在 CategoryPage.php 上: static $many_many = array('Products' => 'Product');你说得对,我需要改变 return $this->CategoryPages()->canView();返回 $this->Categories()->canView();正确引用它。但是现在我收到另一条错误消息:[用户错误]未捕获的异常:对象-> __call():方法'canview'在'ComponentSet'上不存在有关如何解决此问题的任何建议?谢谢谢恩。干杯,山姆:)
    猜你喜欢
    • 2013-11-02
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    • 2013-07-14
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多