# Models Command

This `ModelsCommand` class is a **CLI command** used to **generate new Model files** dynamically in the PHPFast framework.

```php
public function create($modelName) {
        // Define the path for the new model
        $modelName = ucfirst($modelName);
        $modelPath = ROOT_PATH . '/application/Models/' . ucfirst($modelName) . 'Model.php';
        
        // Check if the model already exists
        if (file_exists($modelPath)) {
            echo "Model {$modelName} already exists.\n";
            return;
        }

        // Define the contents of the model
        $modelContent = // Model File Structure

        // Create the model file
        file_put_contents($modelPath, $modelContent);

        echo "Model {$modelName}Model has been created successfully.\n";
    }
```

If you want to create a new Model and named as 'User', run the following command:

```
php init models user
```

If `UserModel.php` is exist:

```php
Model User already exists.
```

If `UserModel.php` is not exist, it will be generated:

```php
<?php
namespace App\Models;
use System\Core\BaseModel;

class UserModel extends BaseModel {

    protected $table = 'User';

    // Columns that are fillable (can be added or modified)
    protected $fillable = ['name'];

    // Columns that are guarded (cannot be modified)
    protected $guarded = ['id', 'created_at'];

    /**
     * Define the table schema
     * 
     * @return array Table schema
     */
    public function _schema() {
        return [
            'id' => [
                'type' => 'int unsigned',
                'auto_increment' => true,
                'key' => 'primary',
                'null' => false
            ],
            'name' => [
                'type' => 'varchar(150)',
                'null' => false,
                'default' => ''
            ]
        ];
    }

    /**
     * Get all records
     */
    public function getUsers($where = '', $params = [], $orderBy = 'id DESC', $limit = null, $offset = null) {
        return $this->list($this->table, $where, $params, $orderBy, $limit, $offset);
    }

    /**
     * Add a new record
     */
    public function addUser($data) {
        $data = $this->fill($data);
        return $this->add($this->table, $data);
    }

    /**
     * Update an existing record
     */
    public function setUser($id, $data) {
        $data = $this->fill($data);
        return $this->set($this->table, $data, 'id = ?', [$id]);
    }

    /**
     * Delete a record
     */
    public function delUser($id) {
        return $this->del($this->table, 'id = ?', [$id]);
    }
```

```
Model UserModel created at /root/application/Models/UserModel.php.
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cmsfullform.com/documents/commands/models-command.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
