mibadger/active-record

This package is abandoned and no longer maintained. No replacement package was suggested.

The Active Record Component

v2.0.0 2021-08-13 10:04 UTC

This package is auto-updated.

Last update: 2022-11-19 17:09:39 UTC


README

Build Status Code Coverage Scrutinizer Code Quality SensioLabsInsight

The Active Record Component. For more in depth information, check out the docs directory. New to ActiveRecord? Begin at docs/AbstractActiveRecord.md

Example

<?php

use miBadger\ActiveRecord\AbstractActiveRecord;

/**
 * The user class.
 */
class User extends AbstractActiveRecord
{
	/** @var string The name. */
	private $name;

	/**
	 * {@inheritdoc}
	 */
	public function getTableName()
	{
		return 'example';
	}

	/**
	 * {@inheritdoc}
	 */
	public function getTableDefinition() 
	{
		return [
			'name' =>
			[
				'value' => &$this->name,
				'validate' => null,
				'type' => 'VARCHAR',
				'length' => 256,
				'properties' => ColumnProperty::NOT_NULL | ColumnProperty::UNIQUE
			]
		];
	}

	/**
	 * Returns the name.
	 *
	 * @return string the name.
	 */
	public function getName()
	{
		return $this->name;
	}

	/**
	 * Sets the name.
	 *
	 * @param string $name
	 * @return $this
	 */
	public function setName($name)
	{
		$this->name = $name;

		return $this;
	}
}
<?php

/**
 * Create an active record instance.
 */
$user = new User($pdo);

/**
 * Read the row with the given ID from the database.
 */
$user->read($id);

/**
 * Returns the name.
 */
$user->getName(); // John

/**
 * Set a new name.
 */
$user->setName('Jane');

/**
 * Synchronize the active record with the database.
 */
$user->sync();

Requirements

ActiveRecord is tested with

  • php 7.2+
  • mysql (mariadb) 8.0.6.16 For more detailed information, check out the scrutinizer reports.

Setup

The easiest way to install miBadger.ActiveRecord is using composer composer require mibadger/activerecord or by including the package in your composer.json

"require": {
	"mibadger/activerecord": "^2.0"
}

Limitations

Changelog

2.0-dev

  • New interfaces & style of declaring columns
  • getTableDefinition now returns an associative array of columnEntries
  • added functionality for code reuse by means of traits. Some common traits can be found in src/Traits/
  • added autoApi trait for handling simple CRUD operations.