---
title: "Phalcon Mvc"
version: "5.6"
---

> Documentation Index
> Fetch the complete documentation index at: https://docs.phalcon.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Phalcon Mvc

:::info[NOTE]
All classes are prefixed with `Phalcon`
:::

## Mvc\Application 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Application.zep)

-   __Namespace__

    - `Phalcon\Mvc`

-   __Uses__

    - `Closure`
    - `Phalcon\Application\AbstractApplication`
    - `Phalcon\Di\DiInterface`
    - `Phalcon\Events\ManagerInterface`
    - `Phalcon\Http\ResponseInterface`
    - `Phalcon\Mvc\Application\Exception`
    - `Phalcon\Mvc\ModuleDefinitionInterface`
    - `Phalcon\Mvc\Router\RouteInterface`

-   __Extends__

    `AbstractApplication`

-   __Implements__

Phalcon\Mvc\Application

This component encapsulates all the complex operations behind instantiating
every component needed and integrating it with the rest to allow the MVC
pattern to operate as desired.

```php
use Phalcon\Mvc\Application;

class MyApp extends Application
{
Register the services here to make them general or register
in the ModuleDefinition to make them module-specific
\/
protected function registerServices()
{

}

This method registers all the modules in the application
\/
public function main()
{
    $this->registerModules(
        [
            "frontend" => [
                "className" => "Multiple\\Frontend\\Module",
                "path"      => "../apps/frontend/Module.php",
            ],
            "backend" => [
                "className" => "Multiple\\Backend\\Module",
                "path"      => "../apps/backend/Module.php",
            ],
        ]
    );
}
}

$application = new MyApp();

$application->main();
```

### Properties
```php
/**
 * @var bool
 */
protected $implicitView = true;

/**
 * @var bool
 */
protected $sendCookies = true;

/**
 * @var bool
 */
protected $sendHeaders = true;

```

### Methods

```php
public function handle( string $uri ): ResponseInterface | bool;
```
Handles a MVC request

```php
public function sendCookiesOnHandleRequest( bool $sendCookies ): Application;
```
Enables or disables sending cookies by each request handling

```php
public function sendHeadersOnHandleRequest( bool $sendHeaders ): Application;
```
Enables or disables sending headers by each request handling

```php
public function useImplicitView( bool $implicitView ): Application;
```
By default. The view is implicitly buffering all the output
You can full disable the view component using this method

## Mvc\Application\Exception 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Application/Exception.zep)

-   __Namespace__

    - `Phalcon\Mvc\Application`

-   __Uses__

-   __Extends__

    `\Phalcon\Application\Exception`

-   __Implements__

Phalcon\Mvc\Application\Exception

Exceptions thrown in Phalcon\Mvc\Application class will use this class

## Mvc\Controller ![Abstract](/assets/images/abstract-green.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Controller.zep)

-   __Namespace__

    - `Phalcon\Mvc`

-   __Uses__

    - `Phalcon\Di\Injectable`

-   __Extends__

    `Injectable`

-   __Implements__

    - `ControllerInterface`

Phalcon\Mvc\Controller

Every application controller should extend this class that encapsulates all
the controller functionality

The controllers provide the “flow” between models and views. Controllers are
responsible for processing the incoming requests from the web browser,
interrogating the models for data, and passing that data on to the views for
presentation.

```php
<?php

class PeopleController extends \Phalcon\Mvc\Controller
{
// This action will be executed by default
public function indexAction()
{

}

public function findAction()
{

}

public function saveAction()
{
    // Forwards flow to the index action
    $this->dispatcher->forward(
        [
            "controller" => "people",
            "action"     => "index",
        ]
    );
}
}
```

### Methods

```php
final public function __construct();
```
Phalcon\Mvc\Controller constructor

## Mvc\Controller\BindModelInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Controller/BindModelInterface.zep)

-   __Namespace__

    - `Phalcon\Mvc\Controller`

-   __Uses__

-   __Extends__

-   __Implements__

Phalcon\Mvc\Controller\BindModelInterface

Interface for Phalcon\Mvc\Controller

### Methods

```php
public static function getModelName(): string;
```
Return the model name associated with this controller

## Mvc\ControllerInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/ControllerInterface.zep)

-   __Namespace__

    - `Phalcon\Mvc`

-   __Uses__

-   __Extends__

-   __Implements__

Phalcon\Mvc\ControllerInterface

Interface for controller handlers

## Mvc\Dispatcher 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Dispatcher.zep)

-   __Namespace__

    - `Phalcon\Mvc`

-   __Uses__

    - `Phalcon\Dispatcher\AbstractDispatcher`
    - `Phalcon\Events\ManagerInterface`
    - `Phalcon\Http\ResponseInterface`
    - `Phalcon\Mvc\Dispatcher\Exception`

-   __Extends__

    `BaseDispatcher`

-   __Implements__

    - `DispatcherInterface`

Dispatching is the process of taking the request object, extracting the
module name, controller name, action name, and optional parameters contained
in it, and then instantiating a controller and calling an action of that
controller.

```php
$di = new \Phalcon\Di\Di();

$dispatcher = new \Phalcon\Mvc\Dispatcher();

$dispatcher->setDI($di);

$dispatcher->setControllerName("posts");
$dispatcher->setActionName("index");
$dispatcher->setParams([]);

$controller = $dispatcher->dispatch();
```

### Properties
```php
//
protected $defaultAction = index;

//
protected $defaultHandler = index;

//
protected $handlerSuffix = Controller;

```

### Methods

```php
public function forward( array $forward ): void;
```
Forwards the execution flow to another controller/action.

```php
use Phalcon\Events\Event;
use Phalcon\Mvc\Dispatcher;
use App\Backend\Bootstrap as Backend;
use App\Frontend\Bootstrap as Frontend;

// Registering modules
$modules = [
"frontend" => [
    "className" => Frontend::class,
    "path"      => __DIR__ . "/app/Modules/Frontend/Bootstrap.php",
    "metadata"  => [
        "controllersNamespace" => "App\Frontend\Controllers",
    ],
],
"backend" => [
    "className" => Backend::class,
    "path"      => __DIR__ . "/app/Modules/Backend/Bootstrap.php",
    "metadata"  => [
        "controllersNamespace" => "App\Backend\Controllers",
    ],
],
];

$application->registerModules($modules);

// Setting beforeForward listener
$eventsManager  = $di->getShared("eventsManager");

$eventsManager->attach(
"dispatch:beforeForward",
function(Event $event, Dispatcher $dispatcher, array $forward) use ($modules) {
    $metadata = $modules[$forward["module"]]["metadata"];

    $dispatcher->setModuleName(
        $forward["module"]
    );

    $dispatcher->setNamespaceName(
        $metadata["controllersNamespace"]
    );
}
);

// Forward
$this->dispatcher->forward(
[
    "module"     => "backend",
    "controller" => "posts",
    "action"     => "index",
]
);
```

```php
public function getActiveController(): ControllerInterface;
```
Returns the active controller in the dispatcher

```php
public function getControllerClass(): string;
```
Possible controller class name that will be located to dispatch the
request

```php
public function getControllerName(): string;
```
Gets last dispatched controller name

```php
public function getLastController(): ControllerInterface;
```
Returns the latest dispatched controller

```php
public function getPreviousActionName(): string;
```
Gets previous dispatched action name

```php
public function getPreviousControllerName(): string;
```
Gets previous dispatched controller name

```php
public function getPreviousNamespaceName(): string;
```
Gets previous dispatched namespace name

```php
public function setControllerName( string $controllerName );
```
Sets the controller name to be dispatched

```php
public function setControllerSuffix( string $controllerSuffix );
```
Sets the default controller suffix

```php
public function setDefaultController( string $controllerName );
```
Sets the default controller name

```php
protected function handleException( \Exception $exception );
```
Handles a user exception

```php
protected function throwDispatchException( string $message, int $exceptionCode = int );
```
Throws an internal exception

## Mvc\Dispatcher\Exception 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Dispatcher/Exception.zep)

-   __Namespace__

    - `Phalcon\Mvc\Dispatcher`

-   __Uses__

-   __Extends__

    `\Phalcon\Dispatcher\Exception`

-   __Implements__

Phalcon\Mvc\Dispatcher\Exception

Exceptions thrown in Phalcon\Mvc\Dispatcher will use this class

## Mvc\DispatcherInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/DispatcherInterface.zep)

-   __Namespace__

    - `Phalcon\Mvc`

-   __Uses__

    - `Phalcon\Dispatcher\DispatcherInterface`

-   __Extends__

    `DispatcherInterfaceBase`

-   __Implements__

Phalcon\Mvc\DispatcherInterface

Interface for Phalcon\Mvc\Dispatcher

### Methods

```php
public function getActiveController(): ControllerInterface;
```
Returns the active controller in the dispatcher

```php
public function getControllerName(): string;
```
Gets last dispatched controller name

```php
public function getLastController(): ControllerInterface;
```
Returns the latest dispatched controller

```php
public function setControllerName( string $controllerName );
```
Sets the controller name to be dispatched

```php
public function setControllerSuffix( string $controllerSuffix );
```
Sets the default controller suffix

```php
public function setDefaultController( string $controllerName );
```
Sets the default controller name

## Mvc\EntityInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/EntityInterface.zep)

-   __Namespace__

    - `Phalcon\Mvc`

-   __Uses__

-   __Extends__

-   __Implements__

Phalcon\Mvc\EntityInterface

Interface for Phalcon\Mvc\Collection and Phalcon\Mvc\Model

### Methods

```php
public function readAttribute( string $attribute ): mixed | null;
```
Reads an attribute value by its name

```php
public function writeAttribute( string $attribute, mixed $value );
```
Writes an attribute value by its name

## Mvc\Micro 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Micro.zep)

-   __Namespace__

    - `Phalcon\Mvc`

-   __Uses__

    - `ArrayAccess`
    - `Closure`
    - `Phalcon\Di\DiInterface`
    - `Phalcon\Di\FactoryDefault`
    - `Phalcon\Di\Injectable`
    - `Phalcon\Di\ServiceInterface`
    - `Phalcon\Events\EventsAwareInterface`
    - `Phalcon\Events\ManagerInterface`
    - `Phalcon\Http\ResponseInterface`
    - `Phalcon\Mvc\Micro\Collection`
    - `Phalcon\Mvc\Micro\CollectionInterface`
    - `Phalcon\Mvc\Micro\Exception`
    - `Phalcon\Mvc\Micro\LazyLoader`
    - `Phalcon\Mvc\Micro\MiddlewareInterface`
    - `Phalcon\Mvc\Model\BinderInterface`
    - `Phalcon\Mvc\Router\RouteInterface`
    - `Throwable`

-   __Extends__

    `Injectable`

-   __Implements__

    - `ArrayAccess`
    - `EventsAwareInterface`

Phalcon\Mvc\Micro

With Phalcon you can create "Micro-Framework like" applications. By doing
this, you only need to write a minimal amount of code to create a PHP
application. Micro applications are suitable to small applications, APIs and
prototypes in a practical way.

```php
$app = new \Phalcon\Mvc\Micro();

$app->get(
"/say/welcome/{name}",
function ($name) {
    echo "<h1>Welcome $name!</h1>";
}
);

$app->handle("/say/welcome/Phalcon");
```

### Properties
```php
/**
 * @var callable|null
 */
protected $activeHandler;

/**
 * @var array
 */
protected $afterBindingHandlers;

/**
 * @var array
 */
protected $afterHandlers;

/**
 * @var array
 */
protected $beforeHandlers;

/**
 * @var DiInterface|null
 */
protected $container;

/**
 * @var callable|null
 */
protected $errorHandler;

/**
 * @var ManagerInterface|null
 */
protected $eventsManager;

/**
 * @var array
 */
protected $finishHandlers;

/**
 * @var array
 */
protected $handlers;

/**
 * @var BinderInterface|null
 */
protected $modelBinder;

/**
 * @var callable|null
 */
protected $notFoundHandler;

/**
 * @var callable|null
 */
protected $responseHandler;

/**
 * @var mixed|null
 */
protected $returnedValue;

/**
 * @var RouterInterface|null
 */
protected $router;

/**
 * @var bool
 */
protected $stopped = false;

```

### Methods

```php
public function __construct( DiInterface $container = null );
```
Phalcon\Mvc\Micro constructor

```php
public function after( mixed $handler ): Micro;
```
Appends an 'after' middleware to be called after execute the route

```php
public function afterBinding( mixed $handler ): Micro;
```
Appends a afterBinding middleware to be called after model binding

```php
public function before( mixed $handler ): Micro;
```
Appends a before middleware to be called before execute the route

```php
public function delete( string $routePattern, mixed $handler ): RouteInterface;
```
Maps a route to a handler that only matches if the HTTP method is DELETE

```php
public function error( mixed $handler ): Micro;
```
Sets a handler that will be called when an exception is thrown handling
the route

```php
public function finish( mixed $handler ): Micro;
```
Appends a 'finish' middleware to be called when the request is finished

```php
public function get( string $routePattern, mixed $handler ): RouteInterface;
```
Maps a route to a handler that only matches if the HTTP method is GET

```php
public function getActiveHandler();
```
Return the handler that will be called for the matched route

```php
public function getBoundModels(): array;
```
Returns bound models from binder instance

```php
public function getEventsManager(): ManagerInterface | null;
```
Returns the internal event manager

```php
public function getHandlers(): array;
```
Returns the internal handlers attached to the application

```php
public function getModelBinder(): BinderInterface | null;
```
Gets model binder

```php
public function getReturnedValue();
```
Returns the value returned by the executed handler

```php
public function getRouter(): RouterInterface;
```
Returns the internal router used by the application

```php
public function getService( string $serviceName );
```
Obtains a service from the DI

```php
public function getSharedService( string $serviceName );
```
Obtains a shared service from the DI

```php
public function handle( string $uri );
```
Handle the whole request

```php
public function hasService( string $serviceName ): bool;
```
Checks if a service is registered in the DI

```php
public function head( string $routePattern, mixed $handler ): RouteInterface;
```
Maps a route to a handler that only matches if the HTTP method is HEAD

```php
public function map( string $routePattern, mixed $handler ): RouteInterface;
```
Maps a route to a handler without any HTTP method constraint

```php
public function mount( CollectionInterface $collection ): Micro;
```
Mounts a collection of handlers

```php
public function notFound( mixed $handler ): Micro;
```
Sets a handler that will be called when the router doesn't match any of
the defined routes

```php
public function offsetExists( mixed $offset ): bool;
```
Check if a service is registered in the internal services container using
the array syntax

```php
public function offsetGet( mixed $offset ): mixed;
```
Allows to obtain a shared service in the internal services container
using the array syntax

```php
var_dump(
$app["request"]
);
```

```php
public function offsetSet( mixed $offset, mixed $value ): void;
```
Allows to register a shared service in the internal services container
using the array syntax

```php
   $app["request"] = new \Phalcon\Http\Request();
```

```php
public function offsetUnset( mixed $offset ): void;
```
Removes a service from the internal services container using the array
syntax

```php
public function options( string $routePattern, mixed $handler ): RouteInterface;
```
Maps a route to a handler that only matches if the HTTP method is OPTIONS

```php
public function patch( string $routePattern, mixed $handler ): RouteInterface;
```
Maps a route to a handler that only matches if the HTTP method is PATCH

```php
public function post( string $routePattern, mixed $handler ): RouteInterface;
```
Maps a route to a handler that only matches if the HTTP method is POST

```php
public function put( string $routePattern, mixed $handler ): RouteInterface;
```
Maps a route to a handler that only matches if the HTTP method is PUT

```php
public function setActiveHandler( mixed $activeHandler );
```
Sets externally the handler that must be called by the matched route

```php
public function setDI( DiInterface $container ): void;
```
Sets the DependencyInjector container

```php
public function setEventsManager( ManagerInterface $eventsManager ): void;
```
Sets the events manager

```php
public function setModelBinder( BinderInterface $modelBinder, mixed $cache = null ): Micro;
```
Sets model binder

```php
$micro = new Micro($di);

$micro->setModelBinder(
new Binder(),
'cache'
);
```

```php
public function setResponseHandler( mixed $handler ): Micro;
```
Appends a custom 'response' handler to be called instead of the default
response handler

```php
public function setService( string $serviceName, mixed $definition, bool $shared = bool ): ServiceInterface;
```
Sets a service from the DI

```php
public function stop(): void;
```
Stops the middleware execution avoiding than other middlewares be
executed

## Mvc\Micro\Collection 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Micro/Collection.zep)

-   __Namespace__

    - `Phalcon\Mvc\Micro`

-   __Uses__

-   __Extends__

-   __Implements__

    - `CollectionInterface`

Phalcon\Mvc\Micro\Collection

Groups Micro-Mvc handlers as controllers

```php
$app = new \Phalcon\Mvc\Micro();

$collection = new Collection();

$collection->setHandler(
new PostsController()
);

$collection->get("/posts/edit/{id}", "edit");

$app->mount($collection);
```

### Properties
```php
/**
 * @var callable
 */
protected $handler;

/**
 * @var array
 */
protected $handlers;

/**
 * @var bool
 */
protected $lazy = false;

/**
 * @var string
 */
protected $prefix = ;

```

### Methods

```php
public function delete( string $routePattern, callable $handler, string $name = null ): CollectionInterface;
```
Maps a route to a handler that only matches if the HTTP method is DELETE.

```php
public function get( string $routePattern, callable $handler, string $name = null ): CollectionInterface;
```
Maps a route to a handler that only matches if the HTTP method is GET.

```php
public function getHandler(): mixed;
```
Returns the main handler

```php
public function getHandlers(): array;
```
Returns the registered handlers

```php
public function getPrefix(): string;
```
Returns the collection prefix if any

```php
public function head( string $routePattern, callable $handler, string $name = null ): CollectionInterface;
```
Maps a route to a handler that only matches if the HTTP method is HEAD.

```php
public function isLazy(): bool;
```
Returns if the main handler must be lazy loaded

```php
public function map( string $routePattern, callable $handler, string $name = null ): CollectionInterface;
```
Maps a route to a handler.

```php
public function mapVia( string $routePattern, callable $handler, mixed $method, string $name = null ): CollectionInterface;
```
Maps a route to a handler via methods.

```php
$collection->mapVia(
"/test",
"indexAction",
["POST", "GET"],
"test"
);
```

```php
public function options( string $routePattern, callable $handler, string $name = null ): CollectionInterface;
```
Maps a route to a handler that only matches if the HTTP method is
OPTIONS.

```php
public function patch( string $routePattern, callable $handler, string $name = null ): CollectionInterface;
```
Maps a route to a handler that only matches if the HTTP method is PATCH.

```php
public function post( string $routePattern, callable $handler, string $name = null ): CollectionInterface;
```
Maps a route to a handler that only matches if the HTTP method is POST.

```php
public function put( string $routePattern, callable $handler, string $name = null ): CollectionInterface;
```
Maps a route to a handler that only matches if the HTTP method is PUT.

```php
public function setHandler( mixed $handler, bool $lazy = bool ): CollectionInterface;
```
Sets the main handler.

```php
public function setLazy( bool $lazy ): CollectionInterface;
```
Sets if the main handler must be lazy loaded

```php
public function setPrefix( string $prefix ): CollectionInterface;
```
Sets a prefix for all routes added to the collection

```php
protected function addMap( mixed $method, string $routePattern, callable $handler, string $name = null ): void;
```
Internal function to add a handler to the group.

## Mvc\Micro\CollectionInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Micro/CollectionInterface.zep)

-   __Namespace__

    - `Phalcon\Mvc\Micro`

-   __Uses__

-   __Extends__

-   __Implements__

Phalcon\Mvc\Micro\CollectionInterface

Interface for Phalcon\Mvc\Micro\Collection

### Methods

```php
public function delete( string $routePattern, callable $handler, string $name = null ): CollectionInterface;
```
Maps a route to a handler that only matches if the HTTP method is DELETE

```php
public function get( string $routePattern, callable $handler, string $name = null ): CollectionInterface;
```
Maps a route to a handler that only matches if the HTTP method is GET

```php
public function getHandler(): mixed;
```
Returns the main handler

```php
public function getHandlers(): array;
```
Returns the registered handlers

```php
public function getPrefix(): string;
```
Returns the collection prefix if any

```php
public function head( string $routePattern, callable $handler, string $name = null ): CollectionInterface;
```
Maps a route to a handler that only matches if the HTTP method is HEAD

```php
public function isLazy(): bool;
```
Returns if the main handler must be lazy loaded

```php
public function map( string $routePattern, callable $handler, string $name = null ): CollectionInterface;
```
Maps a route to a handler

```php
public function options( string $routePattern, callable $handler, string $name = null ): CollectionInterface;
```
Maps a route to a handler that only matches if the HTTP method is OPTIONS

```php
public function patch( string $routePattern, callable $handler, string $name = null ): CollectionInterface;
```
Maps a route to a handler that only matches if the HTTP method is PATCH

```php
public function post( string $routePattern, callable $handler, string $name = null ): CollectionInterface;
```
Maps a route to a handler that only matches if the HTTP method is POST

```php
public function put( string $routePattern, callable $handler, string $name = null ): CollectionInterface;
```
Maps a route to a handler that only matches if the HTTP method is PUT

```php
public function setHandler( mixed $handler, bool $lazy = bool ): CollectionInterface;
```
Sets the main handler

```php
public function setLazy( bool $lazy ): CollectionInterface;
```
Sets if the main handler must be lazy loaded

```php
public function setPrefix( string $prefix ): CollectionInterface;
```
Sets a prefix for all routes added to the collection

## Mvc\Micro\Exception 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Micro/Exception.zep)

-   __Namespace__

    - `Phalcon\Mvc\Micro`

-   __Uses__

-   __Extends__

    `\Exception`

-   __Implements__

Exceptions thrown in Phalcon\Mvc\Micro will use this class

## Mvc\Micro\LazyLoader 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Micro/LazyLoader.zep)

-   __Namespace__

    - `Phalcon\Mvc\Micro`

-   __Uses__

    - `Phalcon\Mvc\Model\BinderInterface`

-   __Extends__

-   __Implements__

Phalcon\Mvc\Micro\LazyLoader

Lazy-Load of handlers for Mvc\Micro using auto-loading

### Properties
```php
/**
 * @var string
 */
protected $definition;

/**
 * @var object|null
 */
protected $handler;

```

### Methods

```php
public function __construct( string $definition );
```
Phalcon\Mvc\Micro\LazyLoader constructor

```php
public function callMethod( string $method, mixed $arguments, BinderInterface $modelBinder = null );
```
Calling __call method

```php
public function getDefinition(): string;
```

```php
public function getHandler(): object | null;
```

## Mvc\Micro\MiddlewareInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Micro/MiddlewareInterface.zep)

-   __Namespace__

    - `Phalcon\Mvc\Micro`

-   __Uses__

    - `Phalcon\Mvc\Micro`

-   __Extends__

-   __Implements__

Allows to implement Phalcon\Mvc\Micro middleware in classes

### Methods

```php
public function call( Micro $application );
```
Calls the middleware

## Mvc\Model ![Abstract](/assets/images/abstract-green.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model.zep)

-   __Namespace__

    - `Phalcon\Mvc`

-   __Uses__

    - `JsonSerializable`
    - `Phalcon\Db\Adapter\AdapterInterface`
    - `Phalcon\Db\Column`
    - `Phalcon\Db\DialectInterface`
    - `Phalcon\Db\Enum`
    - `Phalcon\Db\RawValue`
    - `Phalcon\Di\AbstractInjectionAware`
    - `Phalcon\Di\Di`
    - `Phalcon\Di\DiInterface`
    - `Phalcon\Events\ManagerInterface`
    - `Phalcon\Filter\Validation\ValidationInterface`
    - `Phalcon\Messages\Message`
    - `Phalcon\Messages\MessageInterface`
    - `Phalcon\Mvc\ModelInterface`
    - `Phalcon\Mvc\Model\BehaviorInterface`
    - `Phalcon\Mvc\Model\Criteria`
    - `Phalcon\Mvc\Model\CriteriaInterface`
    - `Phalcon\Mvc\Model\Exception`
    - `Phalcon\Mvc\Model\ManagerInterface`
    - `Phalcon\Mvc\Model\MetaDataInterface`
    - `Phalcon\Mvc\Model\Query`
    - `Phalcon\Mvc\Model\QueryInterface`
    - `Phalcon\Mvc\Model\Query\Builder`
    - `Phalcon\Mvc\Model\Query\BuilderInterface`
    - `Phalcon\Mvc\Model\Relation`
    - `Phalcon\Mvc\Model\RelationInterface`
    - `Phalcon\Mvc\Model\ResultInterface`
    - `Phalcon\Mvc\Model\Resultset`
    - `Phalcon\Mvc\Model\ResultsetInterface`
    - `Phalcon\Mvc\Model\TransactionInterface`
    - `Phalcon\Mvc\Model\ValidationFailed`
    - `Phalcon\Support\Collection`
    - `Phalcon\Support\Collection\CollectionInterface`
    - `Serializable`

-   __Extends__

    `AbstractInjectionAware`

-   __Implements__

    - `EntityInterface`
    - `JsonSerializable`
    - `ModelInterface`
    - `ResultInterface`
    - `Serializable`

Phalcon\Mvc\Model

Phalcon\Mvc\Model connects business objects and database tables to create a
persistable domain model where logic and data are presented in one wrapping.
It‘s an implementation of the object-relational mapping (ORM).

A model represents the information (data) of the application and the rules to
manipulate that data. Models are primarily used for managing the rules of
interaction with a corresponding database table. In most cases, each table in
your database will correspond to one model in your application. The bulk of
your application's business logic will be concentrated in the models.

Phalcon\Mvc\Model is the first ORM written in Zephir/C languages for PHP,
giving to developers high performance when interacting with databases while
is also easy to use.

```php
$robot = new Robots();

$robot->type = "mechanical";
$robot->name = "Astro Boy";
$robot->year = 1952;

if ($robot->save() === false) {
echo "Umh, We can store robots: ";

$messages = $robot->getMessages();

foreach ($messages as $message) {
    echo $message;
}
} else {
echo "Great, a new robot was saved successfully!";
}
```

### Constants
```php
const DIRTY_STATE_DETACHED = 2;
const DIRTY_STATE_PERSISTENT = 0;
const DIRTY_STATE_TRANSIENT = 1;
const OP_CREATE = 1;
const OP_DELETE = 3;
const OP_NONE = 0;
const OP_UPDATE = 2;
const TRANSACTION_INDEX = transaction;
```

### Properties
```php
/**
 * @var int
 */
protected $dirtyState = 1;

/**
 * @var array
 */
protected $dirtyRelated;

/**
 * @var array
 */
protected $errorMessages;

/**
 * @var ManagerInterface|null
 */
protected $modelsManager;

/**
 * @var MetaDataInterface|null
 */
protected $modelsMetaData;

/**
 * @var array
 */
protected $related;

/**
 * @var int
 */
protected $operationMade = ;

/**
 * @var array
 */
protected $oldSnapshot;

/**
 * @var bool
 */
protected $skipped = false;

/**
 * @var array
 */
protected $snapshot;

/**
 * @var TransactionInterface|null
 */
protected $transaction;

/**
 * @var string|null
 */
protected $uniqueKey;

/**
 * @var array
 */
protected $uniqueParams;

/**
 * @var array
 */
protected $uniqueTypes;

```

### Methods

```php
public function __call( string $method, array $arguments );
```
Handles method calls when a method is not implemented

```php
public static function __callStatic( string $method, array $arguments );
```
Handles method calls when a static method is not implemented

```php
final public function __construct( mixed $data = null, DiInterface $container = null, ManagerInterface $modelsManager = null );
```
Phalcon\Mvc\Model constructor

```php
public function __get( string $property );
```
Magic method to get related records using the relation alias as a
property

```php
public function __isset( string $property ): bool;
```
Magic method to check if a property is a valid relation

```php
public function __serialize(): array;
```
Serializes a model

```php
public function __set( string $property, mixed $value );
```
Magic method to assign values to the the model

```php
public function __unserialize( array $data ): void;
```
Unserializes an array to the model

```php
public function addBehavior( BehaviorInterface $behavior ): void;
```
Setups a behavior in a model

```php
use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Behavior\Timestampable;

class Robots extends Model
{
public function initialize()
{
    $this->addBehavior(
        new Timestampable(
            [
                "beforeCreate" => [
                    "field"  => "created_at",
                    "format" => "Y-m-d",
                ],
            ]
        )
    );

    $this->addBehavior(
        new Timestampable(
            [
                "beforeUpdate" => [
                    "field"  => "updated_at",
                    "format" => "Y-m-d",
                ],
            ]
        )
    );
}
}
```

```php
public function appendMessage( MessageInterface $message ): ModelInterface;
```
Appends a customized message on the validation process

```php
use Phalcon\Mvc\Model;
use Phalcon\Messages\Message as Message;

class Robots extends Model
{
public function beforeSave()
{
    if ($this->name === "Peter") {
        $message = new Message(
            "Sorry, but a robot cannot be named Peter"
        );

        $this->appendMessage($message);
    }
}
}
```

```php
public function assign( array $data, mixed $whiteList = null, mixed $dataColumnMap = null ): ModelInterface;
```
Assigns values to a model from an array

```php
$robot->assign(
[
    "type" => "mechanical",
    "name" => "Astro Boy",
    "year" => 1952,
]
);

// Assign by db row, column map needed
$robot->assign(
$dbRow,
[
    "db_type" => "type",
    "db_name" => "name",
    "db_year" => "year",
]
);

// Allow assign only name and year
$robot->assign(
$_POST,
[
    "name",
    "year",
]
);

// By default assign method will use setters if exist, you can disable it by using ini_set to directly use properties

ini_set("phalcon.orm.disable_assign_setters", true);

$robot->assign(
$_POST,
[
    "name",
    "year",
]
);
```

```php
public static function average( array $parameters = [] ): double | ResultsetInterface;
```
Returns the average value on a column for a result-set of rows matching
the specified conditions.

Returned value will be a float for simple queries or a ResultsetInterface
instance for when the GROUP condition is used. The results will
contain the average of each group.

```php
// What's the average price of robots?
$average = Robots::average(
[
    "column" => "price",
]
);

echo "The average price is ", $average, "\n";

// What's the average price of mechanical robots?
$average = Robots::average(
[
    "type = 'mechanical'",
    "column" => "price",
]
);

echo "The average price of mechanical robots is ", $average, "\n";
```

```php
public static function cloneResult( ModelInterface $base, array $data, int $dirtyState = int ): ModelInterface;
```
Assigns values to a model from an array returning a new model

```php
$robot = Phalcon\Mvc\Model::cloneResult(
new Robots(),
[
    "type" => "mechanical",
    "name" => "Astro Boy",
    "year" => 1952,
]
);
```

```php
public static function cloneResultMap( mixed $base, array $data, mixed $columnMap, int $dirtyState = int, bool $keepSnapshots = null ): ModelInterface;
```
Assigns values to a model from an array, returning a new model.

```php
$robot = \Phalcon\Mvc\Model::cloneResultMap(
new Robots(),
[
    "type" => "mechanical",
    "name" => "Astro Boy",
    "year" => 1952,
]
);
```

