CRUD

CREATE READ UPDATE DELETE

Interacting with persistent data (usually through a database) is the likely the most common task for any backend. To simplify this process as far as possible, model interactions make use of a abstraction level called "transformer". This layer uses the model definition of your migration in order to generate database transactions. On this page, we will assume the use of the default MySQL providers.

Magic methods


        public static function __callStatic($method, $args)
        {
            if(!method_exists(self::class, $method)){
                $transform = new Transform('user', self::$db);
                return $transform->$method(...$args);
            } else {
                return self::$method(...$args);
            }
        }
    

When generating a model via the cli-tool, the methods __callStatic and init are generated. While the init function is used to assign providers (usually database injection), __callStatic uses the transformer if no own method is implemented. The transformer handles the following methods:

get(string $id)
Retrieve (read) a entity by primary key (id).

create(array $model)
Create a new entity validated against the migration. This method returns the created entity.

find(array $condition)
Retrieve multiple results based on a condition array (per default in accordance with the DatabaseWrapper).

update(array $model)
Update an existing entity after modification. This method returns the updated entity.

delete(string $id, $hard = false)
Sets a delete stamp or completely removes an entity from the database (when $hard is set to true).

Manipulating behavior

In some instances we might want to manipulate what is retrieved from or goes into the transformer logic. In the following example, let's assume we are dealing with a user model and do not want to retrieve the password.


        public static function __callStatic($method, $args)
        {
            if(!method_exists(self::class, $method)){
                $transform = new Transform('user', self::$db);
                // we are calling our own function "out"
                return self::out($transform->$method(...$args));
            } else {
                return self::$method(...$args);
            }
        }

        private static function out($result)
        {
            // We have to account for two scenarios: a single entry is retrieved
            // or multiple entries are retrieved
            if(isset($result['id'])){
                // single (e.g. via get())
                // unset the password
                unset($result['password']);
            } else {
                // multiple entries
                foreach ($result as $i => $hit){
                    // unset each password
                    unset($result[$i]['password']);
                }
            }
            return $result;
        }
    

Using models in controllers

Since the introduction of providers, decoupling will allow easy testing. Although you can use & call the models in various ways, we here assume utilization of the core's loadModel functionality.


        ...
        // in a Unicore scenario
        $this
            ->uni('demo')
            ->addRenderParameter('users', function(\Neoan3\Core\Serve $serve) {
                return $serve->loadModel(\Neoan3\Model\User\UserModel::class)::find(['name' => 'amadeus']);
            })
            ->hook('main', 'profile')
            ->output();
        ...
        // in an API scenario
        return $this->loadModel(\Neoan3\Model\User\UserModel::class)::find(['name' => 'amadeus']);