IceHrm
Search…
Adding Entries to DB
Manipulating the database

Introduction

Let's check how we can add data into the newly created table

Add a Model Class

In order to communicate with the database you need a model. IceHrm uses AdoDB as the ORM. But it provides an abstraction layer on top of AdoDB.
1
icehrm
2
|--extnsions
3
|--tasks
4
|--src
5
|--Tasks
6
|--Model
7
|--Task.php
8
|--Extension.php
9
|--Migration.php
10
|--meta.json
Copied!
Task.php
1
<?php
2
3
namespace Tasks\Model;
4
5
use Classes\ModuleAccess;
6
use Model\BaseModel;
7
8
class Task extends BaseModel
9
{
10
public $table = 'Tasks';
11
}
Copied!

Update Extension Include File

tasks.php

1
<?php
2
require_once __DIR__.'/src/Tasks/Extension.php';
3
require_once __DIR__.'/src/Tasks/Migration.php';
4
require_once __DIR__.'/src/Tasks/Model/Task.php';
Copied!

Create a Random Task

Just for testing we will create a new task on the view file itself, and print it on screen.

web/index.php

1
<?php
2
$user = \Classes\BaseService::getInstance()->getCurrentUser();
3
echo "Welcome ".$user->username."<br/>";
4
5
echo "Creating a task <br/>";
6
7
$task = new \Tasks\Model\Task();
8
$taskName = 'Task-'.rand(rand(0, 100), 50000);
9
10
$task->name = $taskName;
11
$task->employee = $user->employee;
12
$task->description = $taskName.' description';
13
$task->created = date('Y-m-d H:i:s');
14
$task->updated = date('Y-m-d H:i:s');
15
16
/**
17
* Saving the task, $ok will be false if there were any error during the creation
18
*/
19
$ok = $task->Save();
20
21
if (!$ok) {
22
echo "Error: ".$task->ErrorMsg()." <br/>";
23
}
24
25
echo "Find last task <br/>";
26
27
$taskFromDB = new \Tasks\Model\Task();
28
/**
29
* You can use load method to load the first matching task into an empty model
30
*/
31
$taskFromDB->Load('name = ?', [$taskName]);
32
33
var_dump($taskFromDB);
Copied!
Last modified 11mo ago