```php
public static function cloneResultMapHydrate( array $data, mixed $columnMap, int $hydrationMode );
```
Returns an hydrated result based on the data and the column map

```php
public static function count( mixed $parameters = null ): int | ResultsetInterface;
```
Counts how many records match the specified conditions.

Returns an integer for simple queries or a ResultsetInterface
instance for when the GROUP condition is used. The results will
contain the count of each group.

```php
// How many robots are there?
$number = Robots::count();

echo "There are ", $number, "\n";

// How many mechanical robots are there?
$number = Robots::count("type = 'mechanical'");

echo "There are ", $number, " mechanical robots\n";
```

```php
public function create(): bool;
```
Inserts a model instance. If the instance already exists in the
persistence it will throw an exception
Returning true on success or false otherwise.

```php
// Creating a new robot
$robot = new Robots();

$robot->type = "mechanical";
$robot->name = "Astro Boy";
$robot->year = 1952;

$robot->create();

// Passing an array to create
$robot = new Robots();

$robot->assign(
[
    "type" => "mechanical",
    "name" => "Astro Boy",
    "year" => 1952,
]
);

$robot->create();
```

```php
public function delete(): bool;
```
Deletes a model instance. Returning true on success or false otherwise.

```php
$robot = Robots::findFirst("id=100");

$robot->delete();

$robots = Robots::find("type = 'mechanical'");

foreach ($robots as $robot) {
$robot->delete();
}
```

```php
public function doSave( CollectionInterface $visited ): bool;
```
Inserted or updates model instance, expects a visited list of objects.

```php
public function dump(): array;
```
Returns a simple representation of the object that can be used with
`var_dump()`

```php
var_dump(
$robot->dump()
);
```

```php
public static function find( mixed $parameters = null ): ResultsetInterface;
```
Query for a set of records that match the specified conditions

```php
// How many robots are there?
$robots = Robots::find();

echo "There are ", count($robots), "\n";

// How many mechanical robots are there?
$robots = Robots::find(
"type = 'mechanical'"
);

echo "There are ", count($robots), "\n";

// Get and print virtual robots ordered by name
$robots = Robots::find(
[
    "type = 'virtual'",
    "order" => "name",
]
);

foreach ($robots as $robot) {
echo $robot->name, "\n";
}

// Get first 100 virtual robots ordered by name
$robots = Robots::find(
[
    "type = 'virtual'",
    "order" => "name",
    "limit" => 100,
]
);

foreach ($robots as $robot) {
echo $robot->name, "\n";
}

// encapsulate find it into an running transaction esp. useful for application unit-tests
// or complex business logic where we wanna control which transactions are used.

$myTransaction = new Transaction(\Phalcon\Di\Di::getDefault());
$myTransaction->begin();

$newRobot = new Robot();
$newRobot->setTransaction($myTransaction);

$newRobot->assign(
[
    'name' => 'test',
    'type' => 'mechanical',
    'year' => 1944,
]
);

$newRobot->save();

$resultInsideTransaction = Robot::find(
[
    'name' => 'test',
    Model::TRANSACTION_INDEX => $myTransaction,
]
);

$resultOutsideTransaction = Robot::find(['name' => 'test']);

foreach ($setInsideTransaction as $robot) {
echo $robot->name, "\n";
}

foreach ($setOutsideTransaction as $robot) {
echo $robot->name, "\n";
}

// reverts all not commited changes
$myTransaction->rollback();

// creating two different transactions
$myTransaction1 = new Transaction(\Phalcon\Di\Di::getDefault());
$myTransaction1->begin();
$myTransaction2 = new Transaction(\Phalcon\Di\Di::getDefault());
$myTransaction2->begin();

 // add a new robots
$firstNewRobot = new Robot();
$firstNewRobot->setTransaction($myTransaction1);
$firstNewRobot->assign(
[
    'name' => 'first-transaction-robot',
    'type' => 'mechanical',
    'year' => 1944,
]
);
$firstNewRobot->save();

$secondNewRobot = new Robot();
$secondNewRobot->setTransaction($myTransaction2);
$secondNewRobot->assign(
[
    'name' => 'second-transaction-robot',
    'type' => 'fictional',
    'year' => 1984,
]
);
$secondNewRobot->save();

// this transaction will find the robot.
$resultInFirstTransaction = Robot::find(
[
    'name'                   => 'first-transaction-robot',
    Model::TRANSACTION_INDEX => $myTransaction1,
]
);

// this transaction won't find the robot.
$resultInSecondTransaction = Robot::find(
[
    'name'                   => 'first-transaction-robot',
    Model::TRANSACTION_INDEX => $myTransaction2,
]
);

// this transaction won't find the robot.
$resultOutsideAnyExplicitTransaction = Robot::find(
[
    'name' => 'first-transaction-robot',
]
);

// this transaction won't find the robot.
$resultInFirstTransaction = Robot::find(
[
    'name'                   => 'second-transaction-robot',
    Model::TRANSACTION_INDEX => $myTransaction2,
]
);

// this transaction will find the robot.
$resultInSecondTransaction = Robot::find(
[
    'name'                   => 'second-transaction-robot',
    Model::TRANSACTION_INDEX => $myTransaction1,
]
);

// this transaction won't find the robot.
$resultOutsideAnyExplicitTransaction = Robot::find(
[
    'name' => 'second-transaction-robot',
]
);

$transaction1->rollback();
$transaction2->rollback();
```

```php
public static function findFirst( mixed $parameters = null ): mixed | null;
```
Query the first record that matches the specified conditions

```php
// What's the first robot in robots table?
$robot = Robots::findFirst();

echo "The robot name is ", $robot->name;

// What's the first mechanical robot in robots table?
$robot = Robots::findFirst(
"type = 'mechanical'"
);

echo "The first mechanical robot name is ", $robot->name;

// Get first virtual robot ordered by name
$robot = Robots::findFirst(
[
    "type = 'virtual'",
    "order" => "name",
]
);

echo "The first virtual robot name is ", $robot->name;

// behaviour with transaction
$myTransaction = new Transaction(\Phalcon\Di\Di::getDefault());
$myTransaction->begin();

$newRobot = new Robot();
$newRobot->setTransaction($myTransaction);
$newRobot->assign(
[
    'name' => 'test',
    'type' => 'mechanical',
    'year' => 1944,
]
);
$newRobot->save();

$findsARobot = Robot::findFirst(
[
    'name'                   => 'test',
    Model::TRANSACTION_INDEX => $myTransaction,
]
);

$doesNotFindARobot = Robot::findFirst(
[
    'name' => 'test',
]
);

var_dump($findARobot);
var_dump($doesNotFindARobot);

$transaction->commit();

$doesFindTheRobotNow = Robot::findFirst(
[
    'name' => 'test',
]
);
```

```php
public function fireEvent( string $eventName ): bool;
```
Fires an event, implicitly calls behaviors and listeners in the events
manager are notified

```php
public function fireEventCancel( string $eventName ): bool;
```
Fires an event, implicitly calls behaviors and listeners in the events
manager are notified
This method stops if one of the callbacks/listeners returns bool false

```php
public function getChangedFields(): array;
```
Returns a list of changed values.

```php
$robots = Robots::findFirst();
print_r($robots->getChangedFields()); // []

$robots->deleted = 'Y';

$robots->getChangedFields();
print_r($robots->getChangedFields()); // ["deleted"]
```

```php
public function getDirtyState(): int;
```
Returns one of the DIRTY_STATE_* constants telling if the record exists
in the database or not

```php
public function getEventsManager(): EventsManagerInterface | null;
```
Returns the custom events manager or null if there is no custom events manager

```php
public function getMessages( mixed $filter = null ): MessageInterface[];
```
Returns array of validation messages

```php
$robot = new Robots();

$robot->type = "mechanical";
$robot->name = "Astro Boy";
$robot->year = 1952;

if ($robot->save() === false) {
echo "Umh, We can't store robots right now ";

$messages = $robot->getMessages();

foreach ($messages as $message) {
    echo $message;
}
} else {
echo "Great, a new robot was saved successfully!";
}
```

```php
public function getModelsManager(): ManagerInterface;
```
Returns the models manager related to the entity instance

```php
public function getModelsMetaData(): MetaDataInterface;
```
\{@inheritdoc\}

```php
public function getOldSnapshotData(): array;
```
Returns the internal old snapshot data

```php
public function getOperationMade(): int;
```
Returns the type of the latest operation performed by the ORM
Returns one of the OP_* class constants

```php
final public function getReadConnection(): AdapterInterface;
```
Gets the connection used to read data for the model

```php
final public function getReadConnectionService(): string;
```
Returns the DependencyInjection connection service name used to read data
related the model

```php
public function getRelated( string $alias, mixed $arguments = null );
```
Returns related records based on defined relations

```php
final public function getSchema(): string | null;
```
Returns schema name where the mapped table is located

```php
public function getSnapshotData(): array;
```
Returns the internal snapshot data

```php
final public function getSource(): string;
```
Returns the table name mapped in the model

```php
public function getTransaction(): TransactionInterface | null;
```

```php
public function getUpdatedFields(): array;
```
Returns a list of updated values.

```php
$robots = Robots::findFirst();
print_r($robots->getChangedFields()); // []

$robots->deleted = 'Y';

$robots->getChangedFields();
print_r($robots->getChangedFields()); // ["deleted"]
$robots->save();
print_r($robots->getChangedFields()); // []
print_r($robots->getUpdatedFields()); // ["deleted"]
```

```php
final public function getWriteConnection(): AdapterInterface;
```
Gets the connection used to write data to the model

```php
final public function getWriteConnectionService(): string;
```
Returns the DependencyInjection connection service name used to write
data related to the model

```php
public function hasChanged( mixed $fieldName = null, bool $allFields = bool ): bool;
```
Check if a specific attribute has changed
This only works if the model is keeping data snapshots

```php
$robot = new Robots();

$robot->type = "mechanical";
$robot->name = "Astro Boy";
$robot->year = 1952;

$robot->create();

$robot->type = "hydraulic";

$hasChanged = $robot->hasChanged("type"); // returns true
$hasChanged = $robot->hasChanged(["type", "name"]); // returns true
$hasChanged = $robot->hasChanged(["type", "name"], true); // returns false
```

```php
public function hasSnapshotData(): bool;
```
Checks if the object has internal snapshot data

```php
public function hasUpdated( mixed $fieldName = null, bool $allFields = bool ): bool;
```
Check if a specific attribute was updated
This only works if the model is keeping data snapshots

```php
public function isRelationshipLoaded( string $relationshipAlias ): bool;
```
Checks if saved related records have already been loaded.

Only returns true if the records were previously fetched
through the model without any additional parameters.

```php
$robot = Robots::findFirst();
var_dump($robot->isRelationshipLoaded('robotsParts')); // false

$robotsParts = $robot->getRobotsParts(['id > 0']);
var_dump($robot->isRelationshipLoaded('robotsParts')); // false

$robotsParts = $robot->getRobotsParts(); // or $robot->robotsParts
var_dump($robot->isRelationshipLoaded('robotsParts')); // true

$robot->robotsParts = [new RobotsParts()];
var_dump($robot->isRelationshipLoaded('robotsParts')); // false
```

```php
public function jsonSerialize(): array;
```
   Serializes the object for json_encode

```php
   echo json_encode($robot);
```

```php
public static function maximum( mixed $parameters = null ): mixed;
```
Returns the maximum value of a column for a result-set of rows that match
the specified conditions

```php
// What is the maximum robot id?
$id = Robots::maximum(
[
    "column" => "id",
]
);

echo "The maximum robot id is: ", $id, "\n";

// What is the maximum id of mechanical robots?
$sum = Robots::maximum(
[
    "type = 'mechanical'",
    "column" => "id",
]
);

echo "The maximum robot id of mechanical robots is ", $id, "\n";
```

```php
public static function minimum( mixed $parameters = null ): mixed;
```
Returns the minimum value of a column for a result-set of rows that match
the specified conditions

```php
// What is the minimum robot id?
$id = Robots::minimum(
[
    "column" => "id",
]
);

echo "The minimum robot id is: ", $id;

// What is the minimum id of mechanical robots?
$sum = Robots::minimum(
[
    "type = 'mechanical'",
    "column" => "id",
]
);

echo "The minimum robot id of mechanical robots is ", $id;
```

```php
public static function query( DiInterface $container = null ): CriteriaInterface;
```
Create a criteria for a specific model

```php
public function readAttribute( string $attribute ): mixed | null;
```
Reads an attribute value by its name

```php
echo $robot->readAttribute("name");
```

```php
public function refresh(): ModelInterface;
```
Refreshes the model attributes re-querying the record from the database

```php
public function save(): bool;
```
Inserts or updates a model instance. Returning true on success or false
otherwise.

```php
// Creating a new robot
$robot = new Robots();

$robot->type = "mechanical";
$robot->name = "Astro Boy";
$robot->year = 1952;

$robot->save();

// Updating a robot name
$robot = Robots::findFirst("id = 100");

$robot->name = "Biomass";

$robot->save();
```

```php
public function serialize(): string;
```
Serializes the object ignoring connections, services, related objects or
static properties

```php
final public function setConnectionService( string $connectionService ): void;
```
Sets the DependencyInjection connection service name

```php
public function setDirtyState( int $dirtyState ): ModelInterface | bool;
```
Sets the dirty state of the object using one of the DIRTY_STATE_* constants

```php
public function setEventsManager( EventsManagerInterface $eventsManager );
```
Sets a custom events manager

```php
public function setOldSnapshotData( array $data, mixed $columnMap = null );
```
Sets the record's old snapshot data.
This method is used internally to set old snapshot data when the model
was set up to keep snapshot data

```php
final public function setReadConnectionService( string $connectionService ): void;
```
Sets the DependencyInjection connection service name used to read data

```php
public function setSnapshotData( array $data, mixed $columnMap = null ): void;
```
Sets the record's snapshot data.
This method is used internally to set snapshot data when the model was
set up to keep snapshot data

```php
public function setTransaction( TransactionInterface $transaction ): ModelInterface;
```
Sets a transaction related to the Model instance

```php
use Phalcon\Mvc\Model\Transaction\Manager as TxManager;
use Phalcon\Mvc\Model\Transaction\Failed as TxFailed;

try {
$txManager = new TxManager();

$transaction = $txManager->get();

$robot = new Robots();

$robot->setTransaction($transaction);

$robot->name       = "WALL·E";
$robot->created_at = date("Y-m-d");

if ($robot->save() === false) {
    $transaction->rollback("Can't save robot");
}

$robotPart = new RobotParts();

$robotPart->setTransaction($transaction);

$robotPart->type = "head";

if ($robotPart->save() === false) {
    $transaction->rollback("Robot part cannot be saved");
}

$transaction->commit();
} catch (TxFailed $e) {
echo "Failed, reason: ", $e->getMessage();
}
```

```php
final public function setWriteConnectionService( string $connectionService ): void;
```
Sets the DependencyInjection connection service name used to write data

```php
public static function setup( array $options ): void;
```
Enables/disables options in the ORM

```php
public function skipOperation( bool $skip ): void;
```
Skips the current operation forcing a success state

```php
public static function sum( mixed $parameters = null ): double | ResultsetInterface;
```
Calculates the sum on a column for a result-set of rows that match the
specified conditions

```php
// How much are all robots?
$sum = Robots::sum(
[
    "column" => "price",
]
);

echo "The total price of robots is ", $sum, "\n";

// How much are mechanical robots?
$sum = Robots::sum(
[
    "type = 'mechanical'",
    "column" => "price",
]
);

echo "The total price of mechanical robots is  ", $sum, "\n";
```

```php
public function toArray( mixed $columns = null, bool $useGetter = true ): array;
```
Returns the instance as an array representation. By default, it will take into account getters, when constructing the resulting array. If the second parameter is set to `false`, getter methods for properties will be ignored.

```php
print_r(
$robot->toArray()
);
```

```php
public function unserialize( mixed $data );
```
Unserializes the object from a serialized string

```php
public function update(): bool;
```
Updates a model instance. If the instance doesn't exist in the
persistence it will throw an exception. Returning `true` on success or
`false` otherwise.

```php
<?php

use MyApp\Models\Invoices;

$invoice = Invoices::findFirst('inv_id = 4');

$invoice->inv_total = 120;

$invoice->update();
```

:::warning[NOTE]
When retrieving the record with `findFirst()`, you need to get the full
object back (no `columns` definition) but also retrieve it using the
primary key. If not, the ORM will issue an `INSERT` instead of `UPDATE`.
:::

```php
public function validationHasFailed(): bool;
```
Check whether validation process has generated any messages

```php
use Phalcon\Mvc\Model;
use Phalcon\Filter\Validation;
use Phalcon\Filter\Validation\Validator\ExclusionIn;

class Subscriptors extends Model
{
public function validation()
{
    $validator = new Validation();

    $validator->validate(
        "status",
        new ExclusionIn(
            [
                "domain" => [
                    "A",
                    "I",
                ],
            ]
        )
    );

    return $this->validate($validator);
}
}
```

```php
public function writeAttribute( string $attribute, mixed $value ): void;
```
Writes an attribute value by its name

```php
$robot->writeAttribute("name", "Rosey");
```

```php
protected function allowEmptyStringValues( array $attributes ): void;
```
Sets a list of attributes that must be skipped from the
generated UPDATE statement

```php
class Robots extends \Phalcon\Mvc\Model
{
public function initialize()
{
    $this->allowEmptyStringValues(
        [
            "name",
        ]
    );
}
}
```

```php
protected function belongsTo( mixed $fields, string $referenceModel, mixed $referencedFields, array $options = [] ): Relation;
```
Setup a reverse 1-1 or n-1 relation between two models

```php
class RobotsParts extends \Phalcon\Mvc\Model
{
public function initialize()
{
    $this->belongsTo(
        "robots_id",
        Robots::class,
        "id"
    );
}
}
```

```php
protected function cancelOperation();
```
Cancel the current operation

```php
final protected function checkForeignKeysRestrict(): bool;
```
Reads "belongs to" relations and check the virtual foreign keys when
inserting or updating records to verify that inserted/updated values are
present in the related entity

```php
final protected function checkForeignKeysReverseCascade(): bool;
```
Reads both "hasMany" and "hasOne" relations and checks the virtual
foreign keys (cascade) when deleting records

```php
final protected function checkForeignKeysReverseRestrict(): bool;
```
Reads both "hasMany" and "hasOne" relations and checks the virtual
foreign keys (restrict) when deleting records

```php
protected function collectRelatedToSave(): array;
```
Collects previously queried (belongs-to, has-one and has-one-through)
related records along with freshly added one

```php
protected function doLowInsert( MetaDataInterface $metaData, AdapterInterface $connection, mixed $table, mixed $identityField ): bool;
```
Sends a pre-build INSERT SQL statement to the relational database system

```php
protected function doLowUpdate( MetaDataInterface $metaData, AdapterInterface $connection, mixed $table ): bool;
```
Sends a pre-build UPDATE SQL statement to the relational database system

```php
protected function getRelatedRecords( string $modelName, string $method, array $arguments );
```
Returns related records defined relations depending on the method name.
Returns false if the relation is non-existent.

```php
protected static function groupResult( string $functionName, string $alias, mixed $parameters = null ): ResultsetInterface;
```
Generate a PHQL SELECT statement for an aggregate

```php
protected function has( MetaDataInterface $metaData, AdapterInterface $connection ): bool;
```
Checks whether the current record already exists

```php
protected function hasMany( mixed $fields, string $referenceModel, mixed $referencedFields, array $options = [] ): Relation;
```
Setup a 1-n relation between two models

```php
class Robots extends \Phalcon\Mvc\Model
{
public function initialize()
{
    $this->hasMany(
        "id",
        RobotsParts::class,
        "robots_id"
    );
}
}
```

```php
protected function hasManyToMany( mixed $fields, string $intermediateModel, mixed $intermediateFields, mixed $intermediateReferencedFields, string $referenceModel, mixed $referencedFields, array $options = [] ): Relation;
```
Setup an n-n relation between two models, through an intermediate
relation

```php
class Robots extends \Phalcon\Mvc\Model
{
public function initialize()
{
    // Setup a many-to-many relation to Parts through RobotsParts
    $this->hasManyToMany(
        "id",
        RobotsParts::class,
        "robots_id",
        "parts_id",
        Parts::class,
        "id",
    );
}
}
```

```php
protected function hasOne( mixed $fields, string $referenceModel, mixed $referencedFields, array $options = [] ): Relation;
```
Setup a 1-1 relation between two models

```php
class Robots extends \Phalcon\Mvc\Model
{
public function initialize()
{
    $this->hasOne(
        "id",
        RobotsDescription::class,
        "robots_id"
    );
}
}
```

```php
protected function hasOneThrough( mixed $fields, string $intermediateModel, mixed $intermediateFields, mixed $intermediateReferencedFields, string $referenceModel, mixed $referencedFields, array $options = [] ): Relation;
```
Setup a 1-1 relation between two models, through an intermediate
relation

```php
class Robots extends \Phalcon\Mvc\Model
{
public function initialize()
{
    // Setup a 1-1 relation to one item from Parts through RobotsParts
    $this->hasOneThrough(
        "id",
        RobotsParts::class,
        "robots_id",
        "parts_id",
        Parts::class,
        "id",
    );
}
}
```

```php
protected function keepSnapshots( bool $keepSnapshot ): void;
```
Sets if the model must keep the original record snapshot in memory

```php
use Phalcon\Mvc\Model;

class Robots extends Model
{
public function initialize()
{
    $this->keepSnapshots(true);
}
}
```

```php
final protected function possibleSetter( string $property, mixed $value ): bool;
```
Check for, and attempt to use, possible setter.

```php
protected function postSave( bool $success, bool $exists ): bool;
```
Executes internal events after save a record

```php
protected function postSaveRelatedRecords( AdapterInterface $connection, mixed $related, CollectionInterface $visited ): bool;
```
Save the related records assigned in the has-one/has-many relations

```php
protected function preSave( MetaDataInterface $metaData, bool $exists, mixed $identityField ): bool;
```
Executes internal hooks before save a record

```php
protected function preSaveRelatedRecords( AdapterInterface $connection, mixed $related, CollectionInterface $visited ): bool;
```
Saves related records that must be stored prior to save the master record

```php
final protected function setSchema( string $schema ): ModelInterface;
```
Sets schema name where the mapped table is located

```php
final protected function setSource( string $source ): ModelInterface;
```
Sets the table name to which model should be mapped

```php
protected function skipAttributes( array $attributes ): void;
```
Sets a list of attributes that must be skipped from the
generated INSERT/UPDATE statement

```php
class Robots extends \Phalcon\Mvc\Model
{
public function initialize()
{
    $this->skipAttributes(
        [
            "price",
        ]
    );
}
}
```

```php
protected function skipAttributesOnCreate( array $attributes ): void;
```
Sets a list of attributes that must be skipped from the
generated INSERT statement

```php
class Robots extends \Phalcon\Mvc\Model
{
public function initialize()
{
    $this->skipAttributesOnCreate(
        [
            "created_at",
        ]
    );
}
}
```

```php
protected function skipAttributesOnUpdate( array $attributes ): void;
```
Sets a list of attributes that must be skipped from the
generated UPDATE statement

```php
class Robots extends \Phalcon\Mvc\Model
{
public function initialize()
{
    $this->skipAttributesOnUpdate(
        [
            "modified_in",
        ]
    );
}
}
```

```php
protected function useDynamicUpdate( bool $dynamicUpdate ): void;
```
Sets if a model must use dynamic update instead of the all-field update

```php
use Phalcon\Mvc\Model;

class Robots extends Model
{
public function initialize()
{
    $this->useDynamicUpdate(true);
}
}
```

```php
protected function validate( ValidationInterface $validator ): bool;
```
Executes validators on every validation call

```php
use Phalcon\Mvc\Model;
use Phalcon\Filter\Validation;
use Phalcon\Filter\Validation\Validator\ExclusionIn;

class Subscriptors extends Model
{
public function validation()
{
    $validator = new Validation();

    $validator->add(
        "status",
        new ExclusionIn(
            [
                "domain" => [
                    "A",
                    "I",
                ],
            ]
        )
    );

    return $this->validate($validator);
}
}
```

## Mvc\Model\Behavior ![Abstract](/assets/images/abstract-green.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Behavior.zep)

-   __Namespace__

    - `Phalcon\Mvc\Model`

-   __Uses__

    - `Phalcon\Mvc\ModelInterface`

-   __Extends__

-   __Implements__

    - `BehaviorInterface`

Phalcon\Mvc\Model\Behavior

This is an optional base class for ORM behaviors

### Properties
```php
/**
 * @var array
 */
protected $options;

```

### Methods

```php
public function __construct( array $options = [] );
```
Phalcon\Mvc\Model\Behavior

```php
public function missingMethod( ModelInterface $model, string $method, array $arguments = [] );
```
Acts as fallbacks when a missing method is called on the model

```php
public function notify( string $type, ModelInterface $model );
```
This method receives the notifications from the EventsManager

```php
protected function getOptions( string $eventName = null );
```
Returns the behavior options related to an event

```php
protected function mustTakeAction( string $eventName ): bool;
```
Checks whether the behavior must take action on certain event

## Mvc\Model\Behavior\SoftDelete 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Behavior/SoftDelete.zep)

-   __Namespace__

    - `Phalcon\Mvc\Model\Behavior`

-   __Uses__

    - `Phalcon\Mvc\ModelInterface`
    - `Phalcon\Mvc\Model\Behavior`
    - `Phalcon\Mvc\Model\Exception`

-   __Extends__

    `Behavior`

-   __Implements__

Phalcon\Mvc\Model\Behavior\SoftDelete

Instead of permanently delete a record it marks the record as deleted
changing the value of a flag column

### Methods

```php
public function notify( string $type, ModelInterface $model );
```
Listens for notifications from the models manager

## Mvc\Model\Behavior\Timestampable 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Behavior/Timestampable.zep)

-   __Namespace__

    - `Phalcon\Mvc\Model\Behavior`

-   __Uses__

    - `Closure`
    - `Phalcon\Mvc\ModelInterface`
    - `Phalcon\Mvc\Model\Behavior`
    - `Phalcon\Mvc\Model\Exception`

-   __Extends__

    `Behavior`

-   __Implements__

Phalcon\Mvc\Model\Behavior\Timestampable

Allows to automatically update a model’s attribute saving the datetime when a
record is created or updated

### Methods

```php
public function notify( string $type, ModelInterface $model );
```
Listens for notifications from the models manager

## Mvc\Model\BehaviorInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/BehaviorInterface.zep)

-   __Namespace__

    - `Phalcon\Mvc\Model`

-   __Uses__

    - `Phalcon\Mvc\ModelInterface`

-   __Extends__

-   __Implements__

Phalcon\Mvc\Model\BehaviorInterface

Interface for Phalcon\Mvc\Model\Behavior

### Methods

```php
public function missingMethod( ModelInterface $model, string $method, array $arguments = [] );
```
Calls a method when it's missing in the model

```php
public function notify( string $type, ModelInterface $model );
```
This method receives the notifications from the EventsManager

## Mvc\Model\Binder 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Binder.zep)

-   __Namespace__

    - `Phalcon\Mvc\Model`

-   __Uses__

    - `Closure`
    - `Phalcon\Cache\Adapter\AdapterInterface`
    - `Phalcon\Mvc\Controller\BindModelInterface`
    - `Phalcon\Mvc\Model\Binder\BindableInterface`
    - `ReflectionFunction`
    - `ReflectionMethod`

-   __Extends__

-   __Implements__

    - `BinderInterface`

Phalcon\Mvc\Model\Binder

This is an class for binding models into params for handler

### Properties
```php
/**
 * Array for storing active bound models
 *
 * @var array
 */
protected $boundModels;

/**
 * Cache object used for caching parameters for model binding
 *
 * @var AdapterInterface|null
 */
protected $cache;

/**
 * Internal cache for caching parameters for model binding during request
 *
 * @var array
 */
protected $internalCache;

/**
 * Array for original values
 *
 * @var array
 */
protected $originalValues;

```

### Methods

```php
public function __construct( AdapterInterface $cache = null );
```
Phalcon\Mvc\Model\Binder constructor

```php
public function bindToHandler( object $handler, array $params, string $cacheKey, string $methodName = null ): array;
```
Bind models into params in proper handler

```php
public function getBoundModels(): array;
```
Return the active bound models

```php
public function getCache(): AdapterInterface;
```
Sets cache instance

```php
public function getOriginalValues(): array;
```
Return the array for original values

```php
public function setCache( AdapterInterface $cache ): BinderInterface;
```
Gets cache instance

```php
protected function findBoundModel( mixed $paramValue, string $className ): mixed | bool;
```
Find the model by param value.

```php
protected function getParamsFromCache( string $cacheKey ): array | null;
```
Get params classes from cache by key

```php
protected function getParamsFromReflection( object $handler, array $params, string $cacheKey, string $methodName ): array;
```
Get modified params for handler using reflection

## Mvc\Model\Binder\BindableInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Binder/BindableInterface.zep)

-   __Namespace__

    - `Phalcon\Mvc\Model\Binder`

-   __Uses__

-   __Extends__

-   __Implements__

Phalcon\Mvc\Model\Binder\BindableInterface

Interface for bindable classes

### Methods

```php
public function getModelName(): string | array;
```
Return the model name or models names and parameters keys associated with
this class

## Mvc\Model\BinderInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/BinderInterface.zep)

-   __Namespace__

    - `Phalcon\Mvc\Model`

-   __Uses__

    - `Phalcon\Cache\Adapter\AdapterInterface`

-   __Extends__

-   __Implements__

Phalcon\Mvc\Model\BinderInterface

Interface for Phalcon\Mvc\Model\Binder

### Methods

```php
public function bindToHandler( object $handler, array $params, string $cacheKey, string $methodName = null ): array;
```
Bind models into params in proper handler

```php
public function getBoundModels(): array;
```
Gets active bound models

```php
public function getCache(): AdapterInterface;
```
Gets cache instance

```php
public function setCache( AdapterInterface $cache ): BinderInterface;
```
Sets cache instance

## Mvc\Model\Criteria 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Criteria.zep)

-   __Namespace__

    - `Phalcon\Mvc\Model`

-   __Uses__

    - `Phalcon\Db\Column`
    - `Phalcon\Di\Di`
    - `Phalcon\Di\DiInterface`
    - `Phalcon\Di\InjectionAwareInterface`
    - `Phalcon\Mvc\Model\Query\BuilderInterface`

-   __Extends__

-   __Implements__

    - `CriteriaInterface`
    - `InjectionAwareInterface`

This class is used to build the array parameter required by
Phalcon\Mvc\Model::find() and Phalcon\Mvc\Model::findFirst() using an
object-oriented interface.

```php
<?php

$invoices = Invoices::query()
->where("inv_cst_id = :customerId:")
->andWhere("inv_created_date < '2000-01-01'")
->bind(["customerId" => 1])
->limit(5, 10)
->orderBy("inv_title")
->execute();
```

