Custom fields in a Model?


#1

I was wondering if it’s possible to modify fields displayed in a Model, I’m using it to pull info from the db about logs, and it works fine for the most part, although I need to change something.
I have a field in my database that’s named ‘level’, which I want to use in the UI under a different name.
Said field also contains an integer value, and I’d like to be able to translate that value to text (like an enum, but so that the Model would see the integers instead of the strings).

I also need to create a field that’d contain a completely custom view (pure HTML is fine), because that’ll contain the user ID, and I’d like to change that to a url, the user name (so I need to be able to read it), an icon, etc.

How can I achieve this?

My current setup is like this

<?php
/**
 * Created by PhpStorm.
 * User: Pawel K. [pawel.k@icenter.pl]
 * Date: 20.11.2018
 * Time: 10:43
 */

namespace AAA\Module\Settings\Model;

use atk4\data\Model;
use AAA\Enum\Filter;

class Logs extends Model {
    public $table                       =   'logs';
    public $id_field                    =   'lgid';

    public function init() {
        $this->caption = self::ts("Logs");

        parent::init();

        $loggingChannels = [];
        $loggingChannels = apply_filter(Filter::LOGGING_CHANNELS_REGISTER, $loggingChannels);

        $this->addField('level', ['type' => 'integer', 'caption' => self::ts("Level")]);
        $this->addField('message', ['type' => 'string', 'caption' => self::ts("Message")]);
        $this->addField('channel', ['type' => 'enum', 'enum' => $loggingChannels, 'caption' => self::ts("Channel")]);
        $this->addField('remote_addr', ['type' => 'string', 'caption' => self::ts("IP Address")]);
        $this->addField('created_at', ['type' => 'datetime', 'caption' => self::ts("Date")]);
        $this->addField('created_by', ['type' => 'integer', 'caption' => self::ts("Created by")]);

        $this->setOrder('created_at', 'desc');
    }
}

#2

I would add another field in the model that does not exist in the DB table and set its never_persist property to true. Then calculate its test value according to the int value of the other field.