kohana orm巧用字段备注支持扩展

Posted hnhycnlc888

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了kohana orm巧用字段备注支持扩展相关的知识,希望对你有一定的参考价值。

1、SELECT * FROM `bota_language` WHERE `type` = ‘order_type‘;

    id  key     value      type       
------  ------  ---------  ------------
   446  1       原单         order_type 
   447  2       改签单        order_type 
   448  3       退票单        order_type 
   449  6       退款单        order_type 

 

2、SELECT `type`, GROUP_CONCAT(CONCAT_WS(‘->‘,`key`,`value`) ORDER BY `key` ASC SEPARATOR ‘||‘) FROM bota_language GROUP BY `type`;

order_type                1->原单||2->改签单||3->退票单||6->退款单                                                                                                                                                                                                                                                                                                                                                                                                                           
order_writeoff_status     1->审批中||2->已同意||3->已拒绝   

 

3、ORM代码:

<?php defined(‘SYSPATH‘) OR die(‘No direct script access.‘);

class ORM extends Kohana_ORM 

    /**
     * Model configuration, table names plural?
     * @var bool
     */
    protected $_table_names_plural = FALSE;

    /**
     * Creates and returns a new model.
     * Model name must be passed with its‘ original casing, e.g.
     *
     *    $orm = ORM::factory(‘User_Token‘);
     *
     * @chainable
     * @param   string  $orm  Model name
     * @param   mixed   $id     Parameter for find()
     * @return  ORM
     */
    public static function factory($orm, $id = NULL)
    
        // Set class name
        $orm = ‘ORM_‘.$orm;
    
        return new $orm($id);
    

    /**
     * Updates or Creates the record depending on loaded()
     *
     * @author Kumchoy
     * @since 20180712
     * @final 20180712
     *
     * @chainable
     * @param  Validation $validation Validation object
     * @return ORM
     */
    public function save(Validation $validation = NULL)
    
        try 
            //keyword字段拼接
            if (key_exists(‘keyword‘, $this->table_columns()) and
                (!$this->get(‘keyword‘) or preg_match(‘/^(autokw||)/‘, $this->get(‘keyword‘)))) 
                $comment = Arr::path($this->table_columns(), ‘keyword.comment‘);
                $cols = explode(‘||‘, preg_replace(‘/^(.*,)/‘, ‘‘, $comment));
                $database = Kohana::$config->load(‘database.‘ . Database::$default . ‘.connection.database‘);
                $keyword = DB::select(DB::expr("CONCAT(
                ‘CONCAT(\‘autokw||\‘,‘,
                REPLACE(
                    GROUP_CONCAT(
                        CONCAT(‘IFNULL(`‘,column_name,‘`,\‘\‘)‘) ORDER BY column_name SEPARATOR ‘||‘
                    )
                ,‘||‘,‘,\‘||\‘,‘)
                ,‘)‘) AS keyword"))
                    ->from(DB::expr(‘information_schema.columns‘))
                    ->where(‘table_schema‘, ‘=‘, $database)
                    ->where(‘table_name‘, ‘=‘, $this->_db->table_prefix() . $this->table_name())
                    ->where(‘column_name‘, ‘in‘, $cols)
                    ->cached(null)
                    ->execute()
                    ->get(‘keyword‘);
                if ($keyword) 
                    $this->set(‘keyword‘, DB::expr("$keyword"));
                
            
         catch (Exception $e) 
        
        parent::save($validation);

        if (isset($keyword)) 
            $_saved = $this->saved();
            $this->reload();
            $this->_loaded = $this->_saved = $_saved;
        

        return $this;
    
    

    protected $_previous_values = NULL;//cache previous values
    public function create(Validation $validation = NULL)
    
        $this->_previous_values = $this->_original_values;
        return parent::create($validation);
    
    public function update(Validation $validation = NULL)
    
        $this->_previous_values = $this->_original_values;
        return parent::update($validation);
    
    public function delete()
    
        $this->_previous_values = $this->_original_values;
        return parent::delete();
    
    public function previous_values()
    
        return $this->_previous_values;
    

 

4、keyword字段内容如:

autokw||上海浦东新区赵高公路1269号4楼||13455454556||老完||上海||上海广园客房部||7811920||||["userid":"\u5165\u4f4f","username2":"","mobile":"13455555555","idtype":"","userno":"","username":"\u5165\u4f4f","usertype":0,"_usertype":"\u6210\u4eba"]||1||||老完

 

5、soyoung项目注意事项

1、不能自拼sql操作数据库,select例外,但也建议用DB类组装sql执行;
2、insert,update,delete强制orm,原因orm save对keyword字段做了处理

以上是关于kohana orm巧用字段备注支持扩展的主要内容,如果未能解决你的问题,请参考以下文章

巧用 Base62 解决字段太短的问题

Kohana ORM,在模型中定义字段

无侵入!巧用logback扩展,见识它的强大

shell命令之巧用cut

开发中巧用Enum枚举类型——转自翘首以望

Kohana 3.2 中使用 ORM 模型(结果)的字段的自定义过滤器