### Properties
```php
/**
 * @var array
 */
protected $bindParams;

/**
 * @var array
 */
protected $bindTypes;

/**
 * @var int
 */
protected $hiddenParamNumber = ;

/**
 * @var string|null
 */
protected $model;

/**
 * @var array
 */
protected $params;

```

### Methods

```php
public function andWhere( string $conditions, mixed $bindParams = null, mixed $bindTypes = null ): CriteriaInterface;
```
Appends a condition to the current conditions using an AND operator

```php
public function betweenWhere( string $expr, mixed $minimum, mixed $maximum ): CriteriaInterface;
```
Appends a BETWEEN condition to the current conditions

```php
$criteria->betweenWhere("price", 100.25, 200.50);
```

```php
public function bind( array $bindParams, bool $merge = bool ): CriteriaInterface;
```
Sets the bound parameters in the criteria
This method replaces all previously set bound parameters

```php
public function bindTypes( array $bindTypes ): CriteriaInterface;
```
Sets the bind types in the criteria
This method replaces all previously set bound parameters

```php
public function cache( array $cache ): CriteriaInterface;
```
Sets the cache options in the criteria
This method replaces all previously set cache options

```php
public function columns( mixed $columns ): CriteriaInterface;
```
Sets the columns to be queried. The columns can be either a `string` or
an `array` of strings. If the argument is a (single, non-embedded) string,
its content can specify one or more columns, separated by commas, the same
way that one uses the SQL select statement. You can use aliases, aggregate
functions, etc. If you need to reference other models you will need to
reference them with their namespaces.

When using an array as a parameter, you will need to specify one field
per array element. If a non-numeric key is defined in the array, it will
be used as the alias in the query

```php
<?php

// String, comma separated values
$criteria->columns("id, category");

// Array, one column per element
$criteria->columns(
[
    "inv_id",
    "inv_total",
]
);

// Array with named key. The name of the key acts as an
// alias (`AS` clause)
$criteria->columns(
[
    "inv_cst_id",
    "total_invoices" => "COUNT(*)",
]
);

// Different models
$criteria->columns(
[
    "\Phalcon\Models\Invoices.*",
    "\Phalcon\Models\Customers.cst_name_first",
    "\Phalcon\Models\Customers.cst_name_last",
]
);
```

```php
public function conditions( string $conditions ): CriteriaInterface;
```
Adds the conditions parameter to the criteria

```php
public function createBuilder(): BuilderInterface;
```
Creates a query builder from criteria.

&lt;?php

$invoices = Invoices::query()
    ->where("inv_cst_id = :customerId:")
    ->bind(["customerId" => 1])
    ->createBuilder();
```

```php
public function distinct( mixed $distinct ): CriteriaInterface;
```
Sets SELECT DISTINCT / SELECT ALL flag

```php
public function execute(): ResultsetInterface;
```
Executes a find using the parameters built with the criteria

```php
public function forUpdate( bool $forUpdate = bool ): CriteriaInterface;
```
Adds the "for_update" parameter to the criteria

```php
public static function fromInput( DiInterface $container, string $modelName, array $data, string $operator = string ): CriteriaInterface;
```
Builds a Phalcon\Mvc\Model\Criteria based on an input array like $_POST

```php
public function getColumns(): string | array | null;
```
Returns the columns to be queried

```php
public function getConditions(): string | null;
```
Returns the conditions parameter in the criteria

```php
public function getDI(): DiInterface;
```
Returns the DependencyInjector container

```php
public function getGroupBy();
```
Returns the group clause in the criteria

```php
public function getHaving();
```
Returns the having clause in the criteria

```php
public function getLimit(): int | array | null;
```
Returns the limit parameter in the criteria, which will be

- An integer if 'limit' was set without an 'offset'
- An array with 'number' and 'offset' keys if an offset was set with the limit
- NULL if limit has not been set

```php
public function getModelName(): string;
```
Returns an internal model name on which the criteria will be applied

```php
public function getOrderBy(): string | null;
```
Returns the order clause in the criteria

```php
public function getParams(): array;
```
Returns all the parameters defined in the criteria

```php
public function getWhere(): string | null;
```
Returns the conditions parameter in the criteria

```php
public function groupBy( mixed $group ): CriteriaInterface;
```
Adds the group-by clause to the criteria

```php
public function having( mixed $having ): CriteriaInterface;
```
Adds the having clause to the criteria

```php
public function inWhere( string $expr, array $values ): CriteriaInterface;
```
Appends an IN condition to the current conditions

```php
$criteria->inWhere("id", [1, 2, 3]);
```

```php
public function innerJoin( string $model, mixed $conditions = null, mixed $alias = null ): CriteriaInterface;
```
Adds an INNER join to the query

```php
<?php

$criteria->innerJoin(
    Invoices::class
);

$criteria->innerJoin(
    Invoices::class,
    "inv_cst_id = Customers.cst_id"
);

$criteria->innerJoin(
    Invoices::class,
    "i.inv_cst_id = Customers.cst_id",
    "i"
);
```

```php
public function join( string $model, mixed $conditions = null, mixed $alias = null, mixed $type = null ): CriteriaInterface;
```
Adds an INNER join to the query

```php
<?php

$criteria->join(
    Invoices::class
);

$criteria->join(
    Invoices::class,
    "inv_cst_id = Customers.cst_id"
);

$criteria->join(
    Invoices::class,
    "i.inv_cst_id = Customers.cst_id",
    "i"
);

$criteria->join(
    Invoices::class,
    "i.inv_cst_id = Customers.cst_id",
    "i",
    "LEFT"
);
```

```php
public function leftJoin( string $model, mixed $conditions = null, mixed $alias = null ): CriteriaInterface;
```
Adds a LEFT join to the query

```php
<?php

$criteria->leftJoin(
    Invoices::class,
    "i.inv_cst_id = Customers.cst_id",
    "i"
);
```

```php
public function limit( int $limit, int $offset = int ): CriteriaInterface;
```
Adds the limit parameter to the criteria.

```php
$criteria->limit(100);
$criteria->limit(100, 200);
$criteria->limit("100", "200");
```

```php
public function notBetweenWhere( string $expr, mixed $minimum, mixed $maximum ): CriteriaInterface;
```
Appends a NOT BETWEEN condition to the current conditions

```php
$criteria->notBetweenWhere("price", 100.25, 200.50);
```

```php
public function notInWhere( string $expr, array $values ): CriteriaInterface;
```
Appends a NOT IN condition to the current conditions

```php
$criteria->notInWhere("id", [1, 2, 3]);
```

```php
public function orWhere( string $conditions, mixed $bindParams = null, mixed $bindTypes = null ): CriteriaInterface;
```
Appends a condition to the current conditions using an OR operator

```php
public function orderBy( string $orderColumns ): CriteriaInterface;
```
Adds the order-by clause to the criteria

```php
public function rightJoin( string $model, mixed $conditions = null, mixed $alias = null ): CriteriaInterface;
```
Adds a RIGHT join to the query

```php
<?php

$criteria->rightJoin(
    Invoices::class,
    "i.inv_cst_id = Customers.cst_id",
    "i"
);
```

```php
public function setDI( DiInterface $container ): void;
```
Sets the DependencyInjector container

```php
public function setModelName( string $modelName ): CriteriaInterface;
```
Set a model on which the query will be executed

```php
public function sharedLock( bool $sharedLock = bool ): CriteriaInterface;
```
Adds the "shared_lock" parameter to the criteria

```php
public function where( string $conditions, mixed $bindParams = null, mixed $bindTypes = null ): CriteriaInterface;
```
Sets the conditions parameter in the criteria

## Mvc\Model\CriteriaInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/CriteriaInterface.zep)

-   __Namespace__

- `Phalcon\Mvc\Model`

-   __Uses__

- `Phalcon\Di\DiInterface`

-   __Extends__

-   __Implements__

Phalcon\Mvc\Model\CriteriaInterface

Interface for Phalcon\Mvc\Model\Criteria

### Methods

```php
public function andWhere( string $conditions, mixed $bindParams = null, mixed $bindTypes = null ): CriteriaInterface;
```
Appends a condition to the current conditions using an AND operator

```php
public function betweenWhere( string $expr, mixed $minimum, mixed $maximum ): CriteriaInterface;
```
Appends a BETWEEN condition to the current conditions

```php
$criteria->betweenWhere("price", 100.25, 200.50);
```

```php
public function bind( array $bindParams ): CriteriaInterface;
```
Sets the bound parameters in the criteria
This method replaces all previously set bound parameters

```php
public function bindTypes( array $bindTypes ): CriteriaInterface;
```
Sets the bind types in the criteria
This method replaces all previously set bound parameters

```php
public function cache( array $cache ): CriteriaInterface;
```
Sets the cache options in the criteria
This method replaces all previously set cache options

```php
public function conditions( string $conditions ): CriteriaInterface;
```
Adds the conditions parameter to the criteria

```php
public function distinct( mixed $distinct ): CriteriaInterface;
```
Sets SELECT DISTINCT / SELECT ALL flag

```php
public function execute(): ResultsetInterface;
```
Executes a find using the parameters built with the criteria

```php
public function forUpdate( bool $forUpdate = bool ): CriteriaInterface;
```
Sets the "for_update" parameter to the criteria

```php
public function getColumns(): string | array | null;
```
Returns the columns to be queried

```php
public function getConditions(): string | null;
```
Returns the conditions parameter in the criteria

```php
public function getGroupBy();
```
Returns the group clause in the criteria

```php
public function getHaving();
```
Returns the having clause in the criteria

```php
public function getLimit(): int | array | null;
```
Returns the limit parameter in the criteria, which will be

- An integer if 'limit' was set without an 'offset'
- An array with 'number' and 'offset' keys if an offset was set with the limit
- NULL if limit has not been set

```php
public function getModelName(): string;
```
Returns an internal model name on which the criteria will be applied

```php
public function getOrderBy(): string | null;
```
Returns the order parameter in the criteria

```php
public function getParams(): array;
```
Returns all the parameters defined in the criteria

```php
public function getWhere(): string | null;
```
Returns the conditions parameter in the criteria

```php
public function groupBy( mixed $group ): CriteriaInterface;
```
Adds the group-by clause to the criteria

```php
public function having( mixed $having ): CriteriaInterface;
```
Adds the having clause to the criteria

```php
public function inWhere( string $expr, array $values ): CriteriaInterface;
```
Appends an IN condition to the current conditions

```php
$criteria->inWhere("id", [1, 2, 3]);
```

```php
public function innerJoin( string $model, mixed $conditions = null, mixed $alias = null ): CriteriaInterface;
```
Adds an INNER join to the query

```php
$criteria->innerJoin(
    Robots::class
);

$criteria->innerJoin(
    Robots::class,
    "r.id = RobotsParts.robots_id"
);

$criteria->innerJoin(
    Robots::class,
    "r.id = RobotsParts.robots_id",
    "r"
);
```

```php
public function leftJoin( string $model, mixed $conditions = null, mixed $alias = null ): CriteriaInterface;
```
Adds a LEFT join to the query

```php
$criteria->leftJoin(
    Robots::class,
    "r.id = RobotsParts.robots_id",
    "r"
);
```

```php
public function limit( int $limit, int $offset = int ): CriteriaInterface;
```
Sets the limit parameter to the criteria

```php
public function notBetweenWhere( string $expr, mixed $minimum, mixed $maximum ): CriteriaInterface;
```
Appends a NOT BETWEEN condition to the current conditions

```php
$criteria->notBetweenWhere("price", 100.25, 200.50);
```

```php
public function notInWhere( string $expr, array $values ): CriteriaInterface;
```
Appends a NOT IN condition to the current conditions

```php
$criteria->notInWhere("id", [1, 2, 3]);
```

```php
public function orWhere( string $conditions, mixed $bindParams = null, mixed $bindTypes = null ): CriteriaInterface;
```
Appends a condition to the current conditions using an OR operator

```php
public function orderBy( string $orderColumns ): CriteriaInterface;
```
Adds the order-by parameter to the criteria

```php
public function rightJoin( string $model, mixed $conditions = null, mixed $alias = null ): CriteriaInterface;
```
Adds a RIGHT join to the query

```php
$criteria->rightJoin(
    Robots::class,
    "r.id = RobotsParts.robots_id",
    "r"
);
```

```php
public function setModelName( string $modelName ): CriteriaInterface;
```
Set a model on which the query will be executed

```php
public function sharedLock( bool $sharedLock = bool ): CriteriaInterface;
```
Sets the "shared_lock" parameter to the criteria

```php
public function where( string $conditions, mixed $bindParams = null, mixed $bindTypes = null ): CriteriaInterface;
```
Sets the conditions parameter in the criteria

## Mvc\Model\Exception 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Exception.zep)

-   __Namespace__

- `Phalcon\Mvc\Model`

-   __Uses__

-   __Extends__

`\Exception`

-   __Implements__

Phalcon\Mvc\Model\Exception

Exceptions thrown in Phalcon\Mvc\Model\* classes will use this class

## Mvc\Model\Manager 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Manager.zep)

-   __Namespace__

- `Phalcon\Mvc\Model`

-   __Uses__

- `Phalcon\Db\Adapter\AdapterInterface`
- `Phalcon\Di\DiInterface`
- `Phalcon\Di\InjectionAwareInterface`
- `Phalcon\Events\EventsAwareInterface`
- `Phalcon\Events\ManagerInterface`
- `Phalcon\Mvc\ModelInterface`
- `Phalcon\Mvc\Model\Query\Builder`
- `Phalcon\Mvc\Model\Query\BuilderInterface`
- `Phalcon\Mvc\Model\Query\StatusInterface`
- `ReflectionClass`
- `ReflectionProperty`

-   __Extends__

-   __Implements__

- `EventsAwareInterface`
- `InjectionAwareInterface`
- `ManagerInterface`

Phalcon\Mvc\Model\Manager

This components controls the initialization of models, keeping record of
relations between the different models of the application.

A ModelsManager is injected to a model via a Dependency Injector/Services
Container such as Phalcon\Di\Di.

```php
use Phalcon\Di\Di;
use Phalcon\Mvc\Model\Manager as ModelsManager;

$di = new Di();

$di->set(
    "modelsManager",
    function() {
        return new ModelsManager();
    }
);

$robot = new Robots($di);
```

### Properties
```php
/**
 * @var array
 */
protected $aliases;

/**
 * Models' behaviors
 *
 * @var array
 */
protected $behaviors;

/**
 * Belongs to relations
 *
 * @var array
 */
protected $belongsTo;

/**
 * All the relationships by model
 *
 * @var array
 */
protected $belongsToSingle;

/**
 * @var BuilderInterface|null
 */
protected $builder;

/**
 * @var DiInterface|null
 */
protected $container;

/**
 * @var array
 */
protected $customEventsManager;

/**
 * Does the model use dynamic update, instead of updating all rows?
 *
 * @var array
 */
protected $dynamicUpdate;

/**
 * @var EventsManagerInterface|null
 */
protected $eventsManager;

/**
 * Has many relations
 *
 * @var array
 */
protected $hasMany;

/**
 * Has many relations by model
 *
 * @var array
 */
protected $hasManySingle;

/**
 * Has many-Through relations
 *
 * @var array
 */
protected $hasManyToMany;

/**
 * Has many-Through relations by model
 *
 * @var array
 */
protected $hasManyToManySingle;

/**
 * Has one relations
 *
 * @var array
 */
protected $hasOne;

/**
 * Has one relations by model
 *
 * @var array
 */
protected $hasOneSingle;

/**
 * Has one through relations
 *
 * @var array
 */
protected $hasOneThrough;

/**
 * Has one through relations by model
 *
 * @var array
 */
protected $hasOneThroughSingle;

/**
 * Mark initialized models
 *
 * @var array
 */
protected $initialized;

/**
 * @var array
 */
protected $keepSnapshots;

/**
 * Last model initialized
 *
 * @var ModelInterface|null
 */
protected $lastInitialized;

/**
 * Last query created/executed
 *
 * @var QueryInterface|null
 */
protected $lastQuery;

/**
 * @var array
 */
protected $modelVisibility;

/**
 * @var string
 */
protected $prefix = ;

/**
 * @var array
 */
protected $readConnectionServices;

/**
 * @var array
 */
protected $sources;

/**
 * @var array
 */
protected $schemas;

/**
 * @var array
 */
protected $writeConnectionServices;

/**
 * Stores a list of reusable instances
 *
 * @var array
 */
protected $reusable;

```

### Methods

```php
public function __destruct();
```
Destroys the current PHQL cache

```php
public function addBehavior( ModelInterface $model, BehaviorInterface $behavior ): void;
```
Binds a behavior to a model

```php
public function addBelongsTo( ModelInterface $model, mixed $fields, string $referencedModel, mixed $referencedFields, array $options = [] ): RelationInterface;
```
Setup a relation reverse many to one between two models

```php
public function addHasMany( ModelInterface $model, mixed $fields, string $referencedModel, mixed $referencedFields, array $options = [] ): RelationInterface;
```
Setup a relation 1-n between two models

```php
public function addHasManyToMany( ModelInterface $model, mixed $fields, string $intermediateModel, mixed $intermediateFields, mixed $intermediateReferencedFields, string $referencedModel, mixed $referencedFields, array $options = [] ): RelationInterface;
```
Setups a relation n-m between two models

```php
public function addHasOne( ModelInterface $model, mixed $fields, string $referencedModel, mixed $referencedFields, array $options = [] ): RelationInterface;
```
Setup a 1-1 relation between two models

```php
public function addHasOneThrough( ModelInterface $model, mixed $fields, string $intermediateModel, mixed $intermediateFields, mixed $intermediateReferencedFields, string $referencedModel, mixed $referencedFields, array $options = [] ): RelationInterface;
```
Setups a relation 1-1 between two models using an intermediate model

```php
public function clearReusableObjects(): void;
```
Clears the internal reusable list

```php
public function createBuilder( mixed $params = null ): BuilderInterface;
```
Creates a Phalcon\Mvc\Model\Query\Builder

```php
public function createQuery( string $phql ): QueryInterface;
```
Creates a Phalcon\Mvc\Model\Query without execute it

```php
public function executeQuery( string $phql, mixed $placeholders = null, mixed $types = null ): mixed;
```
Creates a Phalcon\Mvc\Model\Query and execute it

```php
$model = new Robots();
$manager = $model->getModelsManager();

// \Phalcon\Mvc\Model\Resultset\Simple
$manager->executeQuery('SELECTFROM Robots');

// \Phalcon\Mvc\Model\Resultset\Complex
$manager->executeQuery('SELECT COUNT(type) FROM Robots GROUP BY type');

// \Phalcon\Mvc\Model\Query\StatusInterface
$manager->executeQuery('INSERT INTO Robots (id) VALUES (1)');

// \Phalcon\Mvc\Model\Query\StatusInterface
$manager->executeQuery('UPDATE Robots SET id = 0 WHERE id = :id:', ['id' => 1]);

// \Phalcon\Mvc\Model\Query\StatusInterface
$manager->executeQuery('DELETE FROM Robots WHERE id = :id:', ['id' => 1]);
```

```php
public function existsBelongsTo( string $modelName, string $modelRelation ): bool;
```
Checks whether a model has a belongsTo relation with another model
@deprecated

```php
public function existsHasMany( string $modelName, string $modelRelation ): bool;
```
Checks whether a model has a hasMany relation with another model
@deprecated

```php
public function existsHasManyToMany( string $modelName, string $modelRelation ): bool;
```
Checks whether a model has a hasManyToMany relation with another model
@deprecated

```php
public function existsHasOne( string $modelName, string $modelRelation ): bool;
```
Checks whether a model has a hasOne relation with another model
@deprecated

```php
public function existsHasOneThrough( string $modelName, string $modelRelation ): bool;
```
Checks whether a model has a hasOneThrough relation with another model
@deprecated

```php
public function getBelongsTo( ModelInterface $model ): RelationInterface[] | array;
```
Gets all the belongsTo relations defined in a model

```php
$relations = $modelsManager->getBelongsTo(
    new Robots()
);
```

```php
public function getBelongsToRecords( string $modelName, string $modelRelation, ModelInterface $record, mixed $parameters = null, string $method = null ): ResultsetInterface | bool;
```
Gets belongsTo related records from a model

```php
public function getBuilder(): BuilderInterface | null;
```
Returns the newly created Phalcon\Mvc\Model\Query\Builder or null

```php
public function getConnectionService( ModelInterface $model, array $connectionServices ): string;
```
Returns the connection service name used to read or write data related to
a model depending on the connection services

```php
public function getCustomEventsManager( ModelInterface $model ): EventsManagerInterface | null;
```
Returns a custom events manager related to a model or null if there is
no related events manager

```php
public function getDI(): DiInterface;
```
Returns the DependencyInjector container

```php
public function getEventsManager(): EventsManagerInterface | null;
```
Returns the internal event manager

```php
public function getHasMany( ModelInterface $model ): RelationInterface[] | array;
```
Gets hasMany relations defined on a model

```php
public function getHasManyRecords( string $modelName, string $modelRelation, ModelInterface $record, mixed $parameters = null, string $method = null ): ResultsetInterface | bool;
```
Gets hasMany related records from a model

```php
public function getHasManyToMany( ModelInterface $model ): RelationInterface[] | array;
```
Gets hasManyToMany relations defined on a model

```php
public function getHasOne( ModelInterface $model ): array;
```
Gets hasOne relations defined on a model

```php
public function getHasOneAndHasMany( ModelInterface $model ): RelationInterface[];
```
Gets hasOne relations defined on a model

```php
public function getHasOneRecords( string $modelName, string $modelRelation, ModelInterface $record, mixed $parameters = null, string $method = null ): ModelInterface | bool;
```
Gets belongsTo related records from a model

```php
public function getHasOneThrough( ModelInterface $model ): RelationInterface[] | array;
```
Gets hasOneThrough relations defined on a model

```php
public function getLastInitialized(): ModelInterface;
```
Get last initialized model

```php
public function getLastQuery(): QueryInterface;
```
Returns the last query created or executed in the models manager

```php
public function getModelPrefix(): string;
```
Returns the prefix for all model sources.

```php
public function getModelSchema( ModelInterface $model ): string | null;
```
Returns the mapped schema for a model

```php
public function getModelSource( ModelInterface $model ): string;
```
Returns the mapped source for a model

```php
public function getReadConnection( ModelInterface $model ): AdapterInterface;
```
Returns the connection to read data related to a model

```php
public function getReadConnectionService( ModelInterface $model ): string;
```
Returns the connection service name used to read data related to a model

```php
public function getRelationByAlias( string $modelName, string $alias ): RelationInterface | bool;
```
Returns a relation by its alias

```php
public function getRelationRecords( RelationInterface $relation, ModelInterface $record, mixed $parameters = null, string $method = null );
```
Helper method to query records based on a relation definition

```php
public function getRelations( string $modelName ): RelationInterface[];
```
Query all the relationships defined on a model

```php
public function getRelationsBetween( string $first, string $second ): RelationInterface[] | bool;
```
Query the first relationship defined between two models

```php
public function getReusableRecords( string $modelName, string $key );
```
Returns a reusable object from the internal list

```php
public function getWriteConnection( ModelInterface $model ): AdapterInterface;
```
Returns the connection to write data related to a model

```php
public function getWriteConnectionService( ModelInterface $model ): string;
```
Returns the connection service name used to write data related to a model

```php
public function hasBelongsTo( string $modelName, string $modelRelation ): bool;
```
Checks whether a model has a belongsTo relation with another model

```php
public function hasHasMany( string $modelName, string $modelRelation ): bool;
```
Checks whether a model has a hasMany relation with another model

```php
public function hasHasManyToMany( string $modelName, string $modelRelation ): bool;
```
Checks whether a model has a hasManyToMany relation with another model

```php
public function hasHasOne( string $modelName, string $modelRelation ): bool;
```
Checks whether a model has a hasOne relation with another model

```php
public function hasHasOneThrough( string $modelName, string $modelRelation ): bool;
```
Checks whether a model has a hasOneThrough relation with another model

```php
public function initialize( ModelInterface $model ): bool;
```
Initializes a model in the model manager

```php
public function isInitialized( string $className ): bool;
```
Check whether a model is already initialized

```php
public function isKeepingSnapshots( ModelInterface $model ): bool;
```
Checks if a model is keeping snapshots for the queried records

```php
public function isUsingDynamicUpdate( ModelInterface $model ): bool;
```
Checks if a model is using dynamic update instead of all-field update

```php
final public function isVisibleModelProperty( ModelInterface $model, string $property ): bool;
```
Check whether a model property is declared as public.

```php
$isPublic = $manager->isVisibleModelProperty(
    new Robots(),
    "name"
);
```

```php
public function keepSnapshots( ModelInterface $model, bool $keepSnapshots ): void;
```
Sets if a model must keep snapshots

```php
public function load( string $modelName ): ModelInterface;
```
Loads a model throwing an exception if it doesn't exist

```php
public function missingMethod( ModelInterface $model, string $eventName, mixed $data );
```
Dispatch an event to the listeners and behaviors
This method expects that the endpoint listeners/behaviors returns true
meaning that a least one was implemented

```php
public function notifyEvent( string $eventName, ModelInterface $model );
```
Receives events generated in the models and dispatches them to an
events-manager if available. Notify the behaviors that are listening in
the model

```php
public function setConnectionService( ModelInterface $model, string $connectionService ): void;
```
Sets both write and read connection service for a model

```php
public function setCustomEventsManager( ModelInterface $model, EventsManagerInterface $eventsManager ): void;
```
Sets a custom events manager for a specific model

```php
public function setDI( DiInterface $container ): void;
```
Sets the DependencyInjector container

```php
public function setEventsManager( EventsManagerInterface $eventsManager ): void;
```
Sets a global events manager

```php
public function setModelPrefix( string $prefix ): void;
```
Sets the prefix for all model sources.

```php
use Phalcon\Mvc\Model\Manager;

$di->set(
    "modelsManager",
    function () {
        $modelsManager = new Manager();

        $modelsManager->setModelPrefix("wp_");

        return $modelsManager;
    }
);

$robots = new Robots();

echo $robots->getSource(); // wp_robots
```

$param string $prefix

```php
public function setModelSchema( ModelInterface $model, string $schema ): void;
```
Sets the mapped schema for a model

```php
public function setModelSource( ModelInterface $model, string $source ): void;
```
Sets the mapped source for a model

```php
public function setReadConnectionService( ModelInterface $model, string $connectionService ): void;
```
Sets read connection service for a model

```php
public function setReusableRecords( string $modelName, string $key, mixed $records ): void;
```
Stores a reusable record in the internal list

```php
public function setWriteConnectionService( ModelInterface $model, string $connectionService ): void;
```
Sets write connection service for a model

```php
public function useDynamicUpdate( ModelInterface $model, bool $dynamicUpdate ): void;
```
Sets if a model must use dynamic update instead of the all-field update

```php
protected function getConnection( ModelInterface $model, array $connectionServices ): AdapterInterface;
```
Returns the connection to read or write data related to a model
depending on the connection services.

```php
final protected function mergeFindParameters( mixed $findParamsOne, mixed $findParamsTwo ): array;
```
Merge two arrays of find parameters

## Mvc\Model\ManagerInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/ManagerInterface.zep)

-   __Namespace__

- `Phalcon\Mvc\Model`

-   __Uses__

- `Phalcon\Db\Adapter\AdapterInterface`
- `Phalcon\Mvc\ModelInterface`
- `Phalcon\Mvc\Model\Query\BuilderInterface`
- `Phalcon\Mvc\Model\Query\StatusInterface`

-   __Extends__

-   __Implements__

Phalcon\Mvc\Model\ManagerInterface

Interface for Phalcon\Mvc\Model\Manager

### Methods

```php
public function addBehavior( ModelInterface $model, BehaviorInterface $behavior ): void;
```
Binds a behavior to a model

```php
public function addBelongsTo( ModelInterface $model, mixed $fields, string $referencedModel, mixed $referencedFields, array $options = [] ): RelationInterface;
```
Setup a relation reverse 1-1  between two models

```php
public function addHasMany( ModelInterface $model, mixed $fields, string $referencedModel, mixed $referencedFields, array $options = [] ): RelationInterface;
```
Setup a relation 1-n between two models

```php
public function addHasManyToMany( ModelInterface $model, mixed $fields, string $intermediateModel, mixed $intermediateFields, mixed $intermediateReferencedFields, string $referencedModel, mixed $referencedFields, array $options = [] ): RelationInterface;
```
Setups a relation n-m between two models

```php
public function addHasOne( ModelInterface $model, mixed $fields, string $referencedModel, mixed $referencedFields, array $options = [] ): RelationInterface;
```
Setup a 1-1 relation between two models

```php
public function addHasOneThrough( ModelInterface $model, mixed $fields, string $intermediateModel, mixed $intermediateFields, mixed $intermediateReferencedFields, string $referencedModel, mixed $referencedFields, array $options = [] ): RelationInterface;
```
Setups a 1-1 relation between two models using an intermediate table

```php
public function createBuilder( mixed $params = null ): BuilderInterface;
```
Creates a Phalcon\Mvc\Model\Query\Builder

```php
public function createQuery( string $phql ): QueryInterface;
```
Creates a Phalcon\Mvc\Model\Query without execute it

```php
public function executeQuery( string $phql, mixed $placeholders = null, mixed $types = null ): mixed;
```
Creates a Phalcon\Mvc\Model\Query and execute it

```php
public function getBelongsTo( ModelInterface $model ): RelationInterface[] | array;
```
Gets belongsTo relations defined on a model

```php
public function getBelongsToRecords( string $modelName, string $modelRelation, ModelInterface $record, mixed $parameters = null, string $method = null ): ResultsetInterface | bool;
```
Gets belongsTo related records from a model

```php
public function getBuilder(): BuilderInterface | null;
```
Returns the newly created Phalcon\Mvc\Model\Query\Builder or null

```php
public function getHasMany( ModelInterface $model ): RelationInterface[] | array;
```
Gets hasMany relations defined on a model

```php
public function getHasManyRecords( string $modelName, string $modelRelation, ModelInterface $record, mixed $parameters = null, string $method = null ): ResultsetInterface | bool;
```
Gets hasMany related records from a model

```php
public function getHasManyToMany( ModelInterface $model ): RelationInterface[] | array;
```
Gets hasManyToMany relations defined on a model

```php
public function getHasOne( ModelInterface $model ): RelationInterface[] | array;
```
Gets hasOne relations defined on a model

```php
public function getHasOneAndHasMany( ModelInterface $model ): RelationInterface[];
```
Gets hasOne relations defined on a model

```php
public function getHasOneRecords( string $modelName, string $modelRelation, ModelInterface $record, mixed $parameters = null, string $method = null ): ModelInterface | bool;
```
Gets hasOne related records from a model

```php
public function getHasOneThrough( ModelInterface $model ): RelationInterface[] | array;
```
Gets hasOneThrough relations defined on a model

```php
public function getLastInitialized(): ModelInterface;
```
Get last initialized model

```php
public function getLastQuery(): QueryInterface;
```
Returns the last query created or executed in the models manager

```php
public function getModelSchema( ModelInterface $model ): string | null;
```
Returns the mapped schema for a model

```php
public function getModelSource( ModelInterface $model ): string;
```
Returns the mapped source for a model

```php
public function getReadConnection( ModelInterface $model ): AdapterInterface;
```
Returns the connection to read data related to a model

```php
public function getReadConnectionService( ModelInterface $model ): string;
```
Returns the connection service name used to read data related to a model

```php
public function getRelationByAlias( string $modelName, string $alias ): RelationInterface | bool;
```
Returns a relation by its alias

```php
public function getRelationRecords( RelationInterface $relation, ModelInterface $record, mixed $parameters = null, string $method = null );
```
Helper method to query records based on a relation definition

```php
public function getRelations( string $modelName ): RelationInterface[];
```
Query all the relationships defined on a model

```php
public function getRelationsBetween( string $first, string $second ): RelationInterface[] | bool;
```
Query the relations between two models

```php
public function getWriteConnection( ModelInterface $model ): AdapterInterface;
```
Returns the connection to write data related to a model

```php
public function getWriteConnectionService( ModelInterface $model ): string;
```
Returns the connection service name used to write data related to a model

```php
public function hasBelongsTo( string $modelName, string $modelRelation ): bool;
```
Checks whether a model has a belongsTo relation with another model

```php
public function hasHasMany( string $modelName, string $modelRelation ): bool;
```
Checks whether a model has a hasMany relation with another model

```php
public function hasHasManyToMany( string $modelName, string $modelRelation ): bool;
```
Checks whether a model has a hasManyToMany relation with another model

```php
public function hasHasOne( string $modelName, string $modelRelation ): bool;
```
Checks whether a model has a hasOne relation with another model

```php
public function hasHasOneThrough( string $modelName, string $modelRelation ): bool;
```
Checks whether a model has a hasOneThrough relation with another model

```php
public function initialize( ModelInterface $model );
```
Initializes a model in the model manager

```php
public function isInitialized( string $className ): bool;
```
Check of a model is already initialized

```php
public function isKeepingSnapshots( ModelInterface $model ): bool;
```
Checks if a model is keeping snapshots for the queried records

```php
public function isUsingDynamicUpdate( ModelInterface $model ): bool;
```
Checks if a model is using dynamic update instead of all-field update

```php
public function isVisibleModelProperty( ModelInterface $model, string $property ): bool;
```
Check whether a model property is declared as public.

```php
$isPublic = $manager->isVisibleModelProperty(
    new Robots(),
    "name"
);
```

```php
public function keepSnapshots( ModelInterface $model, bool $keepSnapshots ): void;
```
Sets if a model must keep snapshots

```php
public function load( string $modelName ): ModelInterface;
```
Loads a model throwing an exception if it doesn't exist

```php
public function missingMethod( ModelInterface $model, string $eventName, mixed $data );
```
Dispatch an event to the listeners and behaviors
This method expects that the endpoint listeners/behaviors returns true
meaning that a least one is implemented

```php
public function notifyEvent( string $eventName, ModelInterface $model );
```
Receives events generated in the models and dispatches them to an events-manager if available
Notify the behaviors that are listening in the model

```php
public function setConnectionService( ModelInterface $model, string $connectionService ): void;
```
Sets both write and read connection service for a model

```php
public function setModelSchema( ModelInterface $model, string $schema ): void;
```
Sets the mapped schema for a model

```php
public function setModelSource( ModelInterface $model, string $source ): void;
```
Sets the mapped source for a model

```php
public function setReadConnectionService( ModelInterface $model, string $connectionService ): void;
```
Sets read connection service for a model

```php
public function setWriteConnectionService( ModelInterface $model, string $connectionService );
```
Sets write connection service for a model

```php
public function useDynamicUpdate( ModelInterface $model, bool $dynamicUpdate ): void;
```
Sets if a model must use dynamic update instead of the all-field update

## Mvc\Model\MetaData ![Abstract](/assets/images/abstract-green.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/MetaData.zep)

-   __Namespace__

- `Phalcon\Mvc\Model`

-   __Uses__

- `Phalcon\Cache\Adapter\AdapterInterface`
- `Phalcon\Di\DiInterface`
- `Phalcon\Di\InjectionAwareInterface`
- `Phalcon\Mvc\ModelInterface`
- `Phalcon\Mvc\Model\MetaData\Strategy\Introspection`
- `Phalcon\Mvc\Model\MetaData\Strategy\StrategyInterface`

-   __Extends__

-   __Implements__

- `InjectionAwareInterface`
- `MetaDataInterface`

Phalcon\Mvc\Model\MetaData

Because Phalcon\Mvc\Model requires meta-data like field names, data types,
primary keys, etc. This component collect them and store for further
querying by Phalcon\Mvc\Model. Phalcon\Mvc\Model\MetaData can also use
adapters to store temporarily or permanently the meta-data.

A standard Phalcon\Mvc\Model\MetaData can be used to query model attributes:

```php
$metaData = new \Phalcon\Mvc\Model\MetaData\Memory();

