Basic CRUD model with PDO connection
CRUD (create, read, update, delete) model class for standalone PHP projects, but with Laravel-like usage. Beside the model, it includes an autoloader, a configuration file loader and a PDO connector, with table prefix/suffix options as well. For full documentation visit project´s Wiki page
Install package to your app via Composer:
composer require maarsson/model
Classes will be under the Maarsson
namespace, via Composer´s PSR-4 autoloader. After you configured your database connection (see the documentation, how to do that), you can use your database tables as models by extending this base class in your MyModel.php.
namespace App;
use Maarsson\Model;
class MyModel extends Model
{
}
$properties = array(
'title' => 'First Object',
'size' => 99
);
$my_model = MyModel::create($properties);
// single object by ID
$my_model = MyModel::find(1);
// single object by property value
$my_model = MyModel::find('First Object','title');
// all objects
$my_model = MyModel::all();
$properties = array(
'title' => 'First Object', // equivalent with ['title','=', 'First Object']
['size','>=', 99]
);
$my_model = MyModel::where($properties);
// simple ascending order by a field
$properties = array();
$my_model = MyModel::where($properties, 'name');
// complex ordering
$properties = array();
$orderBy = array(
'name' => 'asc',
'value' => 'desc'
);
$my_model = MyModel::where($properties, $orderBy);
$properties = array(
'title' => 'First Object',
['size','>=', 99]
);
$my_model = MyModel::findOrCreate($properties);
$my_model = MyModel::find(1);
// update one property
$my_model->updateProperty('name','Updated Name');
// mass-update properties
$properties = array(
'title' => 'Updated Object',
'size' => 100
);
$my_model->update($properties);
$my_model = MyModel::find(1);
$my_model->delete();
Models can have different types of relationships:
User
has one Account
)User
has many Phone
)Account
belongs to one User
)User
belongs to many Role
)The relationship keys in the database are the id
and the related_table_id
columns.
Always use full namespaced class names for definitions.
class User extends Model
{
protected static $_hasOne = [
'account' => 'App\\Account' // 'property_name_in_this_model' => 'Other_Model'
];
protected static $_hasMany = [
'phones' => 'App\\User\\Phone'
];
}
Related data will automatically attached when getting the model.
You can use autoloader to load your classes from multiple direcories. Just add:
Maarsson\Autoloader::addPath('path/to/classes');
Maarsson\Autoloader::addPath('path/to/modules');