PHP code example of friedolinfoerder / wp-activerecord

1. Go to this page and download the library: Download friedolinfoerder/wp-activerecord 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/ */

    

friedolinfoerder / wp-activerecord example snippets



// create a model class for the table {wp-prefix}slideshows 
class Slideshow extends \wp_activerecord\ActiveRecord {
    protected static $table_name = 'slideshows';
}

// create new row
$slideshow = Slideshow::create([
    'title'        => 'Header slideshow',
    'slide_time'   => 3000,
    'slide_effect' => 'fade'
]);

// retrieve by id...
$slideshow = Slideshow::get(1);

// ... and update the row
$slideshow->title = 'New title';
$slideshow->slide_effect = 'slide';
$slideshow->save();

class Slideshow extends \wp_activerecord\ActiveRecord {
    protected static $casts = [
        'num_slides' => 'int',
        'duration' => 'float',
        'active' => 'boolean',
        'created_at' => 'datetime',
    ];
}

$activeRecord = Table::create();
// or
$activeRecord = Table::create([
   'name'  => 'wp-activerecord',
   'title' => 'WordPress ActiveRecord'
]);

Table::delete_by_id(3);

$activeRecords = Table::get(); // all records
$activeRecord = Table::get(3); // one record by id

$activeRecord = Table::get_one_by_title('WordPress');
$array = Table::get_by_name_or_title('wp-activerecord', 'WP');
$row = Table::get_row_by_name_and_title('wp', 'WP');
$var = Table::get_var_name_by_id(3);

$table_name = Table::get_table_name();

$last_insert_id = Table::insert([
   'name'  => 'wp-activerecord',
   'title' => 'WordPress ActiveRecord'
]);
// or
$last_insert_id = Table::insert([[
   'name'  => 'ActiveRecord',
   'title' => 'Class ActiveRecord'
], [
   'name'  => 'Query',
   'title' => 'Class Query'
]]);

$query = Table::query();

$query = Table::update('name', 'wp-activerecord-updated');
// or
$query = Table::update([
   'name'  => 'wp-activerecord-updated',
   'title' => 'Updated WordPress ActiveRecord'
]);

$wpdb = Table::wpdb();

// use case:
$userInput = '20%';
Table::query()
  ->delete()
  ->where('name', 'like', '%' . Table::wpdb()->esc_like($userInput) . '%')
  ->execute();

$activeRecord->delete();

$activeRecord->save();

// in your derived class:
protected function save_pre($isNew) {
    $this->new = $isNew ? 1 : 0;
}

// in your derived class:
protected function save_post($isNew) {
    // do something with $this
}

// in your derived class:
protected function delete_pre() {
    // do something with $this
}

// in your derived class:
protected function delete_post() {
    // do something with $this
}

$wpdb = Query::wpdb();

$activeRecord = Table::query()
  ->select('id', 'name')
  ->get();

Table::query()
  ->delete()
  ->where('name', 'wp')
  ->execute();

Table::query()
  ->update()
  ->set('name', 'wp')
  ->execute();
// or
Table::query()
  ->update('name', 'wp')
  ->execute();
// or
Table::query()
  ->update([
    'name'  => 'wp',
    'title' => 'WordPress'
  ])
  ->execute();

Table::query()
  ->set('name', 'wp')
  ->execute();
// or
Table::query()
  ->set([
    'name'  => 'wp',
    'title' => 'WordPress'
  ])
  ->execute();

Table::query()
  ->insert([
    'name'  => 'wp',
    'title' => 'WordPress'
  ])
  ->execute();
// or
Table::query
  ->insert([[
    'name'  => 'ActiveRecord',
    'title' => 'Class ActiveRecord'
  ], [
    'name'  => 'Query',
    'title' => 'Class Query'
  ]])
  ->execute();

$activeRecords = Table::query()
  ->where('name', 'wp')
  ->where('title', 'LIKE', '%active%')
  ->where([
    'start' => 12,
    'end'   => 37
  ])
  ->where(['deleted_at', null]) // query for NULL value, produces  `deleted_at` IS NULL
  ->where('value', '>', ['RAND()']) // raw value wrapped in array
  ->where('numbers', 'in', [[1, 2, 3]] // a array as raw value will be joined
  ->get();

$activeRecords = Table::query()
  ->group_by('name', 'asc')
  ->get();

$activeRecords = Table::query()
  ->group_by('name')
  ->having(["SUM(price)"], ">", 10) // raw column value wrapped in array
  ->get();

$activeRecords = Table::query()
  ->order_by('description')
  ->order_by('name', 'desc')
  ->get();

$activeRecords = Table::query()
  ->limit(5)
  ->get();

$activeRecords = Table::query()
  ->offset(10)
  ->get();

$activeRecords = Table::query()
  ->join('OtherTable', 'id', 'table_id')
  ->get();

$sql = Table::query()
  ->select('description')
  ->where('description', 'like', 'Title: %')
  ->sql();

$results = Table::query()
  ->get_results();

$row = Table::query()
  ->where('name', 'this is a unique name')
  ->get_row();

$descriptions = Table::query()
  ->select('description')
  ->get_col();

$description = Table::query()
  ->select('description')
  ->where('name', 'this is a unique name')
  ->get_var();

$activeRecords = Table::query()
  ->get();

$activeRecord = Table::query()
  ->where('name', 'this is a unique name')
  ->get_one();

Table::query()
  ->delete()
  ->where('name', 'this is a unique name')
  ->execute();