【问题标题】:Protected variables on zend framework dbzend 框架数据库上的受保护变量
【发布时间】:2019-07-20 09:25:31
【问题描述】:

我希望能够分离我的数据库表,因为我不希望表变得太大。

当一家公司在我的网站上注册时,我希望他们使用自己的数据库空间。所以在 zend 中,我有连接到数据库的模型文件,但它不允许我使其动态化。请有人帮我..

db model file to connect to departments that belong to a company

public function company_id(){
  $auth = Zend_Auth::getInstance();
  $company_id = $auth->getIdentity()->comp;
  return $company_id;
}

protected $_comp = $this->company_id();
//$name = '2_departments';
protected $_name = $this->_comp."_departments";
protected $_rowClass = 'Application_Model_Department';

当我使用上面的代码时,它会崩溃而没有错误。但是如果手动编码并为每个 db 表创建一个 php 文件,它将工作文件。

manual entry

protected $_name = "2_departments";
protected $_rowClass = 'Application_Model_Department';

如果有人可以分配那将是很棒的。

我还有一个例子

主类

类 Application_Model_Table_Departments 扩展了 Teabag_Db_Table {

 public function fetchdepartments() {
       $auth = Zend_Auth::getInstance();
       $company_id = $auth->getIdentity()->comp;

       $select = $this->select()
                      ->from(array('deps'=>$this->_name), array('*'))
                      ->where('deps.is_deleted = 0');

       return $this->fetchAll($select);
   }

}

但是我用另外两个文件扩展了这个类 custom1department.php custom2department.php

类 Application_Model_Table_Custom1Departments 扩展 Application_Model_Table_Departments {

protected $_name = "1_departments";
protected $_rowClass = 'Application_Model_Department';

}

类 Application_Model_Table_Custom2Departments 扩展 Application_Model_Table_Departments {

protected $_name = "2_departments";
protected $_rowClass = 'Application_Model_Department';

}

关于如何使类动态和类名动态化的任何想法?

【问题讨论】:

  • 请发布代码而不是代码图片。
  • 对不起,请稍等
  • 您还在使用 Zend-Framework version1 吗? (“Zend_Auth”似乎是 ZF1。)这可能是您的代码在使用 PHP7 时不起作用的原因。此外:如果您还在使用 ZF1,这将存在很大的安全隐患,因为它很早就达到了 EOL。
  • 哦,废话,它们是更新到较新 zend 的简单方法吗?
  • 迁移可能相当麻烦。如果可能,我建议一起启动一个新的 ZF3-Application,然后尝试手动传输一些您自己的代码。启动new ZF3-App 既简单又直接。如果您决定仍要迁移整个 ZF1 应用程序:This answer 可能会有所帮助。但是,这是有关如何将 ZF1 迁移到 ZF2 的指南。目前ZF3是最新版本,应该使用。

标签: zend-framework php-7 zend-db


【解决方案1】:

您可以考虑在部门表中使用 company_id 字段来跟踪每个公司的部门。这样,您将不需要任何动态表生成功能。

此外,您应该尝试将 company_id 作为参数传递给方法/函数,而不是获取 zend-auth 实例,以便获取登录角色的公司 ID。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-16
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    相关资源
    最近更新 更多