Jump to content

ActiveRecord Improvements


CodingJungle

Recommended Posts

Posted

i would like to make 2 request for the ActiveRecord, first one, to have a static "all" method:

    public static function all(array $config = []): ActiveRecordIterator
    {
        $sql = Db::i()->select($config['columns']??'*', static::$databaseTable, $config['where'] ?? null, $config['order'] ?? null, $config['limit'] ?? null, $config['group']??null, $config['having']??null,$config['flags']??0 );
        return new ActiveRecordIterator( $sql, static::class);
    }

Reason: a lot of frameworks "model" class offers a similar method, like laravel. this would reduce down on code, and add an extra layer of abstraction.

second request would be a way to retrieve $_data with or without prefixes, for data store/caching reasons, as a lof of time, i will load the record to do the cache, so i can use bitwise or other methods inside the class to determine if it needs to be in the cache or where it belongs in the cache. so something like this:

    public function getData(bool $withPrefix=false):array
    {
        if( $withPrefix === false){
            return $this->_data;
        }

        $prefix = static::$databasePrefix;
        $new = [];
        foreach ($this->_data as $key => $val) {
            $new[ $prefix . $key ] = $val;
        }

        return $new;
    }

it would also help in situations, where i already have the record, have changed it and want to target the cache that needs to be updated. so there are a lot of uses for it imho.

Posted
13 hours ago, teraßyte said:

I actually implemented something similar to getData in a couple of my custom apps since I needed it exactly for caching. I totally support this.

i've been implementing both for quite awhile thru a trait that i add to my models/items/AR's, but it would be nice if this was apart of core ? 

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...