PHP code example of matajm / php-orm
1. Go to this page and download the library: Download matajm/php-orm 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/ */
matajm / php-orm example snippets
Database::$dns = 'pgsql:port=[port];host=[host];dbname=[db_name]';
Database::$username = 'Ingrese su usuario';
Database::$password = 'Contraseña';
use \phpORM\Models;
use \phpORM\Fields\StringField;
use \phpORM\Fields\AutoIncrementField;
use \phpORM\Fields\IntegerField;
class Animal extends Models {
protected static $table_name = "animal";
public $id = [
"type" => AutoIncrementField::class,
"db_column" => "animal_id",
"primary_key" => true
];
public $name = [
"type" => StringField::class,
"db_column" => "animal_nombre"
];
public $eyes = [
"type" => StringField::class,
"db_column" => "animal_cantidad_ojos",
"default" => 0
];
}
class Perro extends Animal {
protected static $table_name = "perro";
public $eyes = [
"type" => StringField::class,
"db_column" => "animal_cantidad_ojos",
"default" => 2
];
}
Animal::createTable() # crea la tabla animal
Database::createTables([
Animal::class,
Perro::class
]) # crea todas las tablas
Animal::dropTable() # elimina la tabla animal
Database::dropTables([
Animal::class,
Perro::class
]) # crea todas las tablas
$mascota = Perro::create([
"name" => "Punky",
]);
$mascota->name
#Punky
$mascota->id
#NULL
$mascota->save() # Inserta el registro
$mascota->id
# 1
$mascota->name = "Bolt";
$mascota->save();
$mascota->remove();
$mascota->save(); # IntegrityError no existe
Perro::find() # busca todos los registros
Perro::findOne() # busca un registro
Perro::findId(1) #busca el registro que posea la clave id 1