【问题标题】:Functions of getparent and getChildrengetparent 和 getChildren 函数
【发布时间】:2014-01-17 20:30:13
【问题描述】:
  1. 这些 getParent() 和 getChildren() 有什么作用?这一切都与数据库中的父子表关系有关吗?如果是这样,我应该在 Zend_Model 或 Zend_Db_Table 中将这些 - parent_type 和 parent_id 放在哪里?

  2. 如何在控制器中调用 getParent()/getChildren() 以及它们将返回什么。

  3. 我可以对表关系使用 Zend 常规约定吗?

【问题讨论】:

    标签: php socialengine


    【解决方案1】:

    我已经想通了。我发现getParent(<itemType>)getChildren(<itemType>) 返回Core_Model_Item_Abstract。但是您必须分别在子表模型和父表模型中设置$_parent_type$_children_types[array](而不是在_model_dbtable 中)。

    您还必须在子表model(_Model_DbTable_) 中创建一个getChildrenSelectOfItemType($obj,$params = array(),它返回一个Zend_Db_Select,您必须定义此选择将返回什么。

    这里 ItemType 在 SE 中的意思是 moduleName_itemname(例如 album_photo )。

    假设帐户和错误具有 1:n 关系,因此 MyModule_Model_DbTable_Bugs 中必须包含以下内容。

    public function getChildrenSelectOfMyModuleAccount($obj,$params = array())
        {
    
        $select = $this->select();
        $select ->where('account_id = ?', $obj->getIdentity());
    
        return $select;
        }
    
    }
    

    然后您可以通过(在您的控制器中)获取相关的孩子 $account=Engine_Api::_()->getItem('mymodule_account', 1); //1 = matching account_id
    $bug=$account->getChildren('mymodule_bug'); //return Core_Model_Item_Abstract or row obj
    .

    获取父表行不太复杂,在子表中声明$_parent_type,并且数据库子表中必须存在 parent_id 列 - 错误。

    $bug=Engine_Api::_()->getItem('mymodule_bug', 1); //1 = bug_id $account=$bug->getParent();** //return Core_Model_Item_Abstract/row obj(不要忘记在 db bugs 表中添加一个引用列(parent_id)并匹配 id)

    如果您在 MyModule_Model_Bug 中设置“$_parent_is_owner=true”(不声明 $_owner_type 和 $_parent_type),用户将成为父母,因此 getParent 将返回用户

    最重要的一点是

    主题、查看者和所有者

    在社交引擎环境中...

    查看者 = 正在查看页面的任何人(主要是登录用户)。例如John 是一个登录用户,他正在查看照片 - 一个主题。

    主题= 主题被查看者查看和/或查看者实际查看主题。例如如果用户 John 查看相册/照片,则相册/照片是主题。

    所有者=所有者拥有项目/主题。例如John 是他的照片(主题)的所有者。如果 John 正在查看 Jerry 的照片,则主题是 Jerry 的照片,但所有者是 Jerry。

    【讨论】:

      【解决方案2】:

      1) 是的,它们用于父子关系,但不是通过 Zend 原生机制(_dependentTables、_referenceMap 等)

      您可以在此处找到这些方法:抽象类 Core_Model_Item_Abstract extends Engine_Db_Table_Row implements Core_Model_Item_Interface (application/modules/Core/Model/Item/Abstract.php)

      寻找方法参数,它们很重要。

      2) 通常他们用于检索数据库行/行。例如查看 Group_PhotoController::editAction():

      // 通过 id (engine4_group_photos.photo_id) 获取照片

      $photo = Engine_Api::_()->core()->getSubject();
      

      // 通过 group_id (engine4_group_photos.group.id) 查找父节点

      $group = $photo->getParent('group');
      

      我们在这里得到组行对象(Group_Model_Group)。

      您当然可以覆盖这些方法。 例如,请参见此处:class Album_Model_Photo extends Core_Model_Item_Abstract::getParent($type = null)

      3) 您可以尝试,但可能会很棘手,因为 SE 与 Zend 框架代码库发生了很大变化。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-04
        • 1970-01-01
        • 1970-01-01
        • 2016-05-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多