【问题标题】:Custom filter for a field using ORM Model (result) in Kohana 3.2Kohana 3.2 中使用 ORM 模型(结果)的字段的自定义过滤器
【发布时间】:2012-08-31 06:43:05
【问题描述】:

我想知道是否有办法从 ORM 中过滤结果。我希望为我的模型创建一个自定义方法,例如,用于过滤来自我的数据库的输出。目前,只能从表单中过滤 POST 中的数据以将它们保存到数据库中,但是,我想要相反。

如果我的表中有一个名为“标识”的字段,其值为“8-985-256”,我希望该值为“08-0985-00256”(不用担心如何添加额外的零,那部分很简单)。

假设我的模型上有一个名为“format_identification”的自定义方法。 然后,我使用 ORM 类获取数据,如下所示:

$user = ORM::factory('user', 1);

我想回显标识,但是格式正确:

echo $user->format_identification();

这应该打印“08-0985-00256”。如果我使用字段的名称,那可以,但是,值的格式不是。

我希望你明白我想要做什么。

感谢您的宝贵时间。

【问题讨论】:

    标签: php orm model filter kohana


    【解决方案1】:

    将公共方法添加到名为format_identificationModel_User 文件中,例如:

    <?php defined('SYSPATH') or die('No direct access allowed.');
    
    class Model_User extends ORM {
    
        public function format_identification() {
    
            $identification_value = $this->identification;
    
            // Add the extra zeros to value
    
            return $identification_value;
    
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      我认为有更好的方法来解决您的问题:

      class Model_User extends ORM {
      
          public function __get($column) {
              if($column=='identification'){
               // Do your stuff
              }
              return parent::__get($column);
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-18
        • 1970-01-01
        • 2017-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多