$attributes = $metaData->getAttributes(
    new Robots()
);

print_r($attributes);
```

### Constants
```php
const MODELS_ATTRIBUTES = 0;
const MODELS_AUTOMATIC_DEFAULT_INSERT = 10;
const MODELS_AUTOMATIC_DEFAULT_UPDATE = 11;
const MODELS_COLUMN_MAP = 0;
const MODELS_DATA_TYPES = 4;
const MODELS_DATA_TYPES_BIND = 9;
const MODELS_DATA_TYPES_NUMERIC = 5;
const MODELS_DATE_AT = 6;
const MODELS_DATE_IN = 7;
const MODELS_DEFAULT_VALUES = 12;
const MODELS_EMPTY_STRING_VALUES = 13;
const MODELS_IDENTITY_COLUMN = 8;
const MODELS_NON_PRIMARY_KEY = 2;
const MODELS_NOT_NULL = 3;
const MODELS_PRIMARY_KEY = 1;
const MODELS_REVERSE_COLUMN_MAP = 1;
```

### Properties
```php
/**
 * @var CacheAdapterInterface|null
 */
protected $adapter;

/**
 * @var array
 */
protected $columnMap;

/**
 * @var DiInterface|null
 */
protected $container;

/**
 * @var array
 */
protected $metaData;

/**
 * @var StrategyInterface|null
 */
protected $strategy;

```

### Methods

```php
public function getAdapter(): CacheAdapterInterface | null;
```
Return the internal cache adapter

```php
public function getAttributes( ModelInterface $model ): array;
```
Returns table attributes names (fields)

```php
print_r(
    $metaData->getAttributes(
        new Robots()
    )
);
```

```php
public function getAutomaticCreateAttributes( ModelInterface $model ): array;
```
Returns attributes that must be ignored from the INSERT SQL generation

```php
print_r(
    $metaData->getAutomaticCreateAttributes(
        new Robots()
    )
);
```

```php
public function getAutomaticUpdateAttributes( ModelInterface $model ): array;
```
Returns attributes that must be ignored from the UPDATE SQL generation

```php
print_r(
    $metaData->getAutomaticUpdateAttributes(
        new Robots()
    )
);
```

```php
public function getBindTypes( ModelInterface $model ): array;
```
Returns attributes and their bind data types

```php
print_r(
    $metaData->getBindTypes(
        new Robots()
    )
);
```

```php
public function getColumnMap( ModelInterface $model ): array | null;
```
Returns the column map if any

```php
print_r(
    $metaData->getColumnMap(
        new Robots()
    )
);
```

```php
public function getDI(): DiInterface;
```
Returns the DependencyInjector container

```php
public function getDataTypes( ModelInterface $model ): array;
```
Returns attributes and their data types

```php
print_r(
    $metaData->getDataTypes(
        new Robots()
    )
);
```

```php
public function getDataTypesNumeric( ModelInterface $model ): array;
```
Returns attributes which types are numerical

```php
print_r(
    $metaData->getDataTypesNumeric(
        new Robots()
    )
);
```

```php
public function getDefaultValues( ModelInterface $model ): array;
```
Returns attributes (which have default values) and their default values

```php
print_r(
    $metaData->getDefaultValues(
        new Robots()
    )
);
```

```php
public function getEmptyStringAttributes( ModelInterface $model ): array;
```
Returns attributes allow empty strings

```php
print_r(
    $metaData->getEmptyStringAttributes(
        new Robots()
    )
);
```

```php
public function getIdentityField( ModelInterface $model ): string | null;
```
Returns the name of identity field (if one is present)

```php
print_r(
    $metaData->getIdentityField(
        new Robots()
    )
);
```

```php
public function getNonPrimaryKeyAttributes( ModelInterface $model ): array;
```
Returns an array of fields which are not part of the primary key

```php
print_r(
    $metaData->getNonPrimaryKeyAttributes(
        new Robots()
    )
);
```

```php
public function getNotNullAttributes( ModelInterface $model ): array;
```
Returns an array of not null attributes

```php
print_r(
    $metaData->getNotNullAttributes(
        new Robots()
    )
);
```

```php
public function getPrimaryKeyAttributes( ModelInterface $model ): array;
```
Returns an array of fields which are part of the primary key

```php
print_r(
    $metaData->getPrimaryKeyAttributes(
        new Robots()
    )
);
```

```php
public function getReverseColumnMap( ModelInterface $model ): array | null;
```
Returns the reverse column map if any

```php
print_r(
    $metaData->getReverseColumnMap(
        new Robots()
    )
);
```

```php
public function getStrategy(): StrategyInterface;
```
Return the strategy to obtain the meta-data

```php
public function hasAttribute( ModelInterface $model, string $attribute ): bool;
```
Check if a model has certain attribute

```php
var_dump(
    $metaData->hasAttribute(
        new Robots(),
        "name"
    )
);
```

```php
public function isEmpty(): bool;
```
Checks if the internal meta-data container is empty

```php
var_dump(
    $metaData->isEmpty()
);
```

```php
public function read( string $key ): array | null;
```
Reads metadata from the adapter

```php
final public function readColumnMap( ModelInterface $model ): array | null;
```
Reads the ordered/reversed column map for certain model

```php
print_r(
    $metaData->readColumnMap(
        new Robots()
    )
);
```

```php
final public function readColumnMapIndex( ModelInterface $model, int $index ): array | null;
```
Reads column-map information for certain model using a MODEL_* constant

```php
print_r(
    $metaData->readColumnMapIndex(
        new Robots(),
        MetaData::MODELS_REVERSE_COLUMN_MAP
    )
);
```

```php
final public function readMetaData( ModelInterface $model ): array | null;
```
Reads the complete meta-data for certain model

```php
print_r(
    $metaData->readMetaData(
        new Robots()
    )
);
```

```php
final public function readMetaDataIndex( ModelInterface $model, int $index ): array | null;
```
Reads meta-data for certain model

```php
print_r(
    $metaData->readMetaDataIndex(
        new Robots(),
        0
    )
);
```

```php
public function reset(): void;
```
Resets internal meta-data in order to regenerate it

```php
$metaData->reset();
```

```php
public function setAutomaticCreateAttributes( ModelInterface $model, array $attributes ): void;
```
Set the attributes that must be ignored from the INSERT SQL generation

```php
$metaData->setAutomaticCreateAttributes(
    new Robots(),
    [
        "created_at" => true,
    ]
);
```

```php
public function setAutomaticUpdateAttributes( ModelInterface $model, array $attributes ): void;
```
Set the attributes that must be ignored from the UPDATE SQL generation

```php
$metaData->setAutomaticUpdateAttributes(
    new Robots(),
    [
        "modified_at" => true,
    ]
);
```

```php
public function setDI( DiInterface $container ): void;
```
Sets the DependencyInjector container

```php
public function setEmptyStringAttributes( ModelInterface $model, array $attributes ): void;
```
Set the attributes that allow empty string values

```php
$metaData->setEmptyStringAttributes(
    new Robots(),
    [
        "name" => true,
    ]
);
```

```php
public function setStrategy( StrategyInterface $strategy ): void;
```
Set the meta-data extraction strategy

```php
public function write( string $key, array $data ): void;
```
Writes the metadata to adapter

```php
final public function writeMetaDataIndex( ModelInterface $model, int $index, mixed $data ): void;
```
Writes meta-data for certain model using a MODEL_* constant

```php
print_r(
    $metaData->writeColumnMapIndex(
        new Robots(),
        MetaData::MODELS_REVERSE_COLUMN_MAP,
        [
            "leName" => "name",
        ]
    )
);
```

```php
protected function getArrVal( array $collection, mixed $index, mixed $defaultValue = null ): mixed;
```
@todo Remove this when we get traits

```php
final protected function initialize( ModelInterface $model, mixed $key, mixed $table, mixed $schema );
```
Initialize old behaviour for compatability

```php
final protected function initializeColumnMap( ModelInterface $model, mixed $key ): bool;
```
Initialize ColumnMap for a certain table

```php
final protected function initializeMetaData( ModelInterface $model, mixed $key ): bool;
```
Initialize the metadata for certain table

## Mvc\Model\MetaData\Apcu 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/MetaData/Apcu.zep)

-   __Namespace__

- `Phalcon\Mvc\Model\MetaData`

-   __Uses__

- `Phalcon\Cache\AdapterFactory`
- `Phalcon\Mvc\Model\Exception`
- `Phalcon\Mvc\Model\MetaData`

-   __Extends__

`MetaData`

-   __Implements__

Phalcon\Mvc\Model\MetaData\Apcu

Stores model meta-data in the APCu cache. Data will erased if the web server is restarted

By default meta-data is stored for 48 hours (172800 seconds)

You can query the meta-data by printing apcu_fetch('$PMM$') or apcu_fetch('$PMM$my-app-id')

```php
$metaData = new \Phalcon\Mvc\Model\MetaData\Apcu(
    [
        "prefix"   => "my-app-id",
        "lifetime" => 86400,
    ]
);
```

### Methods

```php
public function __construct( AdapterFactory $factory, array $options = null );
```
Phalcon\Mvc\Model\MetaData\Apcu constructor

## Mvc\Model\MetaData\Libmemcached 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/MetaData/Libmemcached.zep)

-   __Namespace__

- `Phalcon\Mvc\Model\MetaData`

-   __Uses__

- `Phalcon\Cache\AdapterFactory`
- `Phalcon\Mvc\Model\Exception`
- `Phalcon\Mvc\Model\MetaData`

-   __Extends__

`MetaData`

-   __Implements__

Phalcon\Mvc\Model\MetaData\Libmemcached

Stores model meta-data in the Memcache.

By default meta-data is stored for 48 hours (172800 seconds)

### Methods

```php
public function __construct( AdapterFactory $factory, array $options = [] );
```
Phalcon\Mvc\Model\MetaData\Libmemcached constructor

```php
public function reset(): void;
```
Flush Memcache data and resets internal meta-data in order to regenerate it

## Mvc\Model\MetaData\Memory 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/MetaData/Memory.zep)

-   __Namespace__

- `Phalcon\Mvc\Model\MetaData`

-   __Uses__

- `Phalcon\Mvc\Model\Exception`
- `Phalcon\Mvc\Model\MetaData`

-   __Extends__

`MetaData`

-   __Implements__

Phalcon\Mvc\Model\MetaData\Memory

Stores model meta-data in memory. Data will be erased when the request finishes

### Methods

```php
public function __construct( mixed $options = null );
```
Phalcon\Mvc\Model\MetaData\Memory constructor

```php
public function read( string $key ): array | null;
```
Reads the meta-data from temporal memory

```php
public function write( string $key, array $data ): void;
```
Writes the meta-data to temporal memory

## Mvc\Model\MetaData\Redis 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/MetaData/Redis.zep)

-   __Namespace__

- `Phalcon\Mvc\Model\MetaData`

-   __Uses__

- `Phalcon\Cache\AdapterFactory`
- `Phalcon\Mvc\Model\MetaData`

-   __Extends__

`MetaData`

-   __Implements__

Phalcon\Mvc\Model\MetaData\Redis

Stores model meta-data in the Redis.

By default meta-data is stored for 48 hours (172800 seconds)

```php
use Phalcon\Mvc\Model\MetaData\Redis;

$metaData = new Redis(
    [
        "host"       => "127.0.0.1",
        "port"       => 6379,
        "persistent" => 0,
        "lifetime"   => 172800,
        "index"      => 2,
    ]
);
```

### Methods

```php
public function __construct( AdapterFactory $factory, array $options = [] );
```
Phalcon\Mvc\Model\MetaData\Redis constructor

```php
public function reset(): void;
```
Flush Redis data and resets internal meta-data in order to regenerate it

## Mvc\Model\MetaData\Strategy\Annotations 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/MetaData/Strategy/Annotations.zep)

-   __Namespace__

- `Phalcon\Mvc\Model\MetaData\Strategy`

-   __Uses__

- `Phalcon\Db\Column`
- `Phalcon\Di\DiInterface`
- `Phalcon\Mvc\ModelInterface`
- `Phalcon\Mvc\Model\Exception`
- `Phalcon\Mvc\Model\MetaData`

-   __Extends__

-   __Implements__

- `StrategyInterface`

This file is part of the Phalcon Framework.

(c) Phalcon Team &lt;team@phalcon.io>

For the full copyright and license information, please view the LICENSE.txt
file that was distributed with this source code.

### Methods

```php
final public function getColumnMaps( ModelInterface $model, DiInterface $container ): array;
```
Read the model's column map, this can't be inferred

```php
final public function getMetaData( ModelInterface $model, DiInterface $container ): array;
```
The meta-data is obtained by reading the column descriptions from the database information schema

## Mvc\Model\MetaData\Strategy\Introspection 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/MetaData/Strategy/Introspection.zep)

-   __Namespace__

- `Phalcon\Mvc\Model\MetaData\Strategy`

-   __Uses__

- `Phalcon\Db\Adapter\AdapterInterface`
- `Phalcon\Db\Column`
- `Phalcon\Di\DiInterface`
- `Phalcon\Mvc\ModelInterface`
- `Phalcon\Mvc\Model\Exception`
- `Phalcon\Mvc\Model\MetaData`

-   __Extends__

-   __Implements__

- `StrategyInterface`

Phalcon\Mvc\Model\MetaData\Strategy\Introspection

Queries the table meta-data in order to introspect the model's metadata

### Methods

```php
final public function getColumnMaps( ModelInterface $model, DiInterface $container ): array;
```
Read the model's column map, this can't be inferred

```php
final public function getMetaData( ModelInterface $model, DiInterface $container ): array;
```
The meta-data is obtained by reading the column descriptions from the database information schema

## Mvc\Model\MetaData\Strategy\StrategyInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/MetaData/Strategy/StrategyInterface.zep)

-   __Namespace__

- `Phalcon\Mvc\Model\MetaData\Strategy`

-   __Uses__

- `Phalcon\Di\DiInterface`
- `Phalcon\Mvc\ModelInterface`

-   __Extends__

-   __Implements__

This file is part of the Phalcon Framework.

(c) Phalcon Team &lt;team@phalcon.io>

For the full copyright and license information, please view the LICENSE.txt
file that was distributed with this source code.

### Methods

```php
public function getColumnMaps( ModelInterface $model, DiInterface $container ): array;
```
Read the model's column map, this can't be inferred

@todo Not implemented

```php
public function getMetaData( ModelInterface $model, DiInterface $container ): array;
```
The meta-data is obtained by reading the column descriptions from the database information schema

## Mvc\Model\MetaData\Stream 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/MetaData/Stream.zep)

-   __Namespace__

- `Phalcon\Mvc\Model\MetaData`

-   __Uses__

- `Phalcon\Mvc\Model\Exception`
- `Phalcon\Mvc\Model\MetaData`

-   __Extends__

`MetaData`

-   __Implements__

Phalcon\Mvc\Model\MetaData\Stream

Stores model meta-data in PHP files.

```php
$metaData = new \Phalcon\Mvc\Model\MetaData\Files(
    [
        "metaDataDir" => "app/cache/metadata/",
    ]
);
```

### Properties
```php
/**
 * @var string
 */
protected $metaDataDir = ./;

```

### Methods

```php
public function __construct( array $options = [] );
```
Phalcon\Mvc\Model\MetaData\Files constructor

```php
public function read( string $key ): array | null;
```
Reads meta-data from files

```php
public function write( string $key, array $data ): void;
```
Writes the meta-data to files

## Mvc\Model\MetaDataInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/MetaDataInterface.zep)

-   __Namespace__

- `Phalcon\Mvc\Model`

-   __Uses__

- `Phalcon\Mvc\ModelInterface`
- `Phalcon\Mvc\Model\MetaData\Strategy\StrategyInterface`

-   __Extends__

-   __Implements__

Phalcon\Mvc\Model\MetaDataInterface

Interface for Phalcon\Mvc\Model\MetaData

### Methods

```php
public function getAttributes( ModelInterface $model ): array;
```
Returns table attributes names (fields)

```php
public function getAutomaticCreateAttributes( ModelInterface $model ): array;
```
Returns attributes that must be ignored from the INSERT SQL generation

```php
public function getAutomaticUpdateAttributes( ModelInterface $model ): array;
```
Returns attributes that must be ignored from the UPDATE SQL generation

```php
public function getBindTypes( ModelInterface $model ): array;
```
Returns attributes and their bind data types

```php
public function getColumnMap( ModelInterface $model ): array | null;
```
Returns the column map if any

```php
public function getDataTypes( ModelInterface $model ): array;
```
Returns attributes and their data types

```php
public function getDataTypesNumeric( ModelInterface $model ): array;
```
Returns attributes which types are numerical

```php
public function getDefaultValues( ModelInterface $model ): array;
```
Returns attributes (which have default values) and their default values

```php
public function getEmptyStringAttributes( ModelInterface $model ): array;
```
Returns attributes allow empty strings

```php
public function getIdentityField( ModelInterface $model ): string | null;
```
Returns the name of identity field (if one is present)

```php
public function getNonPrimaryKeyAttributes( ModelInterface $model ): array;
```
Returns an array of fields which are not part of the primary key

```php
public function getNotNullAttributes( ModelInterface $model ): array;
```
Returns an array of not null attributes

```php
public function getPrimaryKeyAttributes( ModelInterface $model ): array;
```
Returns an array of fields which are part of the primary key

```php
public function getReverseColumnMap( ModelInterface $model ): array | null;
```
Returns the reverse column map if any

```php
public function getStrategy(): StrategyInterface;
```
Return the strategy to obtain the meta-data

```php
public function hasAttribute( ModelInterface $model, string $attribute ): bool;
```
Check if a model has certain attribute

```php
public function isEmpty(): bool;
```
Checks if the internal meta-data container is empty

```php
public function read( string $key ): array | null;
```
Reads meta-data from the adapter

```php
public function readColumnMap( ModelInterface $model ): array | null;
```
Reads the ordered/reversed column map for certain model

```php
public function readColumnMapIndex( ModelInterface $model, int $index ): array | null;
```
Reads column-map information for certain model using a MODEL_* constant

```php
public function readMetaData( ModelInterface $model ): array | null;
```
Reads meta-data for certain model

```php
public function readMetaDataIndex( ModelInterface $model, int $index ): array | null;
```
Reads meta-data for certain model using a MODEL_* constant

```php
public function reset();
```
Resets internal meta-data in order to regenerate it

```php
public function setAutomaticCreateAttributes( ModelInterface $model, array $attributes );
```
Set the attributes that must be ignored from the INSERT SQL generation

```php
public function setAutomaticUpdateAttributes( ModelInterface $model, array $attributes );
```
Set the attributes that must be ignored from the UPDATE SQL generation

```php
public function setEmptyStringAttributes( ModelInterface $model, array $attributes ): void;
```
Set the attributes that allow empty string values

```php
public function setStrategy( StrategyInterface $strategy );
```
Set the meta-data extraction strategy

```php
public function write( string $key, array $data ): void;
```
Writes meta-data to the adapter

```php
public function writeMetaDataIndex( ModelInterface $model, int $index, mixed $data );
```
Writes meta-data for certain model using a MODEL_* constant

## Mvc\Model\Query 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Query.zep)

-   __Namespace__

- `Phalcon\Mvc\Model`

-   __Uses__

- `Phalcon\Db\Adapter\AdapterInterface`
- `Phalcon\Db\Column`
- `Phalcon\Db\DialectInterface`
- `Phalcon\Db\RawValue`
- `Phalcon\Db\ResultInterface`
- `Phalcon\Di\DiInterface`
- `Phalcon\Di\InjectionAwareInterface`
- `Phalcon\Mvc\ModelInterface`
- `Phalcon\Mvc\Model\Query\Lang`
- `Phalcon\Mvc\Model\Query\Status`
- `Phalcon\Mvc\Model\Query\StatusInterface`
- `Phalcon\Mvc\Model\ResultsetInterface`
- `Phalcon\Mvc\Model\Resultset\Complex`
- `Phalcon\Mvc\Model\Resultset\Simple`

-   __Extends__

-   __Implements__

- `InjectionAwareInterface`
- `QueryInterface`

Phalcon\Mvc\Model\Query

This class takes a PHQL intermediate representation and executes it.

```php
$phql = "SELECT c.price*0.16 AS taxes, c.* FROM Cars AS c JOIN Brands AS b
         WHERE b.name = :name: ORDER BY c.name";

$result = $manager->executeQuery(
    $phql,
    [
        "name" => "Lamborghini",
    ]
);

foreach ($result as $row) {
    echo "Name: ",  $row->cars->name, "\n";
    echo "Price: ", $row->cars->price, "\n";
    echo "Taxes: ", $row->taxes, "\n";
}

// with transaction
use Phalcon\Mvc\Model\Query;
use Phalcon\Mvc\Model\Transaction;

// $di needs to have the service "db" registered for this to work
$di = Phalcon\Di\FactoryDefault::getDefault();

$phql = 'SELECTFROM robot';

$myTransaction = new Transaction($di);
$myTransaction->begin();

$newRobot = new Robot();
$newRobot->setTransaction($myTransaction);
$newRobot->type = "mechanical";
$newRobot->name = "Astro Boy";
$newRobot->year = 1952;
$newRobot->save();

$queryWithTransaction = new Query($phql, $di);
$queryWithTransaction->setTransaction($myTransaction);

$resultWithEntries = $queryWithTransaction->execute();

$queryWithOutTransaction = new Query($phql, $di);
$resultWithOutEntries = $queryWithTransaction->execute();
```

### Constants
```php
const TYPE_DELETE = 303;
const TYPE_INSERT = 306;
const TYPE_SELECT = 309;
const TYPE_UPDATE = 300;
```

### Properties
```php
/**
 * @var array
 * TODO: Add default value, instead of null, also remove type check
 */
protected $ast;

/**
 * @var array
 */
protected $bindParams;

/**
 * @var array
 */
protected $bindTypes;

/**
 * @var mixed|null
 */
protected $cache;

/**
 * @var array|null
 */
protected $cacheOptions;

/**
 * @var DiInterface|null
 */
protected $container;

/**
 * @var bool
 */
protected $enableImplicitJoins;

/**
 * @var array
 */
protected $intermediate;

/**
 * @var \Phalcon\Mvc\Model\ManagerInterface|null
 */
protected $manager;

/**
 * @var \Phalcon\Mvc\Model\MetaDataInterface|null
 */
protected $metaData;

/**
 * @var array
 */
protected $models;

/**
 * @var array
 */
protected $modelsInstances;

/**
 * @var int
 */
protected $nestingLevel = -1;

/**
 * @var string|null
 */
protected $phql;

/**
 * @var bool
 */
protected $sharedLock = false;

/**
 * @var array
 */
protected $sqlAliases;

/**
 * @var array
 */
protected $sqlAliasesModels;

/**
 * @var array
 */
protected $sqlAliasesModelsInstances;

/**
 * @var array
 */
protected $sqlColumnAliases;

/**
 * @var array
 */
protected $sqlModelsAliases;

/**
 * @var int|null
 */
protected $type;

/**
 * @var bool
 */
protected $uniqueRow = false;

/**
 * TransactionInterface so that the query can wrap a transaction
 * around batch updates and intermediate selects within the transaction.
 * however if a model got a transaction set inside it will use the local
 * transaction instead of this one
 *
 * @var TransactionInterface|null
 */
protected $transaction;

/**
 * @var array|null
 */
protected static $internalPhqlCache;

