码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-06-28 20:59:32      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:define   eof   ken   sed   cep   protected   upd   previous   col   

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巧用字段备注支持扩展

标签:define   eof   ken   sed   cep   protected   upd   previous   col   

原文地址:https://www.cnblogs.com/hnhycnlc888/p/11104650.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!