<i id='L7VVj'><tr id='L7VVj'><dt id='L7VVj'><q id='L7VVj'><span id='L7VVj'><b id='L7VVj'><form id='L7VVj'><ins id='L7VVj'></ins><ul id='L7VVj'></ul><sub id='L7VVj'></sub></form><legend id='L7VVj'></legend><bdo id='L7VVj'><pre id='L7VVj'><center id='L7VVj'></center></pre></bdo></b><th id='L7VVj'></th></span></q></dt></tr></i><div id='L7VVj'><tfoot id='L7VVj'></tfoot><dl id='L7VVj'><fieldset id='L7VVj'></fieldset></dl></div>

<small id='L7VVj'></small><noframes id='L7VVj'>

    1. <tfoot id='L7VVj'></tfoot>

        <legend id='L7VVj'><style id='L7VVj'><dir id='L7VVj'><q id='L7VVj'></q></dir></style></legend>

        • <bdo id='L7VVj'></bdo><ul id='L7VVj'></ul>

        Yii2 中的动态表名

        Dynamic table names in Yii2(Yii2 中的动态表名)
          <tbody id='HBYlr'></tbody>

            <tfoot id='HBYlr'></tfoot>
              • <bdo id='HBYlr'></bdo><ul id='HBYlr'></ul>

                • <i id='HBYlr'><tr id='HBYlr'><dt id='HBYlr'><q id='HBYlr'><span id='HBYlr'><b id='HBYlr'><form id='HBYlr'><ins id='HBYlr'></ins><ul id='HBYlr'></ul><sub id='HBYlr'></sub></form><legend id='HBYlr'></legend><bdo id='HBYlr'><pre id='HBYlr'><center id='HBYlr'></center></pre></bdo></b><th id='HBYlr'></th></span></q></dt></tr></i><div id='HBYlr'><tfoot id='HBYlr'></tfoot><dl id='HBYlr'><fieldset id='HBYlr'></fieldset></dl></div>

                • <small id='HBYlr'></small><noframes id='HBYlr'>

                  <legend id='HBYlr'><style id='HBYlr'><dir id='HBYlr'><q id='HBYlr'></q></dir></style></legend>
                  本文介绍了Yii2 中的动态表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 Yii2 模型,它使用多个具有相同结构的表.表名会根据登录的用户而改变,表名非常独特,取决于用户名.我将如何将此表名动态分配给模型?到目前为止我已经这样做了.

                  I have a Yii2 Model that uses multiple tables with the same structure. The table names will change according to the user logged in and the table names are very unique and depends on the username .How will i assign this table name to the model dynamically?I have done this so far.

                  在我的模型中:

                      protected $table;
                  
                      public function __construct($table)
                      {
                      $this->table='custom_risk_assignment_table';//logic to obtain the table name goes here
                      }
                  
                      public static function tableName()
                      {
                      return $this->table;
                      }
                  

                  但是这样做会导致错误Using $this when not in object context 因为function tableName() 是一个 static 函数.

                  But by doing so results in error Using $this when not in object context since function tableName() is a static function.

                  那我该怎么做?感谢任何帮助.提前致谢!

                  How can i do it then?Any help appreciated.Thanks in advance!

                  详细图片

                  假设我有一个来自 ABC 公司的用户.我的应用程序中有很多进程,说 PQR 就是其中之一.如果来自公司 ABC 的用户登录并选择进程 PQR,我需要在我的数据库中创建一个表 ABC_PQR(如果它不存在)或加载该表(如果它已经创建).我需要这个表名到我的模型中.同样,可能存在用户和许多进程.管理数据库的最佳方法是什么.

                  Say i have a user from the company ABC. I have so many processes in my application,say PQR is one of them. If user from the company ABC logins and choose the process PQR,i need to create a table ABC_PQR in my database if not exists or load the table if it is already created. I need this table name into my model. Similarly may users and many processes are there.What will be the best approach to manage the database.

                  推荐答案

                  由于 tableName 是一个静态方法(正是错误消息所说的),您无权访问非静态属性.(你不能使用 $this 关键字)

                  Since tableName is a static method (exactly what the error message says) you don't have access to non-static properties. (you can't use $this keyword)

                  所以你必须声明一个静态属性:

                  So you'd have to declare a static property:

                  protected static $table;
                  
                  public function __construct($table)
                  {
                      self::$table = $table;
                  }
                  
                  public static function tableName()
                  {
                      return self::$table;
                  }
                  
                  /* UPDATE */
                  public static function setTableName($table)
                  {
                      self::$table = $table;
                  }
                  

                  更新:

                  好吧,我的错.如果您调用诸如 updateAllfind 等静态方法,则不会调用构造函数.并且不会设置 $table.所以你有不同的选择.

                  Ok my fault. The constructor will not get called if you call a static method like updateAll, find etc. And $table will not get set. So you have different options.

                  1. 在使用静态数据库操作方法之前手动调用构造函数.
                  2. 向模型添加一个静态 setter,如 public static function setTableName($tableName),并在每次成功登录时调用它.
                  1. Manually call the constructor before using the static db manipulation methods.
                  2. Add a static setter to the model like public static function setTableName($tableName) and call it on every successful login.

                  这篇关于Yii2 中的动态表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is Joomla 2.5 much faster than Joomla 1.5 Querywise(Joomla 2.5 比 Joomla 1.5 Querywise 快得多吗)
                  How to share Joomla login session from one joomla website to one ASP.Net MVC website(如何将 Joomla 登录会话从一个 joomla 网站共享到一个 ASP.Net MVC 网站)
                  htaccess redirect root to subdirectory but allow index.php in root AND query strings to function(htaccess 将根重定向到子目录,但允许根和查询字符串中的 index.php 起作用)
                  Joomla include database functions(Joomla 包含数据库功能)
                  nl2br() not working when displaying SQL results(显示 SQL 结果时 nl2br() 不起作用)
                  Joomla 2.5 JFactory::getSession(); seems to be caching in firefox(Joomla 2.5 JFactory::getSession();似乎在 Firefox 中缓存)
                  <tfoot id='Vl5jg'></tfoot>
                    1. <legend id='Vl5jg'><style id='Vl5jg'><dir id='Vl5jg'><q id='Vl5jg'></q></dir></style></legend>
                        <tbody id='Vl5jg'></tbody>

                      <small id='Vl5jg'></small><noframes id='Vl5jg'>

                      <i id='Vl5jg'><tr id='Vl5jg'><dt id='Vl5jg'><q id='Vl5jg'><span id='Vl5jg'><b id='Vl5jg'><form id='Vl5jg'><ins id='Vl5jg'></ins><ul id='Vl5jg'></ul><sub id='Vl5jg'></sub></form><legend id='Vl5jg'></legend><bdo id='Vl5jg'><pre id='Vl5jg'><center id='Vl5jg'></center></pre></bdo></b><th id='Vl5jg'></th></span></q></dt></tr></i><div id='Vl5jg'><tfoot id='Vl5jg'></tfoot><dl id='Vl5jg'><fieldset id='Vl5jg'></fieldset></dl></div>
                        • <bdo id='Vl5jg'></bdo><ul id='Vl5jg'></ul>