```

### Methods

```php
public function __construct( string $phql = null, DiInterface $container = null, array $options = [] );
```
Phalcon\Mvc\Model\Query constructor

```php
public function cache( array $cacheOptions ): QueryInterface;
```
Sets the cache parameters of the query

```php
public static function clean(): void;
```
Destroys the internal PHQL cache

```php
public function execute( array $bindParams = [], array $bindTypes = [] );
```
Executes a parsed PHQL statement

```php
public function getBindParams(): array;
```
Returns default bind params

```php
public function getBindTypes(): array;
```
Returns default bind types

```php
public function getCache(): AdapterInterface;
```
Returns the current cache backend instance

```php
public function getCacheOptions(): array;
```
Returns the current cache options

```php
public function getDI(): DiInterface;
```
Returns the dependency injection container

```php
public function getIntermediate(): array;
```
Returns the intermediate representation of the PHQL statement

```php
public function getSingleResult( array $bindParams = [], array $bindTypes = [] ): ModelInterface;
```
Executes the query returning the first result

```php
public function getSql(): array;
```
Returns the SQL to be generated by the internal PHQL (only works in
SELECT statements)

```php
public function getTransaction(): TransactionInterface | null;
```

```php
public function getType(): int;
```
Gets the type of PHQL statement executed

```php
public function getUniqueRow(): bool;
```
Check if the query is programmed to get only the first row in the
resultset

```php
public function parse(): array;
```
Parses the intermediate code produced by Phalcon\Mvc\Model\Query\Lang
generating another intermediate representation that could be executed by
Phalcon\Mvc\Model\Query

```php
public function setBindParams( array $bindParams, bool $merge = bool ): QueryInterface;
```
Set default bind parameters

```php
public function setBindTypes( array $bindTypes, bool $merge = bool ): QueryInterface;
```
Set default bind parameters

```php
public function setDI( DiInterface $container ): void;
```
Sets the dependency injection container

```php
public function setIntermediate( array $intermediate ): QueryInterface;
```
Allows to set the IR to be executed

```php
public function setSharedLock( bool $sharedLock = bool ): QueryInterface;
```
Set SHARED LOCK clause

```php
public function setTransaction( TransactionInterface $transaction ): QueryInterface;
```
allows to wrap a transaction around all queries

```php
public function setType( int $type ): QueryInterface;
```
Sets the type of PHQL statement to be executed

```php
public function setUniqueRow( bool $uniqueRow ): QueryInterface;
```
Tells to the query if only the first row in the resultset must be
returned

```php
final protected function _prepareDelete(): array;
```
Analyzes a DELETE intermediate code and produces an array to be executed
later

```php
final protected function _prepareInsert(): array;
```
Analyzes an INSERT intermediate code and produces an array to be executed
later

```php
final protected function _prepareSelect( mixed $ast = null, bool $merge = bool ): array;
```
Analyzes a SELECT intermediate code and produces an array to be executed later

```php
final protected function _prepareUpdate(): array;
```
Analyzes an UPDATE intermediate code and produces an array to be executed
later

```php
final protected function executeDelete( array $intermediate, array $bindParams, array $bindTypes ): StatusInterface;
```
Executes the DELETE intermediate representation producing a
Phalcon\Mvc\Model\Query\Status

```php
final protected function executeInsert( array $intermediate, array $bindParams, array $bindTypes ): StatusInterface;
```
Executes the INSERT intermediate representation producing a
Phalcon\Mvc\Model\Query\Status

```php
final protected function executeSelect( array $intermediate, array $bindParams, array $bindTypes, bool $simulate = bool ): ResultsetInterface | array;
```
Executes the SELECT intermediate representation producing a
Phalcon\Mvc\Model\Resultset

```php
final protected function executeUpdate( array $intermediate, array $bindParams, array $bindTypes ): StatusInterface;
```
Executes the UPDATE intermediate representation producing a
Phalcon\Mvc\Model\Query\Status

```php
final protected function getCallArgument( array $argument ): array;
```
Resolves an expression in a single call argument

```php
final protected function getCaseExpression( array $expr ): array;
```
Resolves an expression in a single call argument

```php
final protected function getExpression( array $expr, bool $quoting = bool ): array;
```
Resolves an expression from its intermediate code into an array

```php
final protected function getFunctionCall( array $expr ): array;
```
Resolves an expression in a single call argument

```php
final protected function getGroupClause( array $group ): array;
```
Returns a processed group clause for a SELECT statement

```php
final protected function getJoin( ManagerInterface $manager, array $join ): array;
```
Resolves a JOIN clause checking if the associated models exist

```php
final protected function getJoinType( array $join ): string;
```
Resolves a JOIN type

```php
final protected function getJoins( array $select ): array;
```
Processes the JOINs in the query returning an internal representation for
the database dialect

```php
final protected function getLimitClause( array $limitClause ): array;
```
Returns a processed limit clause for a SELECT statement

```php
final protected function getMultiJoin( string $joinType, mixed $joinSource, string $modelAlias, string $joinAlias, RelationInterface $relation ): array;
```
Resolves joins involving many-to-many relations

```php
final protected function getOrderClause( mixed $order ): array;
```
Returns a processed order clause for a SELECT statement

```php
final protected function getQualified( array $expr ): array;
```
Replaces the model's name to its source name in a qualified-name
expression

```php
protected function getReadConnection( ModelInterface $model, array $intermediate = null, array $bindParams = [], array $bindTypes = [] ): AdapterInterface;
```
Gets the read connection from the model if there is no transaction set
inside the query object

```php
final protected function getRelatedRecords( ModelInterface $model, array $intermediate, array $bindParams, array $bindTypes ): ResultsetInterface;
```
Query the records on which the UPDATE/DELETE operation will be done

```php
final protected function getSelectColumn( array $column ): array;
```
Resolves a column from its intermediate representation into an array
used to determine if the resultset produced is simple or complex

```php
final protected function getSingleJoin( string $joinType, mixed $joinSource, string $modelAlias, string $joinAlias, RelationInterface $relation ): array;
```
Resolves joins involving has-one/belongs-to/has-many relations

```php
final protected function getTable( ManagerInterface $manager, array $qualifiedName );
```
Resolves a table in a SELECT statement checking if the model exists

```php
protected function getWriteConnection( ModelInterface $model, array $intermediate = null, array $bindParams = [], array $bindTypes = [] ): AdapterInterface;
```
Gets the write connection from the model if there is no transaction
inside the query object

## Mvc\Model\Query\Builder 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Query/Builder.zep)

-   __Namespace__

- `Phalcon\Mvc\Model\Query`

-   __Uses__

- `Phalcon\Db\Column`
- `Phalcon\Di\Di`
- `Phalcon\Di\DiInterface`
- `Phalcon\Di\InjectionAwareInterface`
- `Phalcon\Mvc\Model\Exception`
- `Phalcon\Mvc\Model\QueryInterface`

-   __Extends__

-   __Implements__

- `BuilderInterface`
- `InjectionAwareInterface`

Helps to create PHQL queries using an OO interface

```php
$params = [
    "models"     => [
        Users::class,
    ],
    "columns"    => ["id", "name", "status"],
    "conditions" => [
        [
            "created > :min: AND created < :max:",
            [
                "min" => "2013-01-01",
                "max" => "2014-01-01",
            ],
            [
                "min" => PDO::PARAM_STR,
                "max" => PDO::PARAM_STR,
            ],
        ],
    ],
    // or "conditions" => "created > '2013-01-01' AND created < '2014-01-01'",
    "group"      => ["id", "name"],
    "having"     => "name = 'Kamil'",
    "order"      => ["name", "id"],
    "limit"      => 20,
    "offset"     => 20,
    // or "limit" => [20, 20],
];

$queryBuilder = new \Phalcon\Mvc\Model\Query\Builder($params);
```

### Properties
```php
/**
 * @var array
 */
protected $bindParams;

/**
 * @var array
 */
protected $bindTypes;

/**
 * @var array|string|null
 */
protected $columns;

/**
 * @var array|string|null
 */
protected $conditions;

/**
 * @var DiInterface|null
 */
protected $container;

/**
 * @var mixed
 */
protected $distinct;

/**
 * @var bool
 */
protected $forUpdate = false;

/**
 * @var array
 */
protected $group;

/**
 * @var string|null
 */
protected $having;

/**
 * @var int
 */
protected $hiddenParamNumber = ;

/**
 * @var array
 */
protected $joins;

/**
 * @var array|string
 */
protected $limit;

/**
 * @var array|string
 */
protected $models;

/**
 * @var int
 */
protected $offset = ;

/**
 * @var array|string
 */
protected $order;

/**
 * @var bool
 */
protected $sharedLock = false;

```

### Methods

```php
public function __construct( mixed $params = null, DiInterface $container = null );
```
Phalcon\Mvc\Model\Query\Builder constructor

```php
public function addFrom( string $model, string $alias = null ): BuilderInterface;
```
Add a model to take part of the query

```php
// Load data from models Robots
$builder->addFrom(
    Robots::class
);

// Load data from model 'Robots' using 'r' as alias in PHQL
$builder->addFrom(
    Robots::class,
    "r"
);
```

```php
public function andHaving( string $conditions, array $bindParams = [], array $bindTypes = [] ): BuilderInterface;
```
Appends a condition to the current HAVING conditions clause using a AND operator

```php
$builder->andHaving("SUM(Robots.price) > 0");

$builder->andHaving(
    "SUM(Robots.price) > :sum:",
    [
        "sum" => 100,
    ]
);
```

```php
public function andWhere( string $conditions, array $bindParams = [], array $bindTypes = [] ): BuilderInterface;
```
Appends a condition to the current WHERE conditions using a AND operator

```php
$builder->andWhere("name = 'Peter'");

$builder->andWhere(
    "name = :name: AND id > :id:",
    [
        "name" => "Peter",
        "id"   => 100,
    ]
);
```

```php
final public function autoescape( string $identifier ): string;
```
Automatically escapes identifiers but only if they need to be escaped.

```php
public function betweenHaving( string $expr, mixed $minimum, mixed $maximum, string $operator = static-constant-access ): BuilderInterface;
```
Appends a BETWEEN condition to the current HAVING conditions clause

```php
$builder->betweenHaving("SUM(Robots.price)", 100.25, 200.50);
```

```php
public function betweenWhere( string $expr, mixed $minimum, mixed $maximum, string $operator = static-constant-access ): BuilderInterface;
```
Appends a BETWEEN condition to the current WHERE conditions

```php
$builder->betweenWhere("price", 100.25, 200.50);
```

```php
public function columns( mixed $columns ): BuilderInterface;
```
Sets the columns to be queried. The columns can be either a `string` or
an `array` of strings. If the argument is a (single, non-embedded) string,
its content can specify one or more columns, separated by commas, the same
way that one uses the SQL select statement. You can use aliases, aggregate
functions, etc. If you need to reference other models you will need to
reference them with their namespaces.

When using an array as a parameter, you will need to specify one field
per array element. If a non-numeric key is defined in the array, it will
be used as the alias in the query

```php
<?php

// String, comma separated values
$builder->columns("id, category");

// Array, one column per element
$builder->columns(
    [
        "inv_id",
        "inv_total",
    ]
);

// Array with named key. The name of the key acts as an
// alias (`AS` clause)
$builder->columns(
    [
        "inv_cst_id",
        "total_invoices" => "COUNT(*)",
    ]
);

// Different models
$builder->columns(
    [
        "\Phalcon\Models\Invoices.*",
        "\Phalcon\Models\Customers.cst_name_first",
        "\Phalcon\Models\Customers.cst_name_last",
    ]
);
```

```php
public function distinct( mixed $distinct ): BuilderInterface;
```
Sets SELECT DISTINCT / SELECT ALL flag

```php
$builder->distinct("status");
$builder->distinct(null);
```

```php
public function forUpdate( bool $forUpdate ): BuilderInterface;
```
Sets a FOR UPDATE clause

```php
$builder->forUpdate(true);
```

```php
public function from( mixed $models ): BuilderInterface;
```
Sets the models who makes part of the query

```php
$builder->from(
    Robots::class
);

$builder->from(
    [
        Robots::class,
        RobotsParts::class,
    ]
);

$builder->from(
    [
        "r"  => Robots::class,
        "rp" => RobotsParts::class,
    ]
);
```

```php
public function getBindParams(): array;
```
Returns default bind params

```php
public function getBindTypes(): array;
```
Returns default bind types

```php
public function getColumns();
```
Return the columns to be queried

```php
public function getDI(): DiInterface;
```
Returns the DependencyInjector container

```php
public function getDistinct(): bool;
```
Returns SELECT DISTINCT / SELECT ALL flag

```php
public function getFrom();
```
Return the models who makes part of the query

```php
public function getGroupBy(): array;
```
Returns the GROUP BY clause

```php
public function getHaving(): string;
```
Return the current having clause

```php
public function getJoins(): array;
```
Return join parts of the query

```php
public function getLimit();
```
Returns the current LIMIT clause

```php
public function getModels(): string | array | null;
```
Returns the models involved in the query

```php
public function getOffset(): int;
```
Returns the current OFFSET clause

```php
public function getOrderBy();
```
Returns the set ORDER BY clause

```php
final public function getPhql(): string;
```
Returns a PHQL statement built based on the builder parameters

```php
public function getQuery(): QueryInterface;
```
Returns the query built

```php
public function getWhere();
```
Return the conditions for the query

```php
public function groupBy( mixed $group ): BuilderInterface;
```
Sets a GROUP BY clause

```php
$builder->groupBy(
    [
        "Robots.name",
    ]
);
```

```php
public function having( string $conditions, array $bindParams = [], array $bindTypes = [] ): BuilderInterface;
```
Sets the HAVING condition clause

```php
$builder->having("SUM(Robots.price) > 0");

$builder->having(
    "SUM(Robots.price) > :sum:",
    [
        "sum" => 100,
    ]
);
```

```php
public function inHaving( string $expr, array $values, string $operator = static-constant-access ): BuilderInterface;
```
Appends an IN condition to the current HAVING conditions clause

```php
$builder->inHaving("SUM(Robots.price)", [100, 200]);
```

```php
public function inWhere( string $expr, array $values, string $operator = static-constant-access ): BuilderInterface;
```
Appends an IN condition to the current WHERE conditions

```php
$builder->inWhere(
    "id",
    [1, 2, 3]
);
```

```php
public function innerJoin( string $model, string $conditions = null, string $alias = null ): BuilderInterface;
```
Adds an INNER join to the query

```php
// Inner Join model 'Robots' with automatic conditions and alias
$builder->innerJoin(
    Robots::class
);

// Inner Join model 'Robots' specifying conditions
$builder->innerJoin(
    Robots::class,
    "Robots.id = RobotsParts.robots_id"
);

// Inner Join model 'Robots' specifying conditions and alias
$builder->innerJoin(
    Robots::class,
    "r.id = RobotsParts.robots_id",
    "r"
);
```

```php
public function join( string $model, string $conditions = null, string $alias = null, string $type = null ): BuilderInterface;
```
Adds an :type: join (by default type - INNER) to the query

```php
// Inner Join model 'Robots' with automatic conditions and alias
$builder->join(
    Robots::class
);

// Inner Join model 'Robots' specifying conditions
$builder->join(
    Robots::class,
    "Robots.id = RobotsParts.robots_id"
);

// Inner Join model 'Robots' specifying conditions and alias
$builder->join(
    Robots::class,
    "r.id = RobotsParts.robots_id",
    "r"
);

// Left Join model 'Robots' specifying conditions, alias and type of join
$builder->join(
    Robots::class,
    "r.id = RobotsParts.robots_id",
    "r",
    "LEFT"
);
```

```php
public function leftJoin( string $model, string $conditions = null, string $alias = null ): BuilderInterface;
```
Adds a LEFT join to the query

```php
$builder->leftJoin(
    Robots::class,
    "r.id = RobotsParts.robots_id",
    "r"
);
```

```php
public function limit( int $limit, mixed $offset = null ): BuilderInterface;
```
Sets a LIMIT clause, optionally an offset clause

```php
$builder->limit(100);
$builder->limit(100, 20);
$builder->limit("100", "20");
```

```php
public function notBetweenHaving( string $expr, mixed $minimum, mixed $maximum, string $operator = static-constant-access ): BuilderInterface;
```
Appends a NOT BETWEEN condition to the current HAVING conditions clause

```php
$builder->notBetweenHaving("SUM(Robots.price)", 100.25, 200.50);
```

```php
public function notBetweenWhere( string $expr, mixed $minimum, mixed $maximum, string $operator = static-constant-access ): BuilderInterface;
```
Appends a NOT BETWEEN condition to the current WHERE conditions

```php
$builder->notBetweenWhere("price", 100.25, 200.50);
```

```php
public function notInHaving( string $expr, array $values, string $operator = static-constant-access ): BuilderInterface;
```
Appends a NOT IN condition to the current HAVING conditions clause

```php
$builder->notInHaving("SUM(Robots.price)", [100, 200]);
```

```php
public function notInWhere( string $expr, array $values, string $operator = static-constant-access ): BuilderInterface;
```
Appends a NOT IN condition to the current WHERE conditions

```php
$builder->notInWhere("id", [1, 2, 3]);
```

```php
public function offset( int $offset ): BuilderInterface;
```
Sets an OFFSET clause

```php
$builder->offset(30);
```

```php
public function orHaving( string $conditions, array $bindParams = [], array $bindTypes = [] ): BuilderInterface;
```
Appends a condition to the current HAVING conditions clause using an OR operator

```php
$builder->orHaving("SUM(Robots.price) > 0");

$builder->orHaving(
    "SUM(Robots.price) > :sum:",
    [
        "sum" => 100,
    ]
);
```

```php
public function orWhere( string $conditions, array $bindParams = [], array $bindTypes = [] ): BuilderInterface;
```
Appends a condition to the current conditions using an OR operator

```php
$builder->orWhere("name = 'Peter'");

$builder->orWhere(
    "name = :name: AND id > :id:",
    [
        "name" => "Peter",
        "id"   => 100,
    ]
);
```

```php
public function orderBy( mixed $orderBy ): BuilderInterface;
```
Sets an ORDER BY condition clause

```php
$builder->orderBy("Robots.name");
$builder->orderBy(["1", "Robots.name"]);
$builder->orderBy(["Robots.name DESC"]);
```

```php
public function rightJoin( string $model, string $conditions = null, string $alias = null ): BuilderInterface;
```
Adds a RIGHT join to the query

```php
$builder->rightJoin(
    Robots::class,
    "r.id = RobotsParts.robots_id",
    "r"
);
```

```php
public function setBindParams( array $bindParams, bool $merge = bool ): BuilderInterface;
```
Set default bind parameters

```php
public function setBindTypes( array $bindTypes, bool $merge = bool ): BuilderInterface;
```
Set default bind types

```php
public function setDI( DiInterface $container ): void;
```
Sets the DependencyInjector container

```php
public function where( string $conditions, array $bindParams = [], array $bindTypes = [] ): BuilderInterface;
```
Sets the query WHERE conditions

```php
$builder->where(100);

$builder->where("name = 'Peter'");

$builder->where(
    "name = :name: AND id > :id:",
    [
        "name" => "Peter",
        "id"   => 100,
    ]
);
```

```php
protected function conditionBetween( string $clause, string $operator, string $expr, mixed $minimum, mixed $maximum ): BuilderInterface;
```
Appends a BETWEEN condition

```php
protected function conditionIn( string $clause, string $operator, string $expr, array $values ): BuilderInterface;
```
Appends an IN condition

```php
protected function conditionNotBetween( string $clause, string $operator, string $expr, mixed $minimum, mixed $maximum ): BuilderInterface;
```
Appends a NOT BETWEEN condition

```php
protected function conditionNotIn( string $clause, string $operator, string $expr, array $values ): BuilderInterface;
```
Appends a NOT IN condition

## Mvc\Model\Query\BuilderInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Query/BuilderInterface.zep)

-   __Namespace__

- `Phalcon\Mvc\Model\Query`

-   __Uses__

- `Phalcon\Mvc\Model\QueryInterface`

-   __Extends__

-   __Implements__

Phalcon\Mvc\Model\Query\BuilderInterface

Interface for Phalcon\Mvc\Model\Query\Builder

### Constants
```php
const OPERATOR_AND = and;
const OPERATOR_OR = or;
```

### Methods

```php
public function addFrom( string $model, string $alias = null ): BuilderInterface;
```
Add a model to take part of the query

```php
public function andWhere( string $conditions, array $bindParams = [], array $bindTypes = [] ): BuilderInterface;
```
Appends a condition to the current conditions using a AND operator

```php
public function betweenWhere( string $expr, mixed $minimum, mixed $maximum, string $operator = static-constant-access ): BuilderInterface;
```
Appends a BETWEEN condition to the current conditions

```php
public function columns( mixed $columns ): BuilderInterface;
```
Sets the columns to be queried. The columns can be either a `string` or
an `array` of strings. If the argument is a (single, non-embedded) string,
its content can specify one or more columns, separated by commas, the same
way that one uses the SQL select statement. You can use aliases, aggregate
functions, etc. If you need to reference other models you will need to
reference them with their namespaces.

When using an array as a parameter, you will need to specify one field
per array element. If a non-numeric key is defined in the array, it will
be used as the alias in the query

```php
<?php

// String, comma separated values
$builder->columns("id, name");

// Array, one column per element
$builder->columns(
    [
        "id",
        "name",
    ]
);

// Array, named keys. The name of the key acts as an alias (`AS` clause)
$builder->columns(
    [
        "name",
        "number" => "COUNT(*)",
    ]
);

// Different models
$builder->columns(
    [
        "\Phalcon\Models\Invoices.*",
        "\Phalcon\Models\Customers.cst_name_first",
        "\Phalcon\Models\Customers.cst_name_last",
    ]
);
```

```php
public function distinct( mixed $distinct ): BuilderInterface;
```
Sets SELECT DISTINCT / SELECT ALL flag

```php
$builder->distinct("status");
$builder->distinct(null);
```

```php
public function forUpdate( bool $forUpdate ): BuilderInterface;
```
Sets a FOR UPDATE clause

```php
$builder->forUpdate(true);
```

```php
public function from( mixed $models ): BuilderInterface;
```
Sets the models who makes part of the query

```php
public function getBindParams(): array;
```
Returns default bind params

```php
public function getBindTypes(): array;
```
Returns default bind types

```php
public function getColumns();
```
Return the columns to be queried

```php
public function getDistinct(): bool;
```
Returns SELECT DISTINCT / SELECT ALL flag

```php
public function getFrom();
```
Return the models who makes part of the query

```php
public function getGroupBy(): array;
```
Returns the GROUP BY clause

```php
public function getHaving(): string;
```
Returns the HAVING condition clause

```php
public function getJoins(): array;
```
Return join parts of the query

```php
public function getLimit();
```
Returns the current LIMIT clause

```php
public function getModels(): string | array | null;
```
Returns the models involved in the query

```php
public function getOffset(): int;
```
Returns the current OFFSET clause

```php
public function getOrderBy();
```
Return the set ORDER BY clause

```php
public function getPhql(): string;
```
Returns a PHQL statement built based on the builder parameters

```php
public function getQuery(): QueryInterface;
```
Returns the query built

```php
public function getWhere();
```
Return the conditions for the query

```php
public function groupBy( mixed $group ): BuilderInterface;
```
Sets a GROUP BY clause

```php
public function having( string $conditions, array $bindParams = [], array $bindTypes = [] ): BuilderInterface;
```
Sets a HAVING condition clause

```php
public function inWhere( string $expr, array $values, string $operator = static-constant-access ): BuilderInterface;
```
Appends an IN condition to the current conditions

```php
public function innerJoin( string $model, string $conditions = null, string $alias = null ): BuilderInterface;
```
Adds an INNER join to the query

```php
public function join( string $model, string $conditions = null, string $alias = null ): BuilderInterface;
```
Adds an :type: join (by default type - INNER) to the query

```php
public function leftJoin( string $model, string $conditions = null, string $alias = null ): BuilderInterface;
```
Adds a LEFT join to the query

```php
public function limit( int $limit, mixed $offset = null ): BuilderInterface;
```
Sets a LIMIT clause

```php
public function notBetweenWhere( string $expr, mixed $minimum, mixed $maximum, string $operator = static-constant-access ): BuilderInterface;
```
Appends a NOT BETWEEN condition to the current conditions

```php
public function notInWhere( string $expr, array $values, string $operator = static-constant-access ): BuilderInterface;
```
Appends a NOT IN condition to the current conditions

```php
public function offset( int $offset ): BuilderInterface;
```
Sets an OFFSET clause

```php
public function orWhere( string $conditions, array $bindParams = [], array $bindTypes = [] ): BuilderInterface;
```
Appends a condition to the current conditions using an OR operator

```php
public function orderBy( mixed $orderBy ): BuilderInterface;
```
Sets an ORDER BY condition clause

```php
public function rightJoin( string $model, string $conditions = null, string $alias = null ): BuilderInterface;
```
Adds a RIGHT join to the query

```php
public function setBindParams( array $bindParams, bool $merge = bool ): BuilderInterface;
```
Set default bind parameters

```php
public function setBindTypes( array $bindTypes, bool $merge = bool ): BuilderInterface;
```
Set default bind types

```php
public function where( string $conditions, array $bindParams = [], array $bindTypes = [] ): BuilderInterface;
```
Sets conditions for the query

## Mvc\Model\Query\Lang ![Abstract](/assets/images/abstract-green.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Query/Lang.zep)

-   __Namespace__

- `Phalcon\Mvc\Model\Query`

-   __Uses__

-   __Extends__

-   __Implements__

Phalcon\Mvc\Model\Query\Lang

PHQL is implemented as a parser (written in C) that translates syntax in
that of the target RDBMS. It allows Phalcon to offer a unified SQL language to
the developer, while internally doing all the work of translating PHQL
instructions to the most optimal SQL instructions depending on the
RDBMS type associated with a model.

To achieve the highest performance possible, we wrote a parser that uses
the same technology as SQLite. This technology provides a small in-memory
parser with a very low memory footprint that is also thread-safe.

```php
use Phalcon\Mvc\Model\Query\Lang;

$intermediate = Lang::parsePHQL(
    "SELECT r.* FROM Robots r LIMIT 10"
);
```

### Methods

```php
public static function parsePHQL( string $phql ): array;
```
Parses a PHQL statement returning an intermediate representation (IR)

## Mvc\Model\Query\Status 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Query/Status.zep)

-   __Namespace__

- `Phalcon\Mvc\Model\Query`

-   __Uses__

- `Phalcon\Messages\MessageInterface`
- `Phalcon\Mvc\ModelInterface`

-   __Extends__

-   __Implements__

- `StatusInterface`

Phalcon\Mvc\Model\Query\Status

This class represents the status returned by a PHQL
statement like INSERT, UPDATE or DELETE. It offers context
information and the related messages produced by the
model which finally executes the operations when it fails

```php
$phql = "UPDATE Robots SET name = :name:, type = :type:, year = :year: WHERE id = :id:";

$status = $app->modelsManager->executeQuery(
    $phql,
    [
        "id"   => 100,
        "name" => "Astroy Boy",
        "type" => "mechanical",
        "year" => 1959,
    ]
);

// Check if the update was successful
if ($status->success()) {
    echo "OK";
}
```

### Properties
```php
/**
 * @var ModelInterface|null
 */
protected $model;

/**
 * @var bool
 */
protected $success;

```

### Methods

```php
public function __construct( bool $success, ModelInterface $model = null );
```
Phalcon\Mvc\Model\Query\Status

```php
public function getMessages(): MessageInterface[];
```
Returns the messages produced because of a failed operation

```php
public function getModel(): ModelInterface;
```
Returns the model that executed the action

```php
public function success(): bool;
```
Allows to check if the executed operation was successful

## Mvc\Model\Query\StatusInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Query/StatusInterface.zep)

-   __Namespace__

- `Phalcon\Mvc\Model\Query`

-   __Uses__

- `Phalcon\Messages\MessageInterface`
- `Phalcon\Mvc\ModelInterface`

-   __Extends__

-   __Implements__

Phalcon\Mvc\Model\Query\StatusInterface

Interface for Phalcon\Mvc\Model\Query\Status

### Methods

```php
public function getMessages(): MessageInterface[];
```
Returns the messages produced by an operation failed

```php
public function getModel(): ModelInterface;
```
Returns the model which executed the action

```php
public function success(): bool;
```
Allows to check if the executed operation was successful

## Mvc\Model\QueryInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/QueryInterface.zep)

-   __Namespace__

- `Phalcon\Mvc\Model`

-   __Uses__

- `Phalcon\Mvc\ModelInterface`

-   __Extends__

-   __Implements__

Phalcon\Mvc\Model\QueryInterface

Interface for Phalcon\Mvc\Model\Query

### Methods

```php
public function cache( array $cacheOptions ): QueryInterface;
```
Sets the cache parameters of the query

```php
public function execute( array $bindParams = [], array $bindTypes = [] );
```
Executes a parsed PHQL statement

```php
public function getBindParams(): array;
```
Returns default bind params

```php
public function getBindTypes(): array;
```
Returns default bind types

```php
public function getCacheOptions(): array;
```
Returns the current cache options

```php
public function getSingleResult( array $bindParams = [], array $bindTypes = [] ): ModelInterface;
```
Executes the query returning the first result

```php
public function getSql(): array;
```
Returns the SQL to be generated by the internal PHQL (only works in SELECT statements)

```php
public function getUniqueRow(): bool;
```
Check if the query is programmed to get only the first row in the resultset

```php
public function parse(): array;
```
Parses the intermediate code produced by Phalcon\Mvc\Model\Query\Lang generating another
intermediate representation that could be executed by Phalcon\Mvc\Model\Query

```php
public function setBindParams( array $bindParams, bool $merge = bool ): QueryInterface;
```
Set default bind parameters

```php
public function setBindTypes( array $bindTypes, bool $merge = bool ): QueryInterface;
```
Set default bind parameters

```php
public function setSharedLock( bool $sharedLock = bool ): QueryInterface;
```
Set SHARED LOCK clause

```php
public function setUniqueRow( bool $uniqueRow ): QueryInterface;
```
Tells to the query if only the first row in the resultset must be returned

## Mvc\Model\Relation 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Relation.zep)

-   __Namespace__

- `Phalcon\Mvc\Model`

-   __Uses__

-   __Extends__

-   __Implements__

- `RelationInterface`

Phalcon\Mvc\Model\Relation

This class represents a relationship between two models

### Constants
```php
const ACTION_CASCADE = 2;
const ACTION_RESTRICT = 1;
const BELONGS_TO = 0;
const HAS_MANY = 2;
const HAS_MANY_THROUGH = 4;
const HAS_ONE = 1;
const HAS_ONE_THROUGH = 3;
const NO_ACTION = 0;
```

### Properties
```php
/**
 * @var array|string
 */
protected $fields;

/**
 * @var array|string
 */
protected $intermediateFields;

/**
 * @var string|null
 */
protected $intermediateModel;

/**
 * @var array|string
 */
protected $intermediateReferencedFields;

/**
 * @var array
 */
protected $options;

/**
 * @var array|string
 */
protected $referencedFields;

/**
 * @var string
 */
protected $referencedModel;

/**
 * @var int
 */
protected $type;

```

### Methods

```php
public function __construct( int $type, string $referencedModel, mixed $fields, mixed $referencedFields, array $options = [] );
```
Phalcon\Mvc\Model\Relation constructor

```php
public function getFields();
```
Returns the fields

```php
public function getForeignKey();
```
Returns the foreign key configuration

```php
public function getIntermediateFields();
```
Gets the intermediate fields for has-*-through relations

```php
public function getIntermediateModel(): string;
```
Gets the intermediate model for has-*-through relations

```php
public function getIntermediateReferencedFields();
```
Gets the intermediate referenced fields for has-*-through relations

```php
public function getOption( string $name );
```
Returns an option by the specified name
If the option doesn't exist null is returned

```php
public function getOptions(): array;
```
Returns the options

```php
public function getParams();
```
Returns parameters that must be always used when the related records are obtained

```php
public function getReferencedFields();
```
Returns the referenced fields

```php
public function getReferencedModel(): string;
```
Returns the referenced model

```php
public function getType(): int;
```
Returns the relation type

```php
public function isForeignKey(): bool;
```
Check whether the relation act as a foreign key

```php
public function isReusable(): bool;
```
Check if records returned by getting belongs-to/has-many are implicitly cached during the current request

```php
public function isThrough(): bool;
```
Check whether the relation is a 'many-to-many' relation or not

```php
public function setIntermediateRelation( mixed $intermediateFields, string $intermediateModel, mixed $intermediateReferencedFields );
```
Sets the intermediate model data for has-*-through relations

## Mvc\Model\RelationInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/RelationInterface.zep)

-   __Namespace__

- `Phalcon\Mvc\Model`

-   __Uses__

-   __Extends__

-   __Implements__

Phalcon\Mvc\Model\RelationInterface

Interface for Phalcon\Mvc\Model\Relation

### Methods

```php
public function getFields();
```
Returns the fields

```php
public function getForeignKey();
```
Returns the foreign key configuration

```php
public function getIntermediateFields();
```
Gets the intermediate fields for has-*-through relations

```php
public function getIntermediateModel(): string;
```
Gets the intermediate model for has-*-through relations

```php
public function getIntermediateReferencedFields();
```
Gets the intermediate referenced fields for has-*-through relations

```php
public function getOption( string $name );
```
Returns an option by the specified name
If the option doesn't exist null is returned

```php
public function getOptions(): array;
```
Returns the options

```php
public function getParams();
```
Returns parameters that must be always used when the related records are obtained

```php
public function getReferencedFields();
```
Returns the referenced fields

```php
public function getReferencedModel(): string;
```
Returns the referenced model

```php
public function getType(): int;
```
Returns the relations type

```php
public function isForeignKey(): bool;
```
Check whether the relation act as a foreign key

```php
public function isReusable(): bool;
```
Check if records returned by getting belongs-to/has-many are implicitly cached during the current request

```php
public function isThrough(): bool;
```
Check whether the relation is a 'many-to-many' relation or not

```php
public function setIntermediateRelation( mixed $intermediateFields, string $intermediateModel, mixed $intermediateReferencedFields );
```
Sets the intermediate model data for has-*-through relations

## Mvc\Model\ResultInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/ResultInterface.zep)

-   __Namespace__

- `Phalcon\Mvc\Model`

-   __Uses__

- `Phalcon\Mvc\ModelInterface`

-   __Extends__

-   __Implements__

Phalcon\Mvc\Model\ResultInterface

All single objects passed as base objects to Resultsets must implement this interface

### Methods

```php
public function setDirtyState( int $dirtyState ): ModelInterface | bool;
```
Sets the object's state

## Mvc\Model\Resultset ![Abstract](/assets/images/abstract-green.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Resultset.zep)

-   __Namespace__

- `Phalcon\Mvc\Model`

-   __Uses__

- `ArrayAccess`
- `Closure`
- `Countable`
- `Iterator`
- `JsonSerializable`
- `Phalcon\Cache\CacheInterface`
- `Phalcon\Db\Enum`
- `Phalcon\Messages\MessageInterface`
- `Phalcon\Mvc\Model`
- `Phalcon\Mvc\ModelInterface`
- `Phalcon\Storage\Serializer\SerializerInterface`
- `SeekableIterator`
- `Serializable`

-   __Extends__

-   __Implements__

- `ArrayAccess`
- `Countable`
- `Iterator`
- `JsonSerializable`
- `ResultsetInterface`
- `SeekableIterator`
- `Serializable`

Phalcon\Mvc\Model\Resultset

This component allows to Phalcon\Mvc\Model returns large resultsets with the minimum memory consumption
Resultsets can be traversed using a standard foreach or a while statement. If a resultset is serialized
it will dump all the rows into a big array. Then unserialize will retrieve the rows as they were before
serializing.

```php

