PHP code example of gyde / mysql-object-model

1. Go to this page and download the library: Download gyde/mysql-object-model 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/ */

    

gyde / mysql-object-model example snippets


class User extends \namespace\MOMSimple
{
	const DB = 'administration';
	const TABLE = 'users';

	// Primary column name (VE = 'active';
	const COLUMN_CREATED = 'created';
}

$connection = new \PDO('mysql:host=127.0.0.1', 'myuser', 'mypasswd');

// Sets a global PDO connection for MOMBase to use
\namespace\MOMBase::setConnection($connection, TRUE);

// Sets a specific PDO connection for User to use
User::setConnection($connection);

// Create a new object
$object1 = new User();

// Set object properties (all properties are public by default)
$object1->name = 'foo';
$object1->description = 'imfulloffoo';

// Save the object to the database and refetch it (update autoincrements, timestamps and other default values
$object1->save();

// Fetch the object we just created using primary id (assuming user_id is 1)
$object1 = User::getById(1);
if ($object1 instanceOf User)
{
	echo $object1->name;
}

// Fetch all User objects
$users = User::getAll();
foreach ($users as $user)
{
	echo $user->name;
}

$where = '`'.User::COLUMN_NAME.'` = '.User::escapeStatic('foo');
$someObjects = User::getAllByWhere($where);
foreach ($someObjects as $object)
{
	echo $object->name;


class User extends MOMSimple
{
	...
	/**
	  * Fetch users using name
	  * @param string $name
	  * @return array<int user_id, User>
	  */
	public static function getByName($name)
	{
		$where = '`'.self::COLUMN_NAME.'` = '.self::escapeStatic($name);
		return self::getAllByWhere($where, null, true);
	}
	...
}

$where = '`'.User::COLUMN_NAME.'` = '.User::escapeStatic('foo');
$order = '`'
$someObjects = User::getOne($where);
foreach ($someObjects as $object)
{
	echo $object->name;
}