PHP code example of mavisland / orm
1. Go to this page and download the library: Download mavisland/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/ */
mavisland / orm example snippets
define('DB_HOST', 'localhost');
define('DB_NAME', 'veritabani');
define('DB_CHARSET', 'utf8mb4');
define('DB_USER', 'root');
define('DB_PASS', '');
class Kullanici extends Orm {
protected $fillable = ['ad', 'email'];
protected $tablo = 'kullanicilar';
}
$kullanici = new Kullanici();
$veriler = $kullanici->where('aktif', 1)->orderBy('id', 'DESC')->get();
$kullanici = (new Kullanici())->find(1);
$k = new Kullanici();
$k->fill([
'ad' => 'Tanju',
'email' => '[email protected] '
]);
$k->save();
$k = (new Kullanici())->find(1);
$k->email = '[email protected] ';
$k->save();
$k = (new Kullanici())->find(1);
$k->delete(); // softDelete() özelliği açıksa deleted_at kolonunu günceller
class Kullanici extends Orm {
public function yazilar() {
return $this->hasMany(Yazi::class, 'kullanici_id');
}
}
class Yazi extends Orm {
public function yazar() {
return $this->belongsTo(Kullanici::class, 'kullanici_id');
}
}
$veriler = (new Yazi())->with('yazar')->get();
class Kullanici extends Orm {
protected $rules = [
'ad' => ['
$k = new Kullanici();
$k->fill($_POST);
if ($k->validate()) {
$k->save();
} else {
print_r($k->getErrors());
}
$k = (new Kullanici())->find(1);
$array = $k->toArray();
$json = $k->toJson();
$k = new Kullanici();
$sonuc = $k->where('aktif', 1)->paginate(2, 10); // 2. sayfadan 10 kayıt getir
// $sonuc['data'], $sonuc['toplam'], $sonuc['sayfa_sayisi'] vs.