// Using a standard foreach
$robots = Robots::find(
    [
        "type = 'virtual'",
        "order" => "name",
    ]
);

foreach ($robots as robot) {
    echo robot->name, "\n";
}

// Using a while
$robots = Robots::find(
    [
        "type = 'virtual'",
        "order" => "name",
    ]
);

$robots->rewind();

while ($robots->valid()) {
    $robot = $robots->current();

    echo $robot->name, "\n";

    $robots->next();
}
```

### Constants
```php
const HYDRATE_ARRAYS = 1;
const HYDRATE_OBJECTS = 2;
const HYDRATE_RECORDS = 0;
const TYPE_RESULT_FULL = 0;
const TYPE_RESULT_PARTIAL = 1;
```

### Properties
```php
/**
 * @var mixed|null
 */
protected $activeRow;

/**
 * @var CacheInterface|null
 */
protected $cache;

/**
 * @var int
 */
protected $count = ;

/**
 * @var array
 */
protected $errorMessages;

/**
 * @var int
 */
protected $hydrateMode = ;

/**
 * @var bool
 */
protected $isFresh = true;

/**
 * @var int
 */
protected $pointer = ;

/**
 * @var mixed|null
 */
protected $row;

/**
 * @var array|null
 */
protected $rows;

/**
 * Phalcon\Db\ResultInterface or false for empty resultset
 *
 * @var ResultInterface|bool
 */
protected $result;

```

### Methods

```php
public function __construct( mixed $result, mixed $cache = null );
```
Phalcon\Mvc\Model\Resultset constructor

```php
final public function count(): int;
```
Counts how many rows are in the resultset

```php
public function delete( Closure $conditionCallback = null ): bool;
```
Deletes every record in the resultset

```php
public function filter( callable $filter ): ModelInterface[];
```
Filters a resultset returning only those the developer requires

```php
$filtered = $robots->filter(
    function ($robot) {
        if ($robot->id < 3) {
            return $robot;
        }
    }
);
```

```php
public function getCache(): CacheInterface | null;
```
Returns the associated cache for the resultset

```php
public function getFirst(): mixed | null;
```
Get first row in the resultset

```php
$model = new Robots();
$manager = $model->getModelsManager();

// \Robots
$manager->createQuery('SELECTFROM Robots')
        ->execute()
        ->getFirst();

// \Phalcon\Mvc\Model\Row
$manager->createQuery('SELECT r.id FROM Robots AS r')
        ->execute()
        ->getFirst();

// NULL
$manager->createQuery('SELECT r.id FROM Robots AS r WHERE r.name = "NON-EXISTENT"')
        ->execute()
        ->getFirst();
```

```php
public function getHydrateMode(): int;
```
Returns the current hydration mode

```php
public function getLast(): ModelInterface | null;
```
Get last row in the resultset

```php
public function getMessages(): MessageInterface[];
```
Returns the error messages produced by a batch operation

```php
public function getType(): int;
```
Returns the internal type of data retrieval that the resultset is using

```php
public function isFresh(): bool;
```
Tell if the resultset if fresh or an old one cached

```php
public function jsonSerialize(): array;
```
Returns serialised model objects as array for json_encode.
Calls jsonSerialize on each object if present

```php
$robots = Robots::find();

echo json_encode($robots);
```

```php
public function key(): int | null;
```
Gets pointer number of active row in the resultset

```php
public function next(): void;
```
Moves cursor to next row in the resultset

```php
public function offsetExists( mixed $index ): bool;
```
Checks whether offset exists in the resultset

```php
public function offsetGet( mixed $index ): mixed;
```
Gets row in a specific position of the resultset

```php
public function offsetSet( mixed $offset, mixed $value ): void;
```
Resultsets cannot be changed. It has only been implemented to meet the definition of the ArrayAccess interface

```php
public function offsetUnset( mixed $offset ): void;
```
Resultsets cannot be changed. It has only been implemented to meet the definition of the ArrayAccess interface

```php
final public function rewind(): void;
```
Rewinds resultset to its beginning

```php
final public function seek( mixed $position ): void;
```
Changes the internal pointer to a specific position in the resultset.
Set the new position if required, and then set this->row

```php
public function setHydrateMode( int $hydrateMode ): ResultsetInterface;
```
Sets the hydration mode in the resultset

```php
public function setIsFresh( bool $isFresh ): ResultsetInterface;
```
Set if the resultset is fresh or an old one cached

```php
public function update( mixed $data, Closure $conditionCallback = null ): bool;
```
Updates every record in the resultset

```php
public function valid(): bool;
```
Check whether internal resource has rows to fetch

## Mvc\Model\Resultset\Complex 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Resultset/Complex.zep)

-   __Namespace__

- `Phalcon\Mvc\Model\Resultset`

-   __Uses__

- `Phalcon\Db\ResultInterface`
- `Phalcon\Di\Di`
- `Phalcon\Di\DiInterface`
- `Phalcon\Mvc\Model`
- `Phalcon\Mvc\ModelInterface`
- `Phalcon\Mvc\Model\Exception`
- `Phalcon\Mvc\Model\Resultset`
- `Phalcon\Mvc\Model\ResultsetInterface`
- `Phalcon\Mvc\Model\Row`
- `Phalcon\Storage\Serializer\SerializerInterface`
- `stdClass`

-   __Extends__

`Resultset`

-   __Implements__

- `ResultsetInterface`

Phalcon\Mvc\Model\Resultset\Complex

Complex resultsets may include complete objects and scalar values.
This class builds every complex row as it is required

### Properties
```php
/**
 * @var array
 */
protected $columnTypes;

/**
 * Unserialised result-set hydrated all rows already. unserialise() sets
 * disableHydration to true
 *
 * @var bool
 */
protected $disableHydration = false;

```

### Methods

```php
public function __construct( mixed $columnTypes, ResultInterface $result = null, mixed $cache = null );
```
Phalcon\Mvc\Model\Resultset\Complex constructor

```php
public function __serialize(): array;
```

```php
public function __unserialize( array $data ): void;
```

```php
final public function current(): mixed;
```
Returns current row in the resultset

```php
public function serialize(): string;
```
Serializing a resultset will dump all related rows into a big array,
serialize it and return the resulting string

```php
public function toArray(): array;
```
Returns a complete resultset as an array, if the resultset has a big
number of rows it could consume more memory than currently it does.

```php
public function unserialize( mixed $data ): void;
```
Unserializing a resultset will allow to only works on the rows present in the saved state

## Mvc\Model\Resultset\Simple 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Resultset/Simple.zep)

-   __Namespace__

- `Phalcon\Mvc\Model\Resultset`

-   __Uses__

- `Phalcon\Di\Di`
- `Phalcon\Di\DiInterface`
- `Phalcon\Mvc\Model`
- `Phalcon\Mvc\ModelInterface`
- `Phalcon\Mvc\Model\Exception`
- `Phalcon\Mvc\Model\Resultset`
- `Phalcon\Mvc\Model\Row`
- `Phalcon\Storage\Serializer\SerializerInterface`

-   __Extends__

`Resultset`

-   __Implements__

Phalcon\Mvc\Model\Resultset\Simple

Simple resultsets only contains a complete objects
This class builds every complete object as it is required

### Properties
```php
/**
 * @var array|string
 */
protected $columnMap;

/**
 * @var ModelInterface|Row
 */
protected $model;

/**
 * @var bool
 */
protected $keepSnapshots = false;

```

### Methods

```php
public function __construct( mixed $columnMap, mixed $model, mixed $result, mixed $cache = null, bool $keepSnapshots = bool );
```
Phalcon\Mvc\Model\Resultset\Simple constructor

```php
public function __serialize(): array;
```

```php
public function __unserialize( array $data ): void;
```

```php
final public function current(): ModelInterface | null;
```
Returns current row in the resultset

```php
public function serialize(): string;
```
Serializing a resultset will dump all related rows into a big array

```php
public function toArray( bool $renameColumns = bool ): array;
```
Returns a complete resultset as an array, if the resultset has a big
number of rows it could consume more memory than currently it does.
Export the resultset to an array couldn't be faster with a large number
of records

```php
public function unserialize( mixed $data ): void;
```
Unserializing a resultset will allow to only works on the rows present in
the saved state

## Mvc\Model\ResultsetInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/ResultsetInterface.zep)

-   __Namespace__

- `Phalcon\Mvc\Model`

-   __Uses__

- `Closure`
- `Phalcon\Messages\MessageInterface`
- `Phalcon\Mvc\ModelInterface`

-   __Extends__

-   __Implements__

Phalcon\Mvc\Model\ResultsetInterface

Interface for Phalcon\Mvc\Model\Resultset

### Methods

```php
public function delete( Closure $conditionCallback = null ): bool;
```
Deletes every record in the resultset

```php
public function filter( callable $filter ): ModelInterface[];
```
Filters a resultset returning only those the developer requires

```php
$filtered = $robots->filter(
    function ($robot) {
        if ($robot->id < 3) {
            return $robot;
        }
    }
);
```

```php
public function getCache(): mixed | null;
```
Returns the associated cache for the resultset

```php
public function getFirst(): mixed | null;
```
Get first row in the resultset

```php
public function getHydrateMode(): int;
```
Returns the current hydration mode

```php
public function getLast(): ModelInterface | null;
```
Get last row in the resultset

```php
public function getMessages(): MessageInterface[];
```
Returns the error messages produced by a batch operation

```php
public function getType(): int;
```
Returns the internal type of data retrieval that the resultset is using

```php
public function isFresh(): bool;
```
Tell if the resultset if fresh or an old one cached

```php
public function setHydrateMode( int $hydrateMode ): ResultsetInterface;
```
Sets the hydration mode in the resultset

```php
public function setIsFresh( bool $isFresh ): ResultsetInterface;
```
Set if the resultset is fresh or an old one cached

```php
public function toArray(): array;
```
Returns a complete resultset as an array, if the resultset has a big number of rows
it could consume more memory than currently it does.

```php
public function update( mixed $data, Closure $conditionCallback = null ): bool;
```
Updates every record in the resultset

## Mvc\Model\Row 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Row.zep)

-   __Namespace__

- `Phalcon\Mvc\Model`

-   __Uses__

- `ArrayAccess`
- `JsonSerializable`
- `Phalcon\Mvc\EntityInterface`
- `Phalcon\Mvc\ModelInterface`

-   __Extends__

`\stdClass`

-   __Implements__

- `ArrayAccess`
- `EntityInterface`
- `JsonSerializable`
- `ResultInterface`

This component allows Phalcon\Mvc\Model to return rows without an associated entity.
This objects implements the ArrayAccess interface to allow access the object as object->x or array[x].

### Methods

```php
public function jsonSerialize(): array;
```
   Serializes the object for json_encode

```php
public function offsetExists( mixed $index ): bool;
```
Checks whether offset exists in the row

```php
public function offsetGet( mixed $index ): mixed;
```
Gets a record in a specific position of the row

```php
public function offsetSet( mixed $offset, mixed $value ): void;
```
Rows cannot be changed. It has only been implemented to meet the definition of the ArrayAccess interface

```php
public function offsetUnset( mixed $offset ): void;
```
Rows cannot be changed. It has only been implemented to meet the definition of the ArrayAccess interface

```php
public function readAttribute( string $attribute );
```
Reads an attribute value by its name

```php
echo $robot->readAttribute("name");
```

```php
public function setDirtyState( int $dirtyState ): ModelInterface | bool;
```
Set the current object's state

```php
public function toArray(): array;
```
Returns the instance as an array representation

```php
public function writeAttribute( string $attribute, mixed $value ): void;
```
Writes an attribute value by its name

```php
$robot->writeAttribute("name", "Rosey");
```

## Mvc\Model\Transaction 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Transaction.zep)

-   __Namespace__

- `Phalcon\Mvc\Model`

-   __Uses__

- `Phalcon\Db\Adapter\AdapterInterface`
- `Phalcon\Di\DiInterface`
- `Phalcon\Mvc\ModelInterface`
- `Phalcon\Mvc\Model\TransactionInterface`
- `Phalcon\Mvc\Model\Transaction\Failed`
- `Phalcon\Mvc\Model\Transaction\ManagerInterface`

-   __Extends__

-   __Implements__

- `TransactionInterface`

Phalcon\Mvc\Model\Transaction

Transactions are protective blocks where SQL statements are only permanent if
they can all succeed as one atomic action. Phalcon\Transaction is intended to
be used with Phalcon_Model_Base. Phalcon Transactions should be created using
Phalcon\Transaction\Manager.

```php
use Phalcon\Mvc\Model\Transaction\Failed;
use Phalcon\Mvc\Model\Transaction\Manager;

try {
    $manager = new Manager();

    $transaction = $manager->get();

    $robot = new Robots();

    $robot->setTransaction($transaction);

    $robot->name       = "WALL·E";
    $robot->created_at = date("Y-m-d");

    if ($robot->save() === false) {
        $transaction->rollback("Can't save robot");
    }

    $robotPart = new RobotParts();

    $robotPart->setTransaction($transaction);

    $robotPart->type = "head";

    if ($robotPart->save() === false) {
        $transaction->rollback("Can't save robot part");
    }

    $transaction->commit();
} catch(Failed $e) {
    echo "Failed, reason: ", $e->getMessage();
}
```

### Properties
```php
/**
 * @var bool
 */
protected $activeTransaction = false;

/**
 * @var AdapterInterface
 */
protected $connection;

/**
 * @var bool
 */
protected $isNewTransaction = true;

/**
 * @var ManagerInterface|null
 */
protected $manager;

/**
 * @var array
 */
protected $messages;

/**
 * @var ModelInterface|null
 */
protected $rollbackRecord;

/**
 * @var bool
 */
protected $rollbackOnAbort = false;

/**
 * @var bool
 */
protected $rollbackThrowException = false;

```

### Methods

```php
public function __construct( DiInterface $container, bool $autoBegin = bool, string $service = string );
```
Phalcon\Mvc\Model\Transaction constructor

```php
public function begin(): bool;
```
Starts the transaction

```php
public function commit(): bool;
```
Commits the transaction

```php
public function getConnection(): AdapterInterface;
```
Returns the connection related to transaction

```php
public function getMessages(): array;
```
Returns validations messages from last save try

```php
public function isManaged(): bool;
```
Checks whether transaction is managed by a transaction manager

```php
public function isValid(): bool;
```
Checks whether internal connection is under an active transaction

```php
public function rollback( string $rollbackMessage = null, ModelInterface $rollbackRecord = null ): bool;
```
Rollbacks the transaction

```php
public function setIsNewTransaction( bool $isNew ): void;
```
Sets if is a reused transaction or new once

```php
public function setRollbackOnAbort( bool $rollbackOnAbort ): void;
```
Sets flag to rollback on abort the HTTP connection

```php
public function setRollbackedRecord( ModelInterface $record ): void;
```
Sets object which generates rollback action

```php
public function setTransactionManager( ManagerInterface $manager ): void;
```
Sets transaction manager related to the transaction

```php
public function throwRollbackException( bool $status ): TransactionInterface;
```
Enables throwing exception

## Mvc\Model\Transaction\Exception 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Transaction/Exception.zep)

-   __Namespace__

- `Phalcon\Mvc\Model\Transaction`

-   __Uses__

-   __Extends__

`\Phalcon\Mvc\Model\Exception`

-   __Implements__

Phalcon\Mvc\Model\Transaction\Exception

Exceptions thrown in Phalcon\Mvc\Model\Transaction will use this class

## Mvc\Model\Transaction\Failed 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Transaction/Failed.zep)

-   __Namespace__

- `Phalcon\Mvc\Model\Transaction`

-   __Uses__

- `Phalcon\Messages\MessageInterface`
- `Phalcon\Mvc\ModelInterface`

-   __Extends__

`Exception`

-   __Implements__

Phalcon\Mvc\Model\Transaction\Failed

This class will be thrown to exit a try/catch block for isolated transactions

### Properties
```php
/**
 * @var ModelInterface|null
 */
protected $record;

```

### Methods

```php
public function __construct( string $message, ModelInterface $record = null );
```
Phalcon\Mvc\Model\Transaction\Failed constructor

```php
public function getRecord(): ModelInterface;
```
Returns validation record messages which stop the transaction

```php
public function getRecordMessages(): MessageInterface[];
```
Returns validation record messages which stop the transaction

## Mvc\Model\Transaction\Manager 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Transaction/Manager.zep)

-   __Namespace__

- `Phalcon\Mvc\Model\Transaction`

-   __Uses__

- `Phalcon\Di\Di`
- `Phalcon\Di\DiInterface`
- `Phalcon\Di\InjectionAwareInterface`
- `Phalcon\Mvc\Model\Transaction`
- `Phalcon\Mvc\Model\TransactionInterface`

-   __Extends__

-   __Implements__

- `InjectionAwareInterface`
- `ManagerInterface`

Phalcon\Mvc\Model\Transaction\Manager

A transaction acts on a single database connection. If you have multiple
class-specific databases, the transaction will not protect interaction among
them.

This class manages the objects that compose a transaction.
A transaction produces a unique connection that is passed to every object
part of the transaction.

```php
use Phalcon\Mvc\Model\Transaction\Failed;
use Phalcon\Mvc\Model\Transaction\Manager;

try {
   $transactionManager = new Manager();

   $transaction = $transactionManager->get();

   $robot = new Robots();

   $robot->setTransaction($transaction);

   $robot->name       = "WALL·E";
   $robot->created_at = date("Y-m-d");

   if ($robot->save() === false) {
       $transaction->rollback("Can't save robot");
   }

   $robotPart = new RobotParts();

   $robotPart->setTransaction($transaction);

   $robotPart->type = "head";

   if ($robotPart->save() === false) {
       $transaction->rollback("Can't save robot part");
   }

   $transaction->commit();
} catch (Failed $e) {
   echo "Failed, reason: ", $e->getMessage();
}
```

### Properties
```php
/**
 * @var DiInterface|null
 */
protected $container;

/**
 * @var bool
 */
protected $initialized = false;

/**
 * @var int
 */
protected $number = ;

/**
 * @var bool
 */
protected $rollbackPendent = true;

/**
 * @var string
 */
protected $service = db;

/**
 * @var array
 */
protected $transactions;

```

### Methods

```php
public function __construct( DiInterface $container = null );
```
Phalcon\Mvc\Model\Transaction\Manager constructor

```php
public function collectTransactions(): void;
```
Remove all the transactions from the manager

```php
public function commit();
```
Commits active transactions within the manager

```php
public function get( bool $autoBegin = bool ): TransactionInterface;
```
Returns a new \Phalcon\Mvc\Model\Transaction or an already created once
This method registers a shutdown function to rollback active connections

```php
public function getDI(): DiInterface;
```
Returns the dependency injection container

```php
public function getDbService(): string;
```
Returns the database service used to isolate the transaction

```php
public function getOrCreateTransaction( bool $autoBegin = bool ): TransactionInterface;
```
Create/Returns a new transaction or an existing one

```php
public function getRollbackPendent(): bool;
```
Check if the transaction manager is registering a shutdown function to
clean up pendent transactions

```php
public function has(): bool;
```
Checks whether the manager has an active transaction

```php
public function notifyCommit( TransactionInterface $transaction ): void;
```
Notifies the manager about a committed transaction

```php
public function notifyRollback( TransactionInterface $transaction ): void;
```
Notifies the manager about a rollbacked transaction

```php
public function rollback( bool $collect = bool ): void;
```
Rollbacks active transactions within the manager
Collect will remove the transaction from the manager

```php
public function rollbackPendent(): void;
```
Rollbacks active transactions within the manager

```php
public function setDI( DiInterface $container ): void;
```
Sets the dependency injection container

```php
public function setDbService( string $service ): ManagerInterface;
```
Sets the database service used to run the isolated transactions

```php
public function setRollbackPendent( bool $rollbackPendent ): ManagerInterface;
```
Set if the transaction manager must register a shutdown function to clean
up pendent transactions

```php
protected function collectTransaction( TransactionInterface $transaction ): void;
```
Removes transactions from the TransactionManager

## Mvc\Model\Transaction\ManagerInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/Transaction/ManagerInterface.zep)

-   __Namespace__

- `Phalcon\Mvc\Model\Transaction`

-   __Uses__

- `Phalcon\Mvc\Model\TransactionInterface`

-   __Extends__

-   __Implements__

Phalcon\Mvc\Model\Transaction\ManagerInterface

Interface for Phalcon\Mvc\Model\Transaction\Manager

### Methods

```php
public function collectTransactions(): void;
```
Remove all the transactions from the manager

```php
public function commit();
```
Commits active transactions within the manager

```php
public function get( bool $autoBegin = bool ): TransactionInterface;
```
Returns a new \Phalcon\Mvc\Model\Transaction or an already created once

```php
public function getDbService(): string;
```
Returns the database service used to isolate the transaction

```php
public function getRollbackPendent(): bool;
```
Check if the transaction manager is registering a shutdown function to clean up pendent transactions

```php
public function has(): bool;
```
Checks whether manager has an active transaction

```php
public function notifyCommit( TransactionInterface $transaction ): void;
```
Notifies the manager about a committed transaction

```php
public function notifyRollback( TransactionInterface $transaction ): void;
```
Notifies the manager about a rollbacked transaction

```php
public function rollback( bool $collect = bool ): void;
```
Rollbacks active transactions within the manager
Collect will remove transaction from the manager

```php
public function rollbackPendent(): void;
```
Rollbacks active transactions within the manager

```php
public function setDbService( string $service ): ManagerInterface;
```
Sets the database service used to run the isolated transactions

```php
public function setRollbackPendent( bool $rollbackPendent ): ManagerInterface;
```
Set if the transaction manager must register a shutdown function to clean up pendent transactions

## Mvc\Model\TransactionInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/TransactionInterface.zep)

-   __Namespace__

- `Phalcon\Mvc\Model`

-   __Uses__

- `Phalcon\Mvc\ModelInterface`
- `Phalcon\Mvc\Model\Transaction\ManagerInterface`

-   __Extends__

-   __Implements__

Phalcon\Mvc\Model\TransactionInterface

Interface for Phalcon\Mvc\Model\Transaction

### Methods

```php
public function begin(): bool;
```
Starts the transaction

```php
public function commit(): bool;
```
Commits the transaction

```php
public function getConnection(): \Phalcon\Db\Adapter\AdapterInterface;
```
Returns connection related to transaction

```php
public function getMessages(): array;
```
Returns validations messages from last save try

```php
public function isManaged(): bool;
```
Checks whether transaction is managed by a transaction manager

```php
public function isValid(): bool;
```
Checks whether internal connection is under an active transaction

```php
public function rollback( string $rollbackMessage = null, ModelInterface $rollbackRecord = null ): bool;
```
Rollbacks the transaction

```php
public function setIsNewTransaction( bool $isNew ): void;
```
Sets if is a reused transaction or new once

```php
public function setRollbackOnAbort( bool $rollbackOnAbort ): void;
```
Sets flag to rollback on abort the HTTP connection

```php
public function setRollbackedRecord( ModelInterface $record ): void;
```
Sets object which generates rollback action

```php
public function setTransactionManager( ManagerInterface $manager ): void;
```
Sets transaction manager related to the transaction

```php
public function throwRollbackException( bool $status ): TransactionInterface;
```
Enables throwing exception

## Mvc\Model\ValidationFailed 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model/ValidationFailed.zep)

-   __Namespace__

- `Phalcon\Mvc\Model`

-   __Uses__

- `Phalcon\Messages\Message`
- `Phalcon\Mvc\ModelInterface`

-   __Extends__

`Exception`

-   __Implements__

Phalcon\Mvc\Model\ValidationFailed

This exception is generated when a model fails to save a record
Phalcon\Mvc\Model must be set up to have this behavior

### Properties
```php
/**
 * @var array
 */
protected $messages;

/**
 * @var ModelInterface
 */
protected $model;

```

### Methods

```php
public function __construct( ModelInterface $model, array $validationMessages );
```
Phalcon\Mvc\Model\ValidationFailed constructor

```php
public function getMessages(): Message[];
```
Returns the complete group of messages produced in the validation

```php
public function getModel(): ModelInterface;
```
Returns the model that generated the messages

## Mvc\ModelInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/ModelInterface.zep)

-   __Namespace__

- `Phalcon\Mvc`

-   __Uses__

- `Phalcon\Db\Adapter\AdapterInterface`
- `Phalcon\Di\DiInterface`
- `Phalcon\Messages\MessageInterface`
- `Phalcon\Mvc\Model\CriteriaInterface`
- `Phalcon\Mvc\Model\MetaDataInterface`
- `Phalcon\Mvc\Model\ResultsetInterface`
- `Phalcon\Mvc\Model\TransactionInterface`

-   __Extends__

-   __Implements__

Phalcon\Mvc\ModelInterface

Interface for Phalcon\Mvc\Model

### Methods

```php
public function appendMessage( MessageInterface $message ): ModelInterface;
```
Appends a customized message on the validation process

```php
public function assign( array $data, mixed $whiteList = null, mixed $dataColumnMap = null ): ModelInterface;
```
Assigns values to a model from an array

```php
public static function average( array $parameters = [] ): double | ResultsetInterface;
```
Allows to calculate the average value on a column matching the specified
conditions

```php
public static function cloneResult( ModelInterface $base, array $data, int $dirtyState = int ): ModelInterface;
```
Assigns values to a model from an array returning a new model

```php
public static function cloneResultMap( mixed $base, array $data, mixed $columnMap, int $dirtyState = int, bool $keepSnapshots = bool ): ModelInterface;
```
Assigns values to a model from an array returning a new model

```php
public static function cloneResultMapHydrate( array $data, mixed $columnMap, int $hydrationMode );
```
Returns an hydrated result based on the data and the column map

```php
public static function count( mixed $parameters = null ): int | ResultsetInterface;
```
Allows to count how many records match the specified conditions

Returns an integer for simple queries or a ResultsetInterface
instance for when the GROUP condition is used. The results will
contain the count of each group.

```php
public function create(): bool;
```
Inserts a model instance. If the instance already exists in the
persistence it will throw an exception. Returning true on success or
false otherwise.

```php
public function delete(): bool;
```
Deletes a model instance. Returning true on success or false otherwise.

```php
public static function find( mixed $parameters = null ): ResultsetInterface;
```
Allows to query a set of records that match the specified conditions

```php
public static function findFirst( mixed $parameters = null ): mixed | null;
```
Allows to query the first record that match the specified conditions

```php
public function fireEvent( string $eventName ): bool;
```
Fires an event, implicitly calls behaviors and listeners in the events
manager are notified

```php
public function fireEventCancel( string $eventName ): bool;
```
Fires an event, implicitly calls behaviors and listeners in the events
manager are notified. This method stops if one of the callbacks/listeners
returns bool false

```php
public function getDirtyState(): int;
```
Returns one of the DIRTY_STATE_* constants telling if the record exists
in the database or not

```php
public function getMessages(): MessageInterface[];
```
Returns array of validation messages

```php
public function getModelsMetaData(): MetaDataInterface;
```
Returns the models meta-data service related to the entity instance.

```php
public function getOperationMade(): int;
```
Returns the type of the latest operation performed by the ORM
Returns one of the OP_* class constants

```php
public function getReadConnection(): AdapterInterface;
```
Gets internal database connection

```php
public function getReadConnectionService(): string;
```
Returns DependencyInjection connection service used to read data

```php
public function getRelated( string $alias, mixed $arguments = null );
```
Returns related records based on defined relations

```php
public function getSchema(): string | null;
```
Returns schema name where table mapped is located

```php
public function getSource(): string;
```
Returns table name mapped in the model

```php
public function getWriteConnection(): AdapterInterface;
```
Gets internal database connection

```php
public function getWriteConnectionService(): string;
```
Returns DependencyInjection connection service used to write data

```php
public static function maximum( mixed $parameters = null ): mixed;
```
Allows to get the maximum value of a column that match the specified
conditions

```php
public static function minimum( mixed $parameters = null ): mixed;
```
Allows to get the minimum value of a column that match the specified
conditions

```php
public static function query( DiInterface $container = null ): CriteriaInterface;
```
Create a criteria for a specific model

```php
public function refresh(): ModelInterface;
```
Refreshes the model attributes re-querying the record from the database

```php
public function save(): bool;
```
Inserts or updates a model instance. Returning true on success or false
otherwise.

```php
public function setConnectionService( string $connectionService ): void;
```
Sets both read/write connection services

```php
public function setDirtyState( int $dirtyState ): ModelInterface | bool;
```
Sets the dirty state of the object using one of the DIRTY_STATE_*
constants

```php
public function setReadConnectionService( string $connectionService ): void;
```
Sets the DependencyInjection connection service used to read data

```php
public function setSnapshotData( array $data, mixed $columnMap = null ): void;
```
Sets the record's snapshot data. This method is used internally to set
snapshot data when the model was set up to keep snapshot data

```php
public function setTransaction( TransactionInterface $transaction ): ModelInterface;
```
Sets a transaction related to the Model instance

```php
public function setWriteConnectionService( string $connectionService ): void;
```
Sets the DependencyInjection connection service used to write data

```php
public function skipOperation( bool $skip ): void;
```
Skips the current operation forcing a success state

```php
public static function sum( mixed $parameters = null ): double | ResultsetInterface;
```
Allows to calculate a sum on a column that match the specified conditions

```php
public function update(): bool;
```
Updates a model instance. If the instance doesn't exist in the
persistence it will throw an exception. Returning true on success or
false otherwise.

```php
public function validationHasFailed(): bool;
```
Check whether validation process has generated any messages

## Mvc\ModuleDefinitionInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/ModuleDefinitionInterface.zep)

-   __Namespace__

- `Phalcon\Mvc`

-   __Uses__

- `Phalcon\Di\DiInterface`

-   __Extends__

-   __Implements__

Phalcon\Mvc\ModuleDefinitionInterface

This interface must be implemented by class module definitions

### Methods

```php
public function registerAutoloaders( DiInterface $container = null );
```
Registers an autoloader related to the module

```php
public function registerServices( DiInterface $container );
```
Registers services related to the module

## Mvc\Router 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Router.zep)

-   __Namespace__

- `Phalcon\Mvc`

-   __Uses__

- `Phalcon\Di\AbstractInjectionAware`
- `Phalcon\Di\DiInterface`
- `Phalcon\Events\EventsAwareInterface`
- `Phalcon\Events\ManagerInterface`
- `Phalcon\Http\RequestInterface`
- `Phalcon\Mvc\Router\Exception`
- `Phalcon\Mvc\Router\GroupInterface`
- `Phalcon\Mvc\Router\Route`
- `Phalcon\Mvc\Router\RouteInterface`

-   __Extends__

`AbstractInjectionAware`

-   __Implements__

- `EventsAwareInterface`
- `RouterInterface`

Phalcon\Mvc\Router

Phalcon\Mvc\Router is the standard framework router. Routing is the
process of taking a URI endpoint (that part of the URI which comes after the
base URL) and decomposing it into parameters to determine which module,
controller, and action of that controller should receive the request

```php
use Phalcon\Mvc\Router;

