MeekroDB Quick Start Docs ORM Docs FAQ
Download GitHub

Misc MeekroORM Variables and Functions

static::$_tablename

The default table name is the class name followed by an "s", and made lowercase (so class Account will try to access table accounts). You can change it, though:

class Account extends MeekroORM {
  // use database table acct instead of accounts
  static $_tablename = 'acct';
}

static::_meekrodb()

By default, MeekroORM will connect to the static instance of MeekroDB. If you have your own pre-existing MeekroDB object that you want the ORM to use, you'll need to have this function return that object. If you are using MeekroDB within your application, it's pretty important that this function get the same instance of it that you already have. This will speed up your application since MeekroORM won't have to open up a new database connection, and transaction involving MeekroDB+MeekroORM will work correctly.

Warning: This function gets run frequently. If you put new MeekroDB() in it without caching it, you will be creating a new database connection for every query.

class Account extends MeekroORM {
  static function _meekrodb() {
    // maybe your application uses the object version of MeekroDB
    // and keeps it as a global variable?
    return $GLOBALS['meekrodb'];
  }
}

toHash() / toRawHash()

Returns a hash of all the keys that go in the database, and their current values in this object. The difference is that toHash() returns the values as they were set by the user, including things like DateTime objects and bools. On the other hand, toRawHash() returns the values after they've gone through the "marshal" step, and they will all be database-native types like string, int, or double.

dirtyHash() / dirtyFields()

dirtyHash() is like toRawHash(), except it only includes those keys/values that have been changed and not yet saved. dirtyFields() returns just the field names, not the values. If the record is "fresh" (a new record that hasn't been INSERTed yet) then all fields that are going to go in the database are considered dirty.

$Account = Account::Load(1);
$Account->email = '[email protected]'; // change email

var_dump($Account->dirtyHash()); // returns [email => [email protected]]
var_dump($Account->dirtyFields()); // returns [email]

$Account->Save();

var_dump($Account->dirtyHash()); // returns []
var_dump($Account->dirtyFields()); // returns []

has()

Returns true if the object has a database column of this type, otherwise returns false.

$Account = Account::Load(1);
var_dump($Account->has('email')); // true
var_dump($Account->has('xxydf')); // false

get() / set()

Equivalent to getting and setting properties on the object directly. There's one important difference, though: if you directly set a property that doesn't exist as a database column, it'll just be a temporary property on the object. However, get() and set() expect the key to be a valid database column.

$Account = Account::Load(1);

// these two are equivalent
$Account->email = '[email protected]';
$Account->set('email', '[email protected]');

// also equivalent
echo $Account->email;
echo $Account->get('email');

// this isn't a database column, so this will just be a temporary property on the object
// and won't be saved to the database
$Account->xxydf = 'abcde';

// .. and you can read it back!
echo $Account->xxydf;

$Account->set('xxydf', 'defgh'); // throws an exception because not a real column
$Account->get('xxydf'); // also throws exception

getraw() / setraw()

Just like get() and set() except these are the values that haven't been marshaled/unmarshaled.

$Account = Account::Load(1);
var_dump($Account->get('created_at')); // returns a PHP DateTime object
var_dump($Account->getraw('created_at')); // returns e.g. 2020-01-01 10:10:10

// setraw() is the value that's going to go in the database directly
$Account->setraw('created_at', '2024-01-01 10:10:10');

// the set() value will be marshaled first, so you can do this
$Account->set('created_at', new DateTime('now'));

// technically this will work too, because the marshaler is flexible
$Account->set('created_at', '2024-01-01 10:10:10');

isFresh()

Returns true if this object has been created, but not yet saved. Otherwise returns false.

lock()

Reloads the current record with FOR UPDATE in the SELECT query, which locks the record and prevents others from writing to it until your transaction has committed.