How to create a Yii2 model without a database

php, yii2

Solution

This is a `Model` from one of my projects. This `Model` is not connected with any database.

<?php
/**
 * Created by PhpStorm.
 * User: Abhimanyu
 * Date: 18-02-2015
 * Time: 22:07
 */

namespace backend\models;

use yii\base\Model;

class BasicSettingForm extends Model
{
    public $appName;
    public $appBackendTheme;
    public $appFrontendTheme;
    public $cacheClass;
    public $appTour;

    public function rules()
    {
        return [
            // Application Name
            ['appName', 'required'],
            ['appName', 'string', 'max' => 150],

            // Application Backend Theme
            ['appBackendTheme', 'required'],

            // Application Frontend Theme
            ['appFrontendTheme', 'required'],

            // Cache Class
            ['cacheClass', 'required'],
            ['cacheClass', 'string', 'max' => 128],

            // Application Tour
            ['appTour', 'boolean']
        ];
    }

    public function attributeLabels()
    {
        return [
            'appName'          => 'Application Name',
            'appFrontendTheme' => 'Frontend Theme',
            'appBackendTheme'  => 'Backend Theme',
            'cacheClass' => 'Cache Class',
            'appTour'    => 'Show introduction tour for new users'
        ];
    }
}

Use this `Model` like any other. e.g. `view.php`:

<?php
/**
 * Created by PhpStorm.
 * User: Abhimanyu
 * Date: 18-02-2015
 * Time: 16:47
 */

use abhimanyu\installer\helpers\enums\Configuration as Enum;
use yii\caching\DbCache;
use yii\caching\FileCache;
use yii\helpers\Html;
use yii\widgets\ActiveForm;

/** @var $this \yii\web\View */
/** @var $model \backend\models\BasicSettingForm */
/** @var $themes */

$this->title = 'Basic Settings - ' . Yii::$app->name;
?>

<div class="panel panel-default">
    <div class="panel-heading">Basic Settings</div>

    <div class="panel-body">

        <?= $this->render('/alert') ?>

        <?php $form = ActiveForm::begin([
                                        'id'                   => 'basic-setting-form',
                                        'enableAjaxValidation' => FALSE,
                                    ]); ?>

        <h4>Application Settings</h4>

        <div class="form-group">
            <?= $form->field($model, 'appName')->textInput([
                                                           'value'        => Yii::$app->config->get(
                                                               Enum::APP_NAME, 'Starter Kit'),
                                                           'autofocus'    => TRUE,
                                                           'autocomplete' => 'off'
                                                       ])
            ?>
        </div>

        <hr/>

        <h4>Theme Settings</h4>

        <div class="form-group">
            <?= $form->field($model, 'appBackendTheme')->dropDownList($themes, [
            'class'   => 'form-control',
            'options' => [
                Yii::$app->config->get(Enum::APP_BACKEND_THEME, 'yeti') => ['selected ' => TRUE]
            ]
        ]) ?>
    </div>

    <div class="form-group">
        <?= $form->field($model, 'appFrontendTheme')->dropDownList($themes, [
            'class'   => 'form-control',
            'options' => [
                Yii::$app->config->get(Enum::APP_FRONTEND_THEME, 'readable') => ['selected ' => TRUE]
            ]
        ]) ?>
    </div>

    <hr/>

    <h4>Cache Setting</h4>

    <div class="form-group">
        <?= $form->field($model, 'cacheClass')->dropDownList(
            [
                FileCache::className() => 'File Cache',
                DbCache::className()   => 'Db Cache'
            ],
            [
                'class'   => 'form-control',
                'options' => [
                    Yii::$app->config->get(Enum::CACHE_CLASS, FileCache::className()) => ['selected ' => TRUE]
                ]
            ]) ?>
    </div>

    <hr/>

    <h4>Introduction Tour</h4>

    <div class="form-group">
        <div class="checkbox">
            <?= $form->field($model, 'appTour')->checkbox() ?>
        </div>
    </div>

    <?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?>

    <?php $form::end(); ?>
</div>

Problem

I would like to create a yii2 model without a database. Instead, the data is generated dynamically and not stored, just displayed to the user as a json. Basically, I would just like a get a simple, basic example of a non-database Model working but I can't find any documentation on it. So how would I write a model without a database? I have extended `\yii\base\Model` but I get the following error message: ``` <?xml version="1.0" encoding="UTF-8"?> <response> <name>PHP Fatal Error</name> <message>Call to undefined method my\app\models\Test::find()</message> <code>1</code> <type>yii\base\ErrorException</type> <file>/my/app/vendor/yiisoft/yii2/rest/IndexAction.php</file> <line>61</line> <stack-trace> <item>#0 [internal function]: yii\base\ErrorHandler->handleFatalError()</item> <item>#1 {main}</item> </stack-trace> </response> ``` To implement `find()`, I must return a database query object. My Model is completely blank, I'm just looking for a simple example to understand the principal. ``` <?php namespace my\app\models; class Test extends \yii\base\Model{ } ```

Original source