PHP code example of diogodg / neoorm

1. Go to this page and download the library: Download diogodg/neoorm library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

diogodg / neoorm example snippets


// Returns an object with all table columns based on the provided $id
$result = (new Appointment)->get($id);

// Returns an object with all table columns based on the provided $name
$result = (new Appointment)->get($name, "name");

// Returns an array of objects with all columns and records from the table
$result = (new Appointment)->getAll();

// Returns an array of objects with all table columns based on the provided filters
$db = new Appointment;
$results = $db->addFilter("start_date", ">=", $start_date)
              ->addFilter("end_date", "<=", $end_date)
              ->addFilter("schedule_id", "=", intval($schedule_id))
              ->addFilter("status", "!=", $status)
              ->selectAll();

// Returns an array of objects with the specified columns, based on the added filters and joins
use Diogodg\Neoorm\Definitions\Raw

$db = new Appointment;
$result = $db->addJoin("LEFT", "user", "user.id", "appointment.user_id")
             ->addJoin("INNER", "schedule", "schedule.id", "appointment.schedule_id")
             ->addJoin("LEFT", "client", "client.id", "appointment.client_id")
             ->addJoin("INNER", "employee", "employee.id", "appointment.employee_id")
             ->addFilter("schedule.company_id", "=", $company_id)
             ->selectColumns("appointment.id", "user.tax_id", ["client.name","client_name"],new Raw("user.name as user_name"), "user.email", "user.phone",["schedule.name","schedule_name"], ["employee.name","employee_name"], "start_date", "end_date");

// Returns an array of objects with the specified columns that match the provided values, based on the specified filters and limit
$db = new City;
$result = $db->addFilter("name", "LIKE", "%" . $name . "%")
             ->addLimit(1)
             ->selectAll();

$values = new Employee;

// If $values->id is null, empty, or 0, it will attempt an INSERT command. Otherwise, it will attempt an UPDATE.
$values->id = null; // or "" or 0
$values->user_id = $user_id;
$values->name = $name;
$values->tax_id = $tax_id;
$values->email = $email;
$values->phone = $phone;
$values->start_time = $start_time;
$values->end_time = $end_time;
$values->lunch_start = $lunch_start;
$values->lunch_end = $lunch_end;
$values->days = $days;

// Returns false or the record ID
$result = $values->store();

$db = new Employee;

// Returns true or false
$result = $db->addFilter("name", "=", "John")->deleteByFilter();

$id = 1;
$db = new Employee;

// Returns true or false
$result = $db->delete($id);

try {   
    connection::beginTransaction();

    if ($schedule->set()){ 
        $scheduleUser = new ScheduleUser;
        $scheduleUser->user_id = $user->id;
        $scheduleUser->schedule_id = $schedule->id;
        $scheduleUser->set();

        if($schedule->employee_id){
            $scheduleEmployee = new ScheduleEmployee;
            $scheduleEmployee->employee_id = $schedule->employee_id;
            $scheduleEmployee->schedule_id = $schedule->id;
            $scheduleEmployee->set();
        }
        connection::commit();
    }
} catch (\exception $e){
    connection::rollBack();
}

$id = 1;
$db = new db("tb_employee");

// Returns true or false
$result = $db->delete($id);


namespace App\Models;

use Diogodg\Neoorm\Abstract\Model;
use Diogodg\Neoorm\Migrations\Table;
use Diogodg\Neoorm\Migrations\Column;

class State extends Model {
    // Required parameter that will define the table name in the database
    public const table = "state";

    // Must be in this format
    public function __construct() {
        parent::__construct(self::table);
    }

    // Method responsible for creating the table
    public static function table(){
        return (new Table(self::table, comment:"States table"))
                ->addColumn((new Column("id", "INT"))->isPrimary()->setComment("State ID"))
                ->addColumn((new Column("name", "VARCHAR", 120))->isNotNull()->setComment("State name"))
                ->addColumn((new Column("abbreviation", "VARCHAR", 2))->isNotNull()->setComment("State abbreviation"))
                ->addColumn((new Column("country", "INT"))->isNotNull()->setComment("Country ID of the state"))
                ->addForeignKey(Country::table, column:"country")
                ->addColumn((new Column("ibge", "INT"))->isUnique()->setComment("IBGE ID of the state"))
                ->addColumn((new Column("area_code", "VARCHAR", 50))->setComment("Area codes separated by comma"));
    }

    // Method responsible for inserting initial data into the table
    public static function seed(){
        $object = new self;
        if(!$object->addLimit(1)->selectColumns("id")){
            $object->name = "Acre";
            $object->abbreviation = "AC";
            $object->country = 1;
            $object->ibge = 12;
            $object->area_code = "68";
            $object->store();

            $object->name = "Alagoas";
            $object->abbreviation = "AL";
            $object->country = 1;
            $object->ibge = 27;
            $object->area_code = "82";
            $object->store();

            $object->name = "Amapá";
            $object->abbreviation = "AP";
            $object->country = 1;
            $object->ibge = 16;
            $object->area_code = "96";
            $object->store();

            $object->name = "Amazonas";
            $object->abbreviation = "AM";
            $object->country = 1;
            $object->ibge = 13;
            $object->area_code = "92,97";
            $object->store();
        }
    }
}



use Diogodg\Neoorm\Migrations\Migrate;

// If the recreate parameter is true, all tables will be removed and then recreated
(new Migrate)->execute($recreate = false);