$router = new Router();

$router->add(
    "/documentation/{chapter}/{name}\.{type:[a-z]+}",
    [
        "controller" => "documentation",
        "action"     => "show",
    ]
);

$router->handle(
    "/documentation/1/examples.html"
);

echo $router->getControllerName();
```

### Constants
```php
const POSITION_FIRST = 0;
const POSITION_LAST = 1;
```

### Properties
```php
/**
 * @var string
 */
protected $action = ;

/**
 * @var string
 */
protected $controller = ;

/**
 * @var string
 */
protected $defaultAction = ;

/**
 * @var string
 */
protected $defaultController = ;

/**
 * @var string
 */
protected $defaultModule = ;

/**
 * @var string
 */
protected $defaultNamespace = ;

/**
 * @var array
 */
protected $defaultParams;

/**
 * @var ManagerInterface|null
 */
protected $eventsManager;

/**
 * @var array
 */
protected $keyRouteNames;

/**
 * @var array
 */
protected $keyRouteIds;

/**
 * @var RouteInterface|null
 */
protected $matchedRoute;

/**
 * @var array
 */
protected $matches;

/**
 * @var string
 */
protected $module = ;

/**
 * @var string
 */
protected $namespaceName = ;

/**
 * @var array|string|null
 */
protected $notFoundPaths;

/**
 * @var array
 */
protected $params;

/**
 * @var bool
 */
protected $removeExtraSlashes = false;

/**
 * @var array
 */
protected $routes;

/**
 * @var bool
 */
protected $wasMatched = false;

```

### Methods

```php
public function __construct( bool $defaultRoutes = bool );
```
Phalcon\Mvc\Router constructor

```php
public function add( string $pattern, mixed $paths = null, mixed $httpMethods = null, int $position = static-constant-access ): RouteInterface;
```
Adds a route to the router without any HTTP constraint

```php
use Phalcon\Mvc\Router;

$router->add("/about", "About::index");

$router->add(
    "/about",
    "About::index",
    ["GET", "POST"]
);

