TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
KO3.3 ORM my list_columns() to save unnecessarily DB queries
  • Hello, I have slightly improved ORM list_columns() method, which can save tens of DB queries and tenth of seconds... What is your opinion, is it good way? I want to report it as an enhancement...

    <?php
    
    defined('SYSPATH') OR die('No direct script access.');
    
    class ORM extends Kohana_ORM {
    
        /**
         * Proxy method to Database list_columns.
         *
         * <a href="/profile/return">@return array
         */
        public function list_columns() {
            if (!isset($this->_table_columns)) {
                // Proxy to database
                $this->_table_columns = $this->_db->list_columns($this->_table_name);
            }
    
            return $this->_table_columns;
        }
    
    }
    
    
  • why not just fill the $_table_columns ?

  • Because is it more laborious. When DB table schema is changed, I have to change my model.. List_colums does it for me and once.. I might be wrong, but till now I have the best experiences with it.

  • m.b. cache?

    public function list_columns()
    {
        if ( Cache::instance()->get( 'table_columns_' . $this->_object_name ) )
        {
            return Cache::instance()->get( 'table_columns_' . $this->_object_name );
        }
    
        Cache::instance()->set( 'table_columns_' . $this->_object_name, $this->_db->list_columns( $this->table_name() ) );
    
        // Proxy to database
        return $this->_db->list_columns( $this->table_name() );
    }
    
  • All right, cache is also solution...

  • Cache would be best, so long as one checks for it first.

    How would be cache be invalidated/refreshed (i.e. when the columns change)?

  • Currently I pre-generate the metadata at deployment after migrations are run. I dump the data using var_export() and stick it in a file which is then required by the bootstrap.

  • enable caching in PRODUCTION, disable in DEV mode

  • Honestly, I still don't understand why you don't just fill the $_table_columns like @nazarov suggested. If your database layout changes, you most likely have to change your code anyways, to update the rules(), some input fields and whatsoever...

    But if you insist, I'd go for the caching version, too.

  • I use caching for this because of a database that is accessed by multiple applications, and quite often modified for one of the non-Kohana applications (i.e. the Kohana application doesn't change when the DB changes).

  • Ok guys, I would again vote for my solution:

    1) do not need to care if cache is enabled or even exists
    2) so do not need to care if mode is PRODUCTION or DEV
    3) do not need to fill or have in mind my table scheme and update $_table_columns on every scheme change
    4) just simple "If condition" saves me a lot of DB queries and time..

    This was my proposal, of course is up to you how you will manage yours Model table columns.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion