Files
crmPRO/autoload/class.DbModel.php
2024-11-10 11:11:35 +01:00

59 lines
1.3 KiB
PHP

<?php
class DbModel
{
public $data;
public $table;
public $table_key = 'id';
public function __construct( $id )
{
global $mdb;
if ( $id )
{
$result = $mdb -> get( $this -> table, '*', [ $this -> table_key => $id ] );
if ( is_array( $result ) ) foreach ( $result as $key => $val )
$this -> $key = $val;
}
}
public function __get( $variable )
{
if ( array_key_exists( $variable, $this -> data ) )
return $this -> data[$variable];
}
public function __set( $variable, $value )
{
$this -> data[$variable] = $value;
}
public function save()
{
global $mdb;
if ( $this -> __get( $this -> table_key ) )
{
$table_id_param = $this -> table_key;
$table_id_value = $this -> __get( $this -> table_key );
$data_tmp = $this -> data;
unset( $data_tmp[ $table_id_param ] );
return $mdb -> update( $this -> table, $data_tmp, [ $table_id_param => $table_id_value ] );
}
else
{
$mdb -> insert( $this -> table, $this -> data );
$this -> __set( $this -> table_key, $mdb -> id() );
}
return $this -> __get( $this -> table_key );
}
public function delete()
{
global $mdb;
return $mdb -> delete( $this -> table, [ $this -> table_key => $this -> __get( $this -> table_key ) ] );
}
}