$router->add(
    "/about",
    "About::index",
    ["GET", "POST"],
    Router::POSITION_FIRST
);
```

```php
public function addConnect( string $pattern, mixed $paths = null, int $position = static-constant-access ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is CONNECT

```php
public function addDelete( string $pattern, mixed $paths = null, int $position = static-constant-access ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is DELETE

```php
public function addGet( string $pattern, mixed $paths = null, int $position = static-constant-access ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is GET

```php
public function addHead( string $pattern, mixed $paths = null, int $position = static-constant-access ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is HEAD

```php
public function addOptions( string $pattern, mixed $paths = null, int $position = static-constant-access ): RouteInterface;
```
Add a route to the router that only match if the HTTP method is OPTIONS

```php
public function addPatch( string $pattern, mixed $paths = null, int $position = static-constant-access ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is PATCH

```php
public function addPost( string $pattern, mixed $paths = null, int $position = static-constant-access ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is POST

```php
public function addPurge( string $pattern, mixed $paths = null, int $position = static-constant-access ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is PURGE
(Squid and Varnish support)

```php
public function addPut( string $pattern, mixed $paths = null, int $position = static-constant-access ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is PUT

```php
public function addTrace( string $pattern, mixed $paths = null, int $position = static-constant-access ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is TRACE

```php
public function attach( RouteInterface $route, int $position = static-constant-access ): RouterInterface;
```
Attach Route object to the routes stack.

```php
use Phalcon\Mvc\Router;
use Phalcon\Mvc\Router\Route;

class CustomRoute extends Route {
     // ...
}

$router = new Router();

$router->attach(
    new CustomRoute("/about", "About::index", ["GET", "HEAD"]),
    Router::POSITION_FIRST
);
```

```php
public function clear(): void;
```
Removes all the pre-defined routes

```php
public function getActionName(): string;
```
Returns the processed action name

```php
public function getControllerName(): string;
```
Returns the processed controller name

```php
public function getDefaults(): array;
```
Returns an array of default parameters

```php
public function getEventsManager(): ManagerInterface | null;
```
Returns the internal event manager

```php
public function getKeyRouteIds(): array;
```

```php
public function getKeyRouteNames(): array;
```

```php
public function getMatchedRoute(): RouteInterface | null;
```
Returns the route that matches the handled URI

```php
public function getMatches(): array;
```
Returns the sub expressions in the regular expression matched

```php
public function getModuleName(): string;
```
Returns the processed module name

```php
public function getNamespaceName(): string;
```
Returns the processed namespace name

```php
public function getParams(): array;
```
Returns the processed parameters

```php
public function getRouteById( mixed $id ): RouteInterface | bool;
```
Returns a route object by its id

```php
public function getRouteByName( string $name ): RouteInterface | bool;
```
Returns a route object by its name

```php
public function getRoutes(): RouteInterface[];
```
Returns all the routes defined in the router

```php
public function handle( string $uri ): void;
```
Handles routing information received from the rewrite engine

```php
// Passing a URL
$router->handle("/posts/edit/1");
```

```php
public function isExactControllerName(): bool;
```
Returns whether controller name should not be mangled

```php
public function mount( GroupInterface $group ): RouterInterface;
```
Mounts a group of routes in the router

```php
public function notFound( mixed $paths ): RouterInterface;
```
Set a group of paths to be returned when none of the defined routes are
matched

```php
public function removeExtraSlashes( bool $remove ): RouterInterface;
```
Set whether router must remove the extra slashes in the handled routes

```php
public function setDefaultAction( string $actionName ): RouterInterface;
```
Sets the default action name

```php
public function setDefaultController( string $controllerName ): RouterInterface;
```
Sets the default controller name

```php
public function setDefaultModule( string $moduleName ): RouterInterface;
```
Sets the name of the default module

```php
public function setDefaultNamespace( string $namespaceName ): RouterInterface;
```
Sets the name of the default namespace

@parma string namespaceName

```php
public function setDefaults( array $defaults ): RouterInterface;
```
Sets an array of default paths. If a route is missing a path the router
will use the defined here. This method must not be used to set a 404
route

```php
$router->setDefaults(
    [
        "module" => "common",
        "action" => "index",
    ]
);
```

```php
public function setEventsManager( ManagerInterface $eventsManager ): void;
```
Sets the events manager

```php
public function setKeyRouteIds( array $routeIds ): Router;
```

```php
public function setKeyRouteNames( array $routeNames ): Router;
```

```php
public function wasMatched(): bool;
```
Checks if the router matches any of the defined routes

## Mvc\Router\Annotations 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Router/Annotations.zep)

-   __Namespace__

- `Phalcon\Mvc\Router`

-   __Uses__

- `Phalcon\Annotations\Annotation`
- `Phalcon\Di\DiInterface`
- `Phalcon\Mvc\Router`

-   __Extends__

`Router`

-   __Implements__

Phalcon\Mvc\Router\Annotations

A router that reads routes annotations from classes/resources

```php
use Phalcon\Mvc\Router\Annotations;

$di->setShared(
    "router",
    function() {
        // Use the annotations router
        $router = new Annotations(false);

        // This will do the same as above but only if the handled uri starts with /robots
        $router->addResource("Robots", "/robots");

        return $router;
    }
);
```

### Properties
```php
/**
 * @var string
 */
protected $actionSuffix = Action;

/**
 * @var callable|string|null
 */
protected $actionPreformatCallback;

/**
 * @var string
 */
protected $controllerSuffix = Controller;

/**
 * @var array
 */
protected $handlers;

/**
 * @var string
 */
protected $routePrefix = ;

```

### Methods

```php
public function addModuleResource( string $module, string $handler, string $prefix = null ): Annotations;
```
Adds a resource to the annotations handler
A resource is a class that contains routing annotations
The class is located in a module

```php
public function addResource( string $handler, string $prefix = null ): Annotations;
```
Adds a resource to the annotations handler
A resource is a class that contains routing annotations

```php
public function getActionPreformatCallback();
```

```php
public function getResources(): array;
```
Return the registered resources

```php
public function handle( string $uri ): void;
```
Produce the routing parameters from the rewrite information

```php
public function processActionAnnotation( string $module, string $namespaceName, string $controller, string $action, Annotation $annotation ): void;
```
Checks for annotations in the public methods of the controller

```php
public function processControllerAnnotation( string $handler, Annotation $annotation );
```
Checks for annotations in the controller docblock

```php
public function setActionPreformatCallback( mixed $callback = null );
```
Sets the action preformat callback
$action here already without suffix 'Action'

```php
// Array as callback
$annotationRouter->setActionPreformatCallback(
     [
         new Uncamelize(),
         '__invoke'
     ]
 );

// Function as callback
$annotationRouter->setActionPreformatCallback(
    function ($action) {
        return $action;
    }
);

// String as callback
$annotationRouter->setActionPreformatCallback('strtolower');

// If empty method constructor called [null], sets uncamelize with - delimiter
$annotationRouter->setActionPreformatCallback();
```

```php
public function setActionSuffix( string $actionSuffix );
```
Changes the action method suffix

```php
public function setControllerSuffix( string $controllerSuffix );
```
Changes the controller class suffix

## Mvc\Router\Exception 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Router/Exception.zep)

-   __Namespace__

- `Phalcon\Mvc\Router`

-   __Uses__

-   __Extends__

`\Exception`

-   __Implements__

Phalcon\Mvc\Router\Exception

Exceptions thrown in Phalcon\Mvc\Router will use this class

## Mvc\Router\Group 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Router/Group.zep)

-   __Namespace__

- `Phalcon\Mvc\Router`

-   __Uses__

-   __Extends__

-   __Implements__

- `GroupInterface`

Phalcon\Mvc\Router\Group

Helper class to create a group of routes with common attributes

```php
$router = new \Phalcon\Mvc\Router();

//Create a group with a common module and controller
$blog = new Group(
    [
        "module"     => "blog",
        "controller" => "index",
    ]
);

//All the routes start with /blog
$blog->setPrefix("/blog");

//Add a route to the group
$blog->add(
    "/save",
    [
        "action" => "save",
    ]
);

//Add another route to the group
$blog->add(
    "/edit/{id}",
    [
        "action" => "edit",
    ]
);

//This route maps to a controller different than the default
$blog->add(
    "/blog",
    [
        "controller" => "about",
        "action"     => "index",
    ]
);

//Add the group to the router
$router->mount($blog);
```

### Properties
```php
/**
 * @var callable|null
 */
protected $beforeMatch;

/**
 * @var string|null
 */
protected $hostname;

/**
 * @var array|string|null
 */
protected $paths;

/**
 * @var string|null
 */
protected $prefix;

/**
 * @var array
 */
protected $routes;

```

### Methods

```php
public function __construct( mixed $paths = null );
```
Phalcon\Mvc\Router\Group constructor

```php
public function add( string $pattern, mixed $paths = null, mixed $httpMethods = null ): RouteInterface;
```
Adds a route to the router on any HTTP method

```php
$router->add("/about", "About::index");
```

```php
public function addConnect( string $pattern, mixed $paths = null ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is CONNECT

```php
public function addDelete( string $pattern, mixed $paths = null ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is DELETE

```php
public function addGet( string $pattern, mixed $paths = null ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is GET

```php
public function addHead( string $pattern, mixed $paths = null ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is HEAD

```php
public function addOptions( string $pattern, mixed $paths = null ): RouteInterface;
```
Add a route to the router that only match if the HTTP method is OPTIONS

```php
public function addPatch( string $pattern, mixed $paths = null ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is PATCH

```php
public function addPost( string $pattern, mixed $paths = null ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is POST

```php
public function addPurge( string $pattern, mixed $paths = null ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is PURGE

```php
public function addPut( string $pattern, mixed $paths = null ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is PUT

```php
public function addTrace( string $pattern, mixed $paths = null ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is TRACE

```php
public function beforeMatch( callable $beforeMatch ): GroupInterface;
```
Sets a callback that is called if the route is matched.
The developer can implement any arbitrary conditions here
If the callback returns false the route is treated as not matched

```php
public function clear(): void;
```
Removes all the pre-defined routes

```php
public function getBeforeMatch(): callable;
```
Returns the 'before match' callback if any

```php
public function getHostname(): string;
```
Returns the hostname restriction

```php
public function getPaths(): array | string;
```
Returns the common paths defined for this group

```php
public function getPrefix(): string;
```
Returns the common prefix for all the routes

```php
public function getRoutes(): RouteInterface[];
```
Returns the routes added to the group

```php
public function setHostname( string $hostname ): GroupInterface;
```
Set a hostname restriction for all the routes in the group

```php
public function setPaths( mixed $paths ): GroupInterface;
```
Set common paths for all the routes in the group

```php
public function setPrefix( string $prefix ): GroupInterface;
```
Set a common uri prefix for all the routes in this group

```php
protected function addRoute( string $pattern, mixed $paths = null, mixed $httpMethods = null ): RouteInterface;
```
Adds a route applying the common attributes

## Mvc\Router\GroupInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Router/GroupInterface.zep)

-   __Namespace__

- `Phalcon\Mvc\Router`

-   __Uses__

-   __Extends__

-   __Implements__

Phalcon\Mvc\Router\GroupInterface

```php
$router = new \Phalcon\Mvc\Router();

// Create a group with a common module and controller
$blog = new Group(
    [
        "module"     => "blog",
        "controller" => "index",
    ]
);

// All the routes start with /blog
$blog->setPrefix("/blog");

// Add a route to the group
$blog->add(
    "/save",
    [
        "action" => "save",
    ]
);

// Add another route to the group
$blog->add(
    "/edit/{id}",
    [
        "action" => "edit",
    ]
);

// This route maps to a controller different than the default
$blog->add(
    "/blog",
    [
        "controller" => "about",
        "action"     => "index",
    ]
);

// Add the group to the router
$router->mount($blog);
```

### Methods

```php
public function add( string $pattern, mixed $paths = null, mixed $httpMethods = null ): RouteInterface;
```
Adds a route to the router on any HTTP method

```php
router->add("/about", "About::index");
```

```php
public function addConnect( string $pattern, mixed $paths = null ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is CONNECT

```php
public function addDelete( string $pattern, mixed $paths = null ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is DELETE

```php
public function addGet( string $pattern, mixed $paths = null ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is GET

```php
public function addHead( string $pattern, mixed $paths = null ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is HEAD

```php
public function addOptions( string $pattern, mixed $paths = null ): RouteInterface;
```
Add a route to the router that only match if the HTTP method is OPTIONS

```php
public function addPatch( string $pattern, mixed $paths = null ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is PATCH

```php
public function addPost( string $pattern, mixed $paths = null ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is POST

```php
public function addPurge( string $pattern, mixed $paths = null ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is PURGE

```php
public function addPut( string $pattern, mixed $paths = null ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is PUT

```php
public function addTrace( string $pattern, mixed $paths = null ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is TRACE

```php
public function beforeMatch( callable $beforeMatch ): GroupInterface;
```
Sets a callback that is called if the route is matched.
The developer can implement any arbitrary conditions here
If the callback returns false the route is treated as not matched

```php
public function clear(): void;
```
Removes all the pre-defined routes

```php
public function getBeforeMatch(): callable;
```
Returns the 'before match' callback if any

```php
public function getHostname(): string;
```
Returns the hostname restriction

```php
public function getPaths(): array | string;
```
Returns the common paths defined for this group

```php
public function getPrefix(): string;
```
Returns the common prefix for all the routes

```php
public function getRoutes(): RouteInterface[];
```
Returns the routes added to the group

```php
public function setHostname( string $hostname ): GroupInterface;
```
Set a hostname restriction for all the routes in the group

```php
public function setPaths( mixed $paths ): GroupInterface;
```
Set common paths for all the routes in the group

```php
public function setPrefix( string $prefix ): GroupInterface;
```
Set a common uri prefix for all the routes in this group

## Mvc\Router\Route 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Router/Route.zep)

-   __Namespace__

- `Phalcon\Mvc\Router`

-   __Uses__

-   __Extends__

-   __Implements__

- `RouteInterface`

Phalcon\Mvc\Router\Route

This class represents every route added to the router

### Properties
```php
/**
 * @var callable|null
 */
protected $beforeMatch;

/**
 * @var string|null
 */
protected $compiledPattern;

/**
 * @var array
 */
protected $converters;

/**
 * @var GroupInterface|null
 */
protected $group;

/**
 * @var string|null
 */
protected $hostname;

/**
 * @var string
 */
protected $id = ;

/**
 * @var array|string
 */
protected $methods;

/**
 * @var callable|null
 */
protected $match;

/**
 * @var string|null
 */
protected $name;

/**
 * @var array
 */
protected $paths;

/**
 * @var string
 */
protected $pattern;

/**
 * @var int
 */
protected static $uniqueId = ;

```

### Methods

```php
public function __construct( string $pattern, mixed $paths = null, mixed $httpMethods = null );
```
Phalcon\Mvc\Router\Route constructor

```php
public function beforeMatch( callable $callback ): RouteInterface;
```
Sets a callback that is called if the route is matched.
The developer can implement any arbitrary conditions here
If the callback returns false the route is treated as not matched

```php
$router->add(
    "/login",
    [
        "module"     => "admin",
        "controller" => "session",
    ]
)->beforeMatch(
    function ($uri, $route) {
        // Check if the request was made with Ajax
        if ($_SERVER["HTTP_X_REQUESTED_WITH"] === "xmlhttprequest") {
            return false;
        }

        return true;
    }
);
```

```php
public function compilePattern( string $pattern ): string;
```
Replaces placeholders from pattern returning a valid PCRE regular expression

```php
public function convert( string $name, mixed $converter ): RouteInterface;
```
\{@inheritdoc\}

```php
public function extractNamedParams( string $pattern ): array | bool;
```
Extracts parameters from a string

```php
public function getBeforeMatch(): callable;
```
Returns the 'before match' callback if any

```php
public function getCompiledPattern(): string;
```
Returns the route's compiled pattern

```php
public function getConverters(): array;
```
Returns the router converter

```php
public function getGroup(): GroupInterface | null;
```
Returns the group associated with the route

```php
public function getHostname(): string | null;
```
Returns the hostname restriction if any

```php
public function getHttpMethods(): array | string;
```
Returns the HTTP methods that constraint matching the route

```php
public function getId(): string;
```

```php
public function getMatch(): callable;
```
Returns the 'match' callback if any

```php
public function getName(): string | null;
```
Returns the route's name

```php
public function getPaths(): array;
```
Returns the paths

```php
public function getPattern(): string;
```
Returns the route's pattern

```php
public function getReversedPaths(): array;
```
Returns the paths using positions as keys and names as values

```php
public function getRouteId(): string;
```
Returns the route's id

```php
public static function getRoutePaths( mixed $paths = null ): array;
```
Returns routePaths

```php
public function match( mixed $callback ): RouteInterface;
```
Allows to set a callback to handle the request directly in the route

```php
$router->add(
    "/help",
    []
)->match(
    function () {
        return $this->getResponse()->redirect("https://support.google.com/", true);
    }
);
```

```php
public function reConfigure( string $pattern, mixed $paths = null ): void;
```
Reconfigure the route adding a new pattern and a set of paths

```php
public static function reset(): void;
```
Resets the internal route id generator

```php
public function setGroup( GroupInterface $group ): RouteInterface;
```
Sets the group associated with the route

```php
public function setHostname( string $hostname ): RouteInterface;
```
Sets a hostname restriction to the route

```php
$route->setHostname("localhost");
```

```php
public function setHttpMethods( mixed $httpMethods ): RouteInterface;
```
Sets a set of HTTP methods that constraint the matching of the route (alias of via)

```php
$route->setHttpMethods("GET");

$route->setHttpMethods(
    [
        "GET",
        "POST",
    ]
);
```

```php
public function setName( string $name ): RouteInterface;
```
Sets the route's name

```php
$router->add(
    "/about",
    [
        "controller" => "about",
    ]
)->setName("about");
```

```php
public function via( mixed $httpMethods ): RouteInterface;
```
Set one or more HTTP methods that constraint the matching of the route

```php
$route->via("GET");

$route->via(
    [
        "GET",
        "POST",
    ]
);
```

## Mvc\Router\RouteInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Router/RouteInterface.zep)

-   __Namespace__

- `Phalcon\Mvc\Router`

-   __Uses__

-   __Extends__

-   __Implements__

Phalcon\Mvc\Router\RouteInterface

Interface for Phalcon\Mvc\Router\Route

### Methods

```php
public function compilePattern( string $pattern ): string;
```
Replaces placeholders from pattern returning a valid PCRE regular expression

```php
public function convert( string $name, mixed $converter ): RouteInterface;
```
Adds a converter to perform an additional transformation for certain parameter.

```php
public function getCompiledPattern(): string;
```
Returns the route's pattern

```php
public function getHostname(): string | null;
```
Returns the hostname restriction if any

```php
public function getHttpMethods(): string | array;
```
Returns the HTTP methods that constraint matching the route

```php
public function getName(): string | null;
```
Returns the route's name

```php
public function getPaths(): array;
```
Returns the paths

```php
public function getPattern(): string;
```
Returns the route's pattern

```php
public function getReversedPaths(): array;
```
Returns the paths using positions as keys and names as values

```php
public function getRouteId(): string;
```
Returns the route's id

```php
public function reConfigure( string $pattern, mixed $paths = null ): void;
```
Reconfigure the route adding a new pattern and a set of paths

```php
public static function reset(): void;
```
Resets the internal route id generator

```php
public function setHostname( string $hostname ): RouteInterface;
```
Sets a hostname restriction to the route

```php
public function setHttpMethods( mixed $httpMethods ): RouteInterface;
```
Sets a set of HTTP methods that constraint the matching of the route

```php
public function setName( string $name ): RouteInterface;
```
Sets the route's name

```php
public function via( mixed $httpMethods ): RouteInterface;
```
Set one or more HTTP methods that constraint the matching of the route

## Mvc\RouterInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/RouterInterface.zep)

-   __Namespace__

- `Phalcon\Mvc`

-   __Uses__

- `Phalcon\Mvc\Router\GroupInterface`
- `Phalcon\Mvc\Router\RouteInterface`

-   __Extends__

-   __Implements__

Interface for Phalcon\Mvc\Router

### Methods

```php
public function add( string $pattern, mixed $paths = null, mixed $httpMethods = null, int $position = static-constant-access ): RouteInterface;
```
Adds a route to the router on any HTTP method

```php
public function addConnect( string $pattern, mixed $paths = null, int $position = static-constant-access ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is CONNECT

```php
public function addDelete( string $pattern, mixed $paths = null, int $position = static-constant-access ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is DELETE

```php
public function addGet( string $pattern, mixed $paths = null, int $position = static-constant-access ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is GET

```php
public function addHead( string $pattern, mixed $paths = null, int $position = static-constant-access ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is HEAD

```php
public function addOptions( string $pattern, mixed $paths = null, int $position = static-constant-access ): RouteInterface;
```
Add a route to the router that only match if the HTTP method is OPTIONS

```php
public function addPatch( string $pattern, mixed $paths = null, int $position = static-constant-access ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is PATCH

```php
public function addPost( string $pattern, mixed $paths = null, int $position = static-constant-access ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is POST

```php
public function addPurge( string $pattern, mixed $paths = null, int $position = static-constant-access ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is PURGE
(Squid and Varnish support)

```php
public function addPut( string $pattern, mixed $paths = null, int $position = static-constant-access ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is PUT

```php
public function addTrace( string $pattern, mixed $paths = null, int $position = static-constant-access ): RouteInterface;
```
Adds a route to the router that only match if the HTTP method is TRACE

```php
public function attach( RouteInterface $route, int $position = static-constant-access ): RouterInterface;
```
Attach Route object to the routes stack.

```php
public function clear(): void;
```
Removes all the defined routes

```php
public function getActionName(): string;
```
Returns processed action name

```php
public function getControllerName(): string;
```
Returns processed controller name

```php
public function getMatchedRoute(): RouteInterface | null;
```
Returns the route that matches the handled URI

```php
public function getMatches(): array;
```
Return the sub expressions in the regular expression matched

```php
public function getModuleName(): string;
```
Returns processed module name

```php
public function getNamespaceName(): string;
```
Returns processed namespace name

```php
public function getParams(): array;
```
Returns processed extra params

```php
public function getRouteById( mixed $id ): RouteInterface | bool;
```
Returns a route object by its id

```php
public function getRouteByName( string $name ): RouteInterface | bool;
```
Returns a route object by its name

```php
public function getRoutes(): RouteInterface[];
```
Return all the routes defined in the router

```php
public function handle( string $uri ): void;
```
Handles routing information received from the rewrite engine

```php
public function mount( GroupInterface $group ): RouterInterface;
```
Mounts a group of routes in the router

```php
public function setDefaultAction( string $actionName ): RouterInterface;
```
Sets the default action name

```php
public function setDefaultController( string $controllerName ): RouterInterface;
```
Sets the default controller name

```php
public function setDefaultModule( string $moduleName ): RouterInterface;
```
Sets the name of the default module

```php
public function setDefaults( array $defaults ): RouterInterface;
```
Sets an array of default paths

```php
public function wasMatched(): bool;
```
Check if the router matches any of the defined routes

## Mvc\Url 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Url.zep)

-   __Namespace__

- `Phalcon\Mvc`

-   __Uses__

- `Phalcon\Di\AbstractInjectionAware`
- `Phalcon\Di\DiInterface`
- `Phalcon\Mvc\RouterInterface`
- `Phalcon\Mvc\Router\RouteInterface`
- `Phalcon\Mvc\Url\Exception`
- `Phalcon\Mvc\Url\UrlInterface`

-   __Extends__

`AbstractInjectionAware`

-   __Implements__

- `UrlInterface`

This components helps in the generation of: URIs, URLs and Paths

```php
// Generate a URL appending the URI to the base URI
echo $url->get("products/edit/1");

// Generate a URL for a predefined route
echo $url->get(
    [
        "for"   => "blog-post",
        "title" => "some-cool-stuff",
        "year"  => "2012",
    ]
);
```

### Properties
```php
/**
 * @var null | string
 */
protected $baseUri;

/**
 * @var null | string
 */
protected $basePath;

/**
 * @var RouterInterface | null
 */
protected $router;

/**
 * @var null | string
 */
protected $staticBaseUri;

```

### Methods

```php
public function __construct( RouterInterface $router = null );
```

```php
public function get( mixed $uri = null, mixed $args = null, bool $local = null, mixed $baseUri = null ): string;
```
Generates a URL

```php
// Generate a URL appending the URI to the base URI
echo $url->get("products/edit/1");

// Generate a URL for a predefined route
echo $url->get(
    [
        "for"   => "blog-post",
        "title" => "some-cool-stuff",
        "year"  => "2015",
    ]
);

// Generate a URL with GET arguments (/show/products?id=1&name=Carrots)
echo $url->get(
    "show/products",
    [
        "id"   => 1,
        "name" => "Carrots",
    ]
);

// Generate an absolute URL by setting the third parameter as false.
echo $url->get(
    "https://phalcon.io/",
    null,
    false
);
```

```php
public function getBasePath(): string;
```
Returns the base path

```php
public function getBaseUri(): string;
```
Returns the prefix for all the generated urls. By default /

```php
public function getStatic( mixed $uri = null ): string;
```
Generates a URL for a static resource

```php
// Generate a URL for a static resource
echo $url->getStatic("img/logo.png");

// Generate a URL for a static predefined route
echo $url->getStatic(
    [
        "for" => "logo-cdn",
    ]
);
```

```php
public function getStaticBaseUri(): string;
```
Returns the prefix for all the generated static urls. By default /

```php
public function path( string $path = null ): string;
```
Generates a local path

```php
public function setBasePath( string $basePath ): UrlInterface;
```
Sets a base path for all the generated paths

```php
$url->setBasePath("/var/www/htdocs/");
```

```php
public function setBaseUri( string $baseUri ): UrlInterface;
```
Sets a prefix for all the URIs to be generated

```php
$url->setBaseUri("/invo/");

$url->setBaseUri("/invo/index.php/");
```

```php
public function setStaticBaseUri( string $staticBaseUri ): UrlInterface;
```
Sets a prefix for all static URLs generated

```php
$url->setStaticBaseUri("/invo/");
```

## Mvc\Url\Exception 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Url/Exception.zep)

-   __Namespace__

- `Phalcon\Mvc\Url`

-   __Uses__

-   __Extends__

`\Exception`

-   __Implements__

Phalcon\Mvc\Url\Exception

Exceptions thrown in Phalcon\Mvc\Url will use this class

## Mvc\Url\UrlInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Url/UrlInterface.zep)

-   __Namespace__

- `Phalcon\Mvc\Url`

-   __Uses__

-   __Extends__

-   __Implements__

Interface for Phalcon\Mvc\Url\UrlInterface

### Methods

```php
public function get( mixed $uri = null, mixed $args = null, bool $local = null ): string;
```
Generates a URL

```php
public function getBasePath(): string;
```
Returns a base path

```php
public function getBaseUri(): string;
```
Returns the prefix for all the generated urls. By default /

```php
public function path( string $path = null ): string;
```
Generates a local path

```php
public function setBasePath( string $basePath ): UrlInterface;
```
Sets a base paths for all the generated paths

```php
public function setBaseUri( string $baseUri ): UrlInterface;
```
Sets a prefix to all the urls generated

## Mvc\View 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/View.zep)

-   __Namespace__

- `Phalcon\Mvc`

-   __Uses__

- `Closure`
- `Phalcon\Di\DiInterface`
- `Phalcon\Di\Injectable`
- `Phalcon\Events\EventsAwareInterface`
- `Phalcon\Events\ManagerInterface`
- `Phalcon\Mvc\View\Engine\Php`
- `Phalcon\Mvc\View\Exception`

-   __Extends__

`Injectable`

-   __Implements__

- `EventsAwareInterface`
- `ViewInterface`

Phalcon\Mvc\View

Phalcon\Mvc\View is a class for working with the "view" portion of the
model-view-controller pattern. That is, it exists to help keep the view
script separate from the model and controller scripts. It provides a system
of helpers, output filters, and variable escaping.

```php
use Phalcon\Mvc\View;

$view = new View();

// Setting views directory
$view->setViewsDir("app/views/");

$view->start();

// Shows recent posts view (app/views/posts/recent.phtml)
$view->render("posts", "recent");
$view->finish();

// Printing views output
echo $view->getContent();
```

### Constants
```php
const LEVEL_ACTION_VIEW = 1;
const LEVEL_AFTER_TEMPLATE = 4;
const LEVEL_BEFORE_TEMPLATE = 2;
const LEVEL_LAYOUT = 3;
const LEVEL_MAIN_LAYOUT = 5;
const LEVEL_NO_RENDER = 0;
```

### Properties
```php
/**
 * @var string
 */
protected $actionName;

/**
 * @var array
 */
protected $activeRenderPaths;

/**
 * @var string
 */
protected $basePath = ;

/**
 * @var string
 */
protected $content = ;

/**
 * @var string
 */
protected $controllerName;

/**
 * @var int
 */
protected $currentRenderLevel = ;

/**
 * @var bool
 */
protected $disabled = false;

/**
 * @var array
 */
protected $disabledLevels;

/**
 * @var array|bool
 */
protected $engines = false;

/**
 * @var ManagerInterface|null
 */
protected $eventsManager;

/**
 * @var string|null
 */
protected $layout;

/**
 * @var string
 */
protected $layoutsDir = ;

/**
 * @var string
 */
protected $mainView = index;

/**
 * @var array
 */
protected $options;

/**
 * @var array
 */
protected $params;

/**
 * @var array|null
 */
protected $pickView;

/**
 * @var string
 */
protected $partialsDir = ;

/**
 * @var array
 */
protected $registeredEngines;

/**
 * @var int
 */
protected $renderLevel = 5;

/**
 * @var array
 */
protected $templatesAfter;

/**
 * @var array
 */
protected $templatesBefore;

/**
 * @var array
 */
protected $viewsDirs;

/**
 * @var array
 */
protected $viewParams;

```

### Methods

```php
public function __construct( array $options = [] );
```
Phalcon\Mvc\View constructor

```php
public function __get( string $key ): mixed | null;
```
Magic method to retrieve a variable passed to the view

```php
echo $this->view->products;
```

```php
public function __isset( string $key ): bool;
```
Magic method to retrieve if a variable is set in the view

```php
echo isset($this->view->products);
```

```php
public function __set( string $key, mixed $value );
```
Magic method to pass variables to the views

```php
$this->view->products = $products;
```

```php
public function cleanTemplateAfter(): View;
```
Resets any template before layouts

```php
public function cleanTemplateBefore(): View;
```
Resets any "template before" layouts

```php
public function disable(): View;
```
Disables the auto-rendering process

```php
public function disableLevel( mixed $level ): ViewInterface;
```
Disables a specific level of rendering

```php
// Render all levels except ACTION level
$this->view->disableLevel(
    View::LEVEL_ACTION_VIEW
);
```

```php
public function enable(): View;
```
Enables the auto-rendering process

```php
public function exists( string $view ): bool;
```
Checks whether view exists
@deprecated

```php
public function finish(): View;
```
Finishes the render process by stopping the output buffering

```php
public function getActionName(): string;
```
Gets the name of the action rendered

```php
public function getActiveRenderPath(): string | array;
```
Returns the path (or paths) of the views that are currently rendered

```php
public function getBasePath(): string;
```
Gets base path

```php
public function getContent(): string;
```
Returns output from another view stage

```php
public function getControllerName(): string;
```
Gets the name of the controller rendered

```php
public function getCurrentRenderLevel(): int;
```

```php
public function getEventsManager(): ManagerInterface | null;
```
Returns the internal event manager

```php
public function getLayout(): string;
```
Returns the name of the main view

```php
public function getLayoutsDir(): string;
```
Gets the current layouts sub-directory

```php
public function getMainView(): string;
```
Returns the name of the main view

```php
public function getParamsToView(): array;
```
Returns parameters to views

```php
public function getPartial( string $partialPath, mixed $params = null ): string;
```
Renders a partial view

```php
// Retrieve the contents of a partial
echo $this->getPartial("shared/footer");
```

```php
// Retrieve the contents of a partial with arguments
echo $this->getPartial(
    "shared/footer",
    [
        "content" => $html,
    ]
);
```

```php
public function getPartialsDir(): string;
```
Gets the current partials sub-directory

```php
public function getRegisteredEngines(): array;
```

```php
public function getRender( string $controllerName, string $actionName, array $params = [], mixed $configCallback = null ): string;
```
Perform the automatic rendering returning the output as a string

```php
$template = $this->view->getRender(
    "products",
    "show",
    [
        "products" => $products,
    ]
);
```

```php
public function getRenderLevel(): int;
```

```php
public function getVar( string $key ): mixed | null;
```
Returns a parameter previously set in the view

```php
public function getViewsDir(): string | array;
```
Gets views directory

```php
public function has( string $view ): bool;
```
Checks whether view exists

```php
public function isDisabled(): bool;
```
Whether automatic rendering is enabled

```php
public function partial( string $partialPath, mixed $params = null );
```
Renders a partial view

```php
// Show a partial inside another view
$this->partial("shared/footer");
```

```php
// Show a partial inside another view with parameters
$this->partial(
    "shared/footer",
    [
        "content" => $html,
    ]
);
```

```php
public function pick( mixed $renderView ): View;
```
Choose a different view to render instead of last-controller/last-action

```php
use Phalcon\Mvc\Controller;

class ProductsController extends Controller
{
    public function saveAction()
    {
        // Do some save stuff...

        // Then show the list view
        $this->view->pick("products/list");
    }
}
```

```php
public function processRender( string $controllerName, string $actionName, array $params = [], bool $fireEvents = bool ): bool;
```
Processes the view and templates; Fires events if needed

```php
public function registerEngines( array $engines ): View;
```
Register templating engines

```php
$this->view->registerEngines(
    [
        ".phtml" => \Phalcon\Mvc\View\Engine\Php::class,
        ".volt"  => \Phalcon\Mvc\View\Engine\Volt::class,
        ".mhtml" => \MyCustomEngine::class,
    ]
);
```

```php
public function render( string $controllerName, string $actionName, array $params = [] ): View | bool;
```
Executes render process from dispatching data

```php
// Shows recent posts view (app/views/posts/recent.phtml)
$view->start()->render("posts", "recent")->finish();
```

```php
public function reset(): View;
```
Resets the view component to its factory default values

```php
public function setBasePath( string $basePath ): View;
```
Sets base path. Depending of your platform, always add a trailing slash
or backslash

```php
$view->setBasePath(__DIR__ . "/");
```

```php
public function setContent( string $content ): View;
```
Externally sets the view content

```php
$this->view->setContent("<h1>hello</h1>");
```

```php
public function setEventsManager( ManagerInterface $eventsManager ): void;
```
Sets the events manager

```php
public function setLayout( string $layout ): View;
```
Change the layout to be used instead of using the name of the latest
controller name

```php
$this->view->setLayout("main");
```

```php
public function setLayoutsDir( string $layoutsDir ): View;
```
Sets the layouts sub-directory. Must be a directory under the views
directory. Depending of your platform, always add a trailing slash or
backslash

```php
$view->setLayoutsDir("../common/layouts/");
```

```php
public function setMainView( string $viewPath ): View;
```
Sets default view name. Must be a file without extension in the views
directory

```php
// Renders as main view views-dir/base.phtml
$this->view->setMainView("base");
```

```php
public function setParamToView( string $key, mixed $value ): View;
```
Adds parameters to views (alias of setVar)

```php
$this->view->setParamToView("products", $products);
```

```php
public function setPartialsDir( string $partialsDir ): View;
```
Sets a partials sub-directory. Must be a directory under the views
directory. Depending of your platform, always add a trailing slash or
backslash

```php
$view->setPartialsDir("../common/partials/");
```

```php
public function setRenderLevel( int $level ): ViewInterface;
```
Sets the render level for the view

```php
// Render the view related to the controller only
$this->view->setRenderLevel(
    View::LEVEL_LAYOUT
);
```

```php
public function setTemplateAfter( mixed $templateAfter ): View;
```
Sets a "template after" controller layout

```php
public function setTemplateBefore( mixed $templateBefore ): View;
```
Sets a template before the controller layout

```php
public function setVar( string $key, mixed $value ): View;
```
Set a single view parameter

```php
$this->view->setVar("products", $products);
```

```php
public function setVars( array $params, bool $merge = bool ): View;
```
Set all the render params

```php
$this->view->setVars(
    [
        "products" => $products,
    ]
);
```

```php
public function setViewsDir( mixed $viewsDir ): View;
```
Sets the views directory. Depending of your platform,
always add a trailing slash or backslash

```php
public function start(): View;
```
Starts rendering process enabling the output buffering

```php
public function toString( string $controllerName, string $actionName, array $params = [] ): string;
```
Renders the view and returns it as a string

```php
protected function engineRender( array $engines, string $viewPath, bool $silence, bool $mustClean = bool );
```
Checks whether view exists on registered extensions and render it

```php
protected function getViewsDirs(): array;
```
Gets views directories

```php
final protected function isAbsolutePath( string $path );
```
Checks if a path is absolute or not

```php
protected function loadTemplateEngines(): array;
```
Loads registered template engines, if none is registered it will use
Phalcon\Mvc\View\Engine\Php

## Mvc\View\Engine\AbstractEngine ![Abstract](/assets/images/abstract-green.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/View/Engine/AbstractEngine.zep)

-   __Namespace__

- `Phalcon\Mvc\View\Engine`

-   __Uses__

- `Phalcon\Di\DiInterface`
- `Phalcon\Di\Injectable`
- `Phalcon\Mvc\ViewBaseInterface`

-   __Extends__

`Injectable`

-   __Implements__

- `EngineInterface`

All the template engine adapters must inherit this class. This provides
basic interfacing between the engine and the Phalcon\Mvc\View component.

### Properties
```php
/**
 * @var ViewBaseInterface
 */
protected $view;

```

### Methods

```php
public function __construct( ViewBaseInterface $view, DiInterface $container = null );
```
Phalcon\Mvc\View\Engine constructor

```php
public function getContent(): string;
```
Returns cached output on another view stage

```php
public function getView(): ViewBaseInterface;
```
Returns the view component related to the adapter

```php
public function partial( string $partialPath, mixed $params = null ): void;
```
Renders a partial inside another view

## Mvc\View\Engine\EngineInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/View/Engine/EngineInterface.zep)

-   __Namespace__

- `Phalcon\Mvc\View\Engine`

-   __Uses__

-   __Extends__

-   __Implements__

Interface for Phalcon\Mvc\View engine adapters

### Methods

```php
public function getContent(): string;
```
Returns cached output on another view stage

```php
public function partial( string $partialPath, mixed $params = null ): void;
```
Renders a partial inside another view

```php
public function render( string $path, mixed $params, bool $mustClean = bool );
```
Renders a view using the template engine

TODO: Change params to array type

## Mvc\View\Engine\Php 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/View/Engine/Php.zep)

-   __Namespace__

- `Phalcon\Mvc\View\Engine`

-   __Uses__

-   __Extends__

`AbstractEngine`

-   __Implements__

Adapter to use PHP itself as templating engine

### Methods

```php
public function render( string $path, mixed $params, bool $mustClean = bool );
```
Renders a view using the template engine

## Mvc\View\Engine\Volt 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/View/Engine/Volt.zep)

-   __Namespace__

- `Phalcon\Mvc\View\Engine`

-   __Uses__

- `Phalcon\Di\DiInterface`
- `Phalcon\Events\EventsAwareInterface`
- `Phalcon\Events\ManagerInterface`
- `Phalcon\Html\Link\Link`
- `Phalcon\Html\Link\Serializer\Header`
- `Phalcon\Mvc\View\Engine\Volt\Compiler`
- `Phalcon\Mvc\View\Exception`

-   __Extends__

`AbstractEngine`

-   __Implements__

- `EventsAwareInterface`

Designer friendly and fast template engine for PHP written in Zephir/C

### Properties
```php
/**
 * @var Compiler
 */
protected $compiler;

/**
 * @var ManagerInterface|null
 */
protected $eventsManager;

/**
 * @var array
 */
protected $macros;

/**
 * @var array
 */
protected $options;

```

### Methods

```php
public function callMacro( string $name, array $arguments = [] ): mixed;
```
Checks if a macro is defined and calls it

```php
public function convertEncoding( string $text, string $from, string $to ): string;
```
Performs a string conversion

```php
public function getCompiler(): Compiler;
```
Returns the Volt's compiler

```php
public function getEventsManager(): ManagerInterface | null;
```
Returns the internal event manager

```php
public function getOptions(): array;
```
Return Volt's options

```php
public function isIncluded( mixed $needle, mixed $haystack ): bool;
```
Checks if the needle is included in the haystack

```php
public function length( mixed $item ): int;
```
Length filter. If an array/object is passed a count is performed otherwise a strlen/mb_strlen

```php
public function preload( mixed $parameters ): string;
```
Parses the preload element passed and sets the necessary link headers
@todo find a better way to handle this

```php
public function render( string $path, mixed $params, bool $mustClean = bool );
```
Renders a view using the template engine

```php
public function setEventsManager( ManagerInterface $eventsManager ): void;
```
Sets the events manager

```php
public function setOptions( array $options );
```
Set Volt's options

```php
public function slice( mixed $value, int $start = int, mixed $end = null );
```
Extracts a slice from a string/array/traversable object value

```php
public function sort( array $value ): array;
```
Sorts an array

## Mvc\View\Engine\Volt\Compiler 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/View/Engine/Volt/Compiler.zep)

-   __Namespace__

- `Phalcon\Mvc\View\Engine\Volt`

-   __Uses__

- `Closure`
- `Phalcon\Di\DiInterface`
- `Phalcon\Di\InjectionAwareInterface`
- `Phalcon\Mvc\ViewBaseInterface`

-   __Extends__

-   __Implements__

- `InjectionAwareInterface`

This class reads and compiles Volt templates into PHP plain code

```php
$compiler = new \Phalcon\Mvc\View\Engine\Volt\Compiler();

$compiler->compile("views/partials/header.volt");

require $compiler->getCompiledTemplatePath();
```

### Properties
```php
/**
 * @var bool
 */
protected $autoescape = false;

/**
 * @var int
 */
protected $blockLevel = ;

/**
 * @var array|null
 *
 * TODO: Make array only?
 */
protected $blocks;

/**
 * @var DiInterface|null
 */
protected $container;

/**
 * @var string|null
 */
protected $compiledTemplatePath;

/**
 * @var string|null
 */
protected $currentBlock;

/**
 * @var string|null
 */
protected $currentPath;

/**
 * @var int
 */
protected $exprLevel = ;

/**
 * @var bool
 */
protected $extended = false;

/**
 * @var array
 */
protected $extensions;

/**
 * @var array|bool
 *
 * TODO: Make it always array
 */
protected $extendedBlocks;

/**
 * @var array
 */
protected $filters;

/**
 * @var int
 */
protected $foreachLevel = ;

/**
 * @var array
 */
protected $forElsePointers;

/**
 * @var array
 */
protected $functions;

/**
 * @var int
 */
protected $level = ;

/**
 * @var array
 */
protected $loopPointers;

/**
 * @var array
 */
protected $macros;

/**
 * @var array
 */
protected $options;

/**
 * @var string
 */
protected $prefix = ;

/**
 * @var ViewBaseInterface|null
 */
protected $view;

```

### Methods

```php
public function __construct( ViewBaseInterface $view = null );
```
Phalcon\Mvc\View\Engine\Volt\Compiler

```php
public function addExtension( mixed $extension ): Compiler;
```
Registers a Volt's extension

@var mixed extension

```php
public function addFilter( string $name, mixed $definition ): Compiler;
```
Register a new filter in the compiler

```php
public function addFunction( string $name, mixed $definition ): Compiler;
```
Register a new function in the compiler

```php
public function attributeReader( array $expr ): string;
```
Resolves attribute reading

```php
public function compile( string $templatePath, bool $extendsMode = bool );
```
Compiles a template into a file applying the compiler options
This method does not return the compiled path if the template was not compiled

```php
$compiler->compile("views/layouts/main.volt");

require $compiler->getCompiledTemplatePath();
```

```php
public function compileAutoEscape( array $statement, bool $extendsMode ): string;
```
Compiles a "autoescape" statement returning PHP code

```php
public function compileCall( array $statement, bool $extendsMode );
```
Compiles calls to macros

```php
public function compileCase( array $statement, bool $caseClause = bool ): string;
```
Compiles a "case"/"default" clause returning PHP code

```php
public function compileDo( array $statement ): string;
```
Compiles a "do" statement returning PHP code

```php
public function compileEcho( array $statement ): string;
```
Compiles a \{% raw %\}`{{` `}}`\{% endraw %\} statement returning PHP code

```php
public function compileElseIf( array $statement ): string;
```
Compiles a "elseif" statement returning PHP code

```php
public function compileFile( string $path, string $compiledPath, bool $extendsMode = bool );
```
Compiles a template into a file forcing the destination path

```php
$compiler->compileFile(
    "views/layouts/main.volt",
    "views/layouts/main.volt.php"
);
```

```php
public function compileForElse(): string;
```
Generates a 'forelse' PHP code

```php
public function compileForeach( array $statement, bool $extendsMode = bool ): string;
```
Compiles a "foreach" intermediate code representation into plain PHP code

```php
public function compileIf( array $statement, bool $extendsMode = bool ): string;
```
Compiles a 'if' statement returning PHP code

```php
public function compileInclude( array $statement ): string;
```
Compiles a 'include' statement returning PHP code

```php
public function compileMacro( array $statement, bool $extendsMode ): string;
```
Compiles macros

```php
public function compileReturn( array $statement ): string;
```
Compiles a "return" statement returning PHP code

@throws \Phalcon\Mvc\View\Engine\Volt\Exception

```php
public function compileSet( array $statement ): string;
```
Compiles a "set" statement returning PHP code. The method accepts an
array produced by the Volt parser and creates the `set` statement in PHP.
This method is not particularly useful in development, since it requires
advanced knowledge of the Volt parser.

```php
<?php

use Phalcon\Mvc\View\Engine\Volt\Compiler;

$compiler = new Compiler();

// {% set a = ['first': 1] %}

$source = [
    "type" => 306,
    "assignments" => [
        [
            "variable" => [
                "type" => 265,
                "value" => "a",
                "file" => "eval code",
                "line" => 1
            ],
            "op" => 61,
            "expr" => [
                "type" => 360,
                "left" => [
                    [
                        "expr" => [
                            "type" => 258,
                            "value" => "1",
                            "file" => "eval code",
                            "line" => 1
                        ],
                        "name" => "first",
                        "file" => "eval code",
                        "line" => 1
                    ]
                ],
                "file" => "eval code",
                "line" => 1
            ],
            "file" => "eval code",
            "line" => 1
        ]
    ]
];

echo $compiler->compileSet($source);
// <?php $a = ['first' => 1]; ?>
```

```php
public function compileString( string $viewCode, bool $extendsMode = bool ): string;
```
Compiles a template into a string

```php
echo $compiler->compileString({% raw %}'{{ "hello world" }}'{% endraw %});
```

```php
public function compileSwitch( array $statement, bool $extendsMode = bool ): string;
```
Compiles a 'switch' statement returning PHP code

```php
final public function expression( array $expr, bool $doubleQuotes = bool ): string;
```
Resolves an expression node in an AST volt tree

```php
final public function fireExtensionEvent( string $name, array $arguments = [] );
```
Fires an event to registered extensions

```php
public function functionCall( array $expr, bool $doubleQuotes = bool ): string;
```
Resolves function intermediate code into PHP function calls

```php
public function getCompiledTemplatePath(): string;
```
Returns the path to the last compiled template

```php
public function getDI(): DiInterface;
```
Returns the internal dependency injector

```php
public function getExtensions(): array;
```
Returns the list of extensions registered in Volt

```php
public function getFilters(): array;
```
Register the user registered filters

```php
public function getFunctions(): array;
```
Register the user registered functions

```php
public function getOption( string $option ): string | null;
```
Returns a compiler's option

```php
public function getOptions(): array;
```
Returns the compiler options

```php
public function getTemplatePath(): string;
```
Returns the path that is currently being compiled

```php
public function getUniquePrefix(): string;
```
Return a unique prefix to be used as prefix for compiled variables and
contexts

```php
public function parse( string $viewCode ): array;
```
Parses a Volt template returning its intermediate representation

```php
print_r(
    $compiler->parse("{% raw %}{{ 3 + 2 }}{% endraw %}")
);
```

```php
public function resolveTest( array $test, string $left ): string;
```
Resolves filter intermediate code into a valid PHP expression

```php
public function setDI( DiInterface $container ): void;
```
Sets the dependency injector

```php
public function setOption( string $option, mixed $value );
```
Sets a single compiler option

```php
public function setOptions( array $options );
```
Sets the compiler options

```php
public function setUniquePrefix( string $prefix ): Compiler;
```
Set a unique prefix to be used as prefix for compiled variables

```php
protected function compileSource( string $viewCode, bool $extendsMode = bool ): string;
```
Compiles a Volt source code returning a PHP plain version

```php
protected function getFinalPath( string $path );
```
Gets the final path with VIEW

```php
final protected function resolveFilter( array $filter, string $left ): string;
```
Resolves filter intermediate code into PHP function calls

```php
final protected function statementList( array $statements, bool $extendsMode = bool ): string;
```
Traverses a statement list compiling each of its nodes

```php
final protected function statementListOrExtends( mixed $statements );
```
Compiles a block of statements

## Mvc\View\Engine\Volt\Exception 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/View/Engine/Volt/Exception.zep)

-   __Namespace__

- `Phalcon\Mvc\View\Engine\Volt`

-   __Uses__

- `Phalcon\Mvc\View\Exception`

-   __Extends__

`BaseException`

-   __Implements__

Class for exceptions thrown by Phalcon\Mvc\View

### Properties
```php
/**
 * @var array
 */
protected $statement;

```

### Methods

```php
public function __construct( string $message = string, array $statement = [], int $code = int, \Exception $previous = null );
```

```php
public function getStatement(): array;
```
Gets currently parsed statement (if any).

## Mvc\View\Exception 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/View/Exception.zep)

-   __Namespace__

- `Phalcon\Mvc\View`

-   __Uses__

-   __Extends__

`\Exception`

-   __Implements__

Phalcon\Mvc\View\Exception

Class for exceptions thrown by Phalcon\Mvc\View

## Mvc\View\Simple 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/View/Simple.zep)

-   __Namespace__

- `Phalcon\Mvc\View`

-   __Uses__

- `Closure`
- `Phalcon\Di\DiInterface`
- `Phalcon\Di\Injectable`
- `Phalcon\Events\EventsAwareInterface`
- `Phalcon\Events\ManagerInterface`
- `Phalcon\Mvc\ViewBaseInterface`
- `Phalcon\Mvc\View\Engine\EngineInterface`
- `Phalcon\Mvc\View\Engine\Php`

-   __Extends__

`Injectable`

-   __Implements__

- `EventsAwareInterface`
- `ViewBaseInterface`

Phalcon\Mvc\View\Simple

This component allows to render views without hierarchical levels

```php
use Phalcon\Mvc\View\Simple as View;

$view = new View();

// Render a view
echo $view->render(
    "templates/my-view",
    [
        "some" => $param,
    ]
);

// Or with filename with extension
echo $view->render(
    "templates/my-view.volt",
    [
        "parameter" => $here,
    ]
);
```

### Properties
```php
/**
 * @var string
 */
protected $activeRenderPath;

/**
 * @var string
 */
protected $content;

/**
 * @var EngineInterface[]|false
 */
protected $engines = false;

/**
 * @var ManagerInterface|null
 */
protected $eventsManager;

/**
 * @var array
 */
protected $options;

/**
 * @var array
 */
protected $registeredEngines;

/**
 * @var string
 */
protected $viewsDir;

/**
 * @var array
 */
protected $viewParams;

```

### Methods

```php
public function __construct( array $options = [] );
```
Phalcon\Mvc\View\Simple constructor

```php
public function __get( string $key ): mixed | null;
```
Magic method to retrieve a variable passed to the view

```php
echo $this->view->products;
```

```php
public function __set( string $key, mixed $value ): void;
```
Magic method to pass variables to the views

```php
$this->view->products = $products;
```

```php
public function getActiveRenderPath(): string;
```
Returns the path of the view that is currently rendered

```php
public function getContent(): string;
```
Returns output from another view stage

```php
public function getEventsManager(): ManagerInterface | null;
```
Returns the internal event manager

```php
public function getParamsToView(): array;
```
Returns parameters to views

```php
public function getRegisteredEngines(): array;
```

```php
public function getVar( string $key ): mixed | null;
```
Returns a parameter previously set in the view

```php
public function getViewsDir(): string;
```
Gets views directory

```php
public function partial( string $partialPath, mixed $params = null ): void;
```
Renders a partial view

```php
// Show a partial inside another view
$this->partial("shared/footer");
```

```php
// Show a partial inside another view with parameters
$this->partial(
    "shared/footer",
    [
        "content" => $html,
    ]
);
```

```php
public function registerEngines( array $engines ): void;
```
Register templating engines

```php
$this->view->registerEngines(
    [
        ".phtml" => \Phalcon\Mvc\View\Engine\Php::class,
        ".volt"  => \Phalcon\Mvc\View\Engine\Volt::class,
        ".mhtml" => \MyCustomEngine::class,
    ]
);
```

```php
public function render( string $path, array $params = [] ): string;
```
Renders a view

```php
public function setContent( string $content ): Simple;
```
Externally sets the view content

```php
$this->view->setContent("<h1>hello</h1>");
```

```php
public function setEventsManager( ManagerInterface $eventsManager ): void;
```
Sets the events manager

```php
public function setParamToView( string $key, mixed $value ): Simple;
```
Adds parameters to views (alias of setVar)

```php
$this->view->setParamToView("products", $products);
```

```php
public function setVar( string $key, mixed $value ): Simple;
```
Set a single view parameter

```php
$this->view->setVar("products", $products);
```

```php
public function setVars( array $params, bool $merge = bool ): Simple;
```
Set all the render params

```php
$this->view->setVars(
    [
        "products" => $products,
    ]
);
```

```php
public function setViewsDir( string $viewsDir ): void;
```
Sets views directory

```php
final protected function internalRender( string $path, mixed $params ): void;
```
Tries to render the view with every engine registered in the component

```php
protected function loadTemplateEngines(): array;
```
Loads registered template engines, if none are registered it will use
Phalcon\Mvc\View\Engine\Php

## Mvc\ViewBaseInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/ViewBaseInterface.zep)

-   __Namespace__

- `Phalcon\Mvc`

-   __Uses__

- `Phalcon\Cache\Adapter\AdapterInterface`

-   __Extends__

-   __Implements__

Phalcon\Mvc\ViewInterface

Interface for Phalcon\Mvc\View and Phalcon\Mvc\View\Simple

### Methods

```php
public function getContent(): string;
```
Returns cached output from another view stage

```php
public function getParamsToView(): array;
```
Returns parameters to views

```php
public function getViewsDir(): string | array;
```
Gets views directory

```php
public function partial( string $partialPath, mixed $params = null );
```
Renders a partial view

```php
public function setContent( string $content );
```
Externally sets the view content

```php
public function setParamToView( string $key, mixed $value );
```
Adds parameters to views (alias of setVar)

```php
public function setVar( string $key, mixed $value );
```
Adds parameters to views

```php
public function setViewsDir( string $viewsDir );
```
Sets views directory. Depending of your platform, always add a trailing
slash or backslash

## Mvc\ViewInterface ![Interface](/assets/images/interface-blue.svg) 

[Source on GitHub](https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/ViewInterface.zep)

-   __Namespace__

- `Phalcon\Mvc`

-   __Uses__

-   __Extends__

`ViewBaseInterface`

-   __Implements__

Phalcon\Mvc\ViewInterface

Interface for Phalcon\Mvc\View

### Methods

```php
public function cleanTemplateAfter();
```
Resets any template before layouts

```php
public function cleanTemplateBefore();
```
Resets any template before layouts

```php
public function disable();
```
Disables the auto-rendering process

```php
public function enable();
```
Enables the auto-rendering process

```php
public function finish();
```
Finishes the render process by stopping the output buffering

```php
public function getActionName(): string;
```
Gets the name of the action rendered

```php
public function getActiveRenderPath(): string | array;
```
Returns the path of the view that is currently rendered

```php
public function getBasePath(): string;
```
Gets base path

```php
public function getControllerName(): string;
```
Gets the name of the controller rendered

```php
public function getLayout(): string;
```
Returns the name of the main view

```php
public function getLayoutsDir(): string;
```
Gets the current layouts sub-directory

```php
public function getMainView(): string;
```
Returns the name of the main view

```php
public function getPartialsDir(): string;
```
Gets the current partials sub-directory

```php
public function isDisabled(): bool;
```
Whether the automatic rendering is disabled

```php
public function pick( string $renderView );
```
Choose a view different to render than last-controller/last-action

```php
public function registerEngines( array $engines );
```
Register templating engines

```php
public function render( string $controllerName, string $actionName, array $params = [] ): ViewInterface | bool;
```
Executes render process from dispatching data

```php
public function reset();
```
Resets the view component to its factory default values

```php
public function setBasePath( string $basePath );
```
Sets base path. Depending of your platform, always add a trailing slash
or backslash

```php
public function setLayout( string $layout );
```
Change the layout to be used instead of using the name of the latest
controller name

```php
public function setLayoutsDir( string $layoutsDir );
```
Sets the layouts sub-directory. Must be a directory under the views
directory. Depending of your platform, always add a trailing slash or
backslash

```php
public function setMainView( string $viewPath );
```
Sets default view name. Must be a file without extension in the views
directory

```php
public function setPartialsDir( string $partialsDir );
```
Sets a partials sub-directory. Must be a directory under the views
directory. Depending of your platform, always add a trailing slash or
backslash

```php
public function setRenderLevel( int $level ): ViewInterface;
```
Sets the render level for the view

```php
public function setTemplateAfter( mixed $templateAfter );
```
Appends template after controller layout

```php
public function setTemplateBefore( mixed $templateBefore );
```
Appends template before controller layout

```php
public function start();
```
Starts rendering process enabling the output buffering

Source: https://docs.phalcon.io/5.6/api/phalcon_mvc/index.mdx
