MeekroDB Quick Start Docs ORM Docs FAQ
Download GitHub

Marshaling and Data Types

MeekroORM always attempts to read your table structure and figure out whether certain fields are string, int, double, or datetime. When loading or saving values, we encode and decode them appropriately. However, you can override the auto-detected data type or create your own as shown below.

The static::$_columns hash

The $_columns hash in your class can specify the data type for each column. If you don't specify a column, MeekroORM will auto-detect its type. The data types you can specify by default are string, int, double, datetime, bool, and json. Here is an example:

class Account extends MeekroORM {
  static $_columns = [
    'password' => ['type' => 'string'],
    'created_at' => ['type' => 'datetime'],
    'is_admin' => ['type' => 'bool'],
  ];
}

By default, the string, int, and double types are handled pretty much like you'd expect. For datetime, the value will be converted to a PHP DateTime class. For bool, it will be accessible as a PHP bool and stored in the database as a 1 or 0. Finally, the json type lets you easily create PHP arrays and hashes, and they'll be stored in the database as a json string.

The json type

Since it's a bit unusual, let's try an example with the json type. We'll give our Account class a data field that can store a hash. In MySQL, this data column should be a string type such as varchar(255).

class Account extends MeekroORM {
  static $_columns = [
    'data' => ['type' => 'json'],
  ];
}

Now let's store something:

$Account = Account::Load(1);
$Account->data = ['key' => 'value'];
$Account->data['key2'] = 'value2';
$Account->Save();

Your hash will be stored in MySQL as json data. Simple enough, right?

What if I don't want timestamps loaded as the PHP DateTime class?

I think we all agree the DateTime class kinda sucks, so let's dump it. First, you can set $_columns to mark the field as a string. It would then be presented as-is and not encoded or decoded as a DateTime. If the field was called created_at, it would look like this:

class Account extends MeekroORM {
  static $_columns = [
    'created_at' => ['type' => 'string'],
  ];
}

Alternatively, if you wanted to use a timestamp library other than PHP's native DateTime, you could do that too. Let's use the excellent Carbon library, as shown here:

class Account extends MeekroORM {
  function _marshal_type_datetime($key, $value, $is_nullable) {
    // 0000-00-00 00:00:00 is technically not a valid date, and postgres rejects it
    // so lets use 1970-01-01 as default
    if (!$is_nullable && is_null($value)) $value = '1970-01-01 00:00:00';

    // Carbon inherits from DateTime, so this is fine
    if ($value instanceof \DateTime) return $value->format('Y-m-d H:i:s');
    return $value;
  }
  function _unmarshal_type_datetime($key, $value) {
    if (is_null($value)) return null;
    if ($value) return Carbon::createFromFormat('Y-m-d H:i:s', $value);
    return $value;
  }
}

See what we did there? We created a "marshal" function that encodes the Carbon/DateTime object to a MySQL datetime string. We also created an "unmarshal" function that goes in the other direction. Now our datetimes will be Carbon objects!

If you wanted these custom functions for all of your MeekroORM classes, you could just create a class that they inherit from:

abstract class MyORM extends MeekroORM {
  function _marshal_type_datetime($key, $value, $is_nullable) {
    // 0000-00-00 00:00:00 is technically not a valid date, and postgres rejects it
    // so lets use 1970-01-01 as default
    if (!$is_nullable && is_null($value)) $value = '1970-01-01 00:00:00';

    // Carbon inherits from DateTime, so this is fine
    if ($value instanceof \DateTime) return $value->format('Y-m-d H:i:s');
    return $value;
  }
  function _unmarshal_type_datetime($key, $value) {
    if (is_null($value)) return null;
    if ($value) return Carbon::createFromFormat('Y-m-d H:i:s', $value);
    return $value;
  }
}

// use the custom marshal/unmarshal functions here and everywhere
class Account extends MyORM {}

Creating a custom data type

You can create a custom data type by defining the marshal and unmarshal functions for it, and then setting a column to that type with $_columns. You can use the functions shown above as examples to get you started.