larium/model

An abstract superclass for domain models.

1.1.0 2015-08-17 20:40 UTC

This package is auto-updated.

Last update: 2024-11-11 14:55:42 UTC


README

Installation

You can install this library using Composer

Command line

In root directory of your project run through a console:

$ composer require "larium/model":"~1.0"

Composer.json

Include require line in your composer.json file

{
	require: {
    	"larium/model": "~1.0"
    }
}

and run from console in the root directory of your project:

$ composer update

After this you must require autoload file from composer.

<?php
require_once 'vendor/autoload.php';

Usage

<?php
# UserModel.php

class UserModel extends Larium\AbstractModel
{
    protected $username;

    protected $email;
}

Using default constructor.

$user = new UserModel();

$user->setUsername('JohnDoe');

echo $user->getUserName(); # echoes JohnDoe.

Using create method

<?php

$data = array('username' => 'johnDoe');
$user = UserModel::create($data);

echo $user->getUserName(); # echoes JohnDoe.

Using assignData method

<?php

$user = new UserModel();

$data = array('username' => 'JohnDoe');
$user->assignData($data);

echo $user->getUsername(); # echoes JohnDoe

Using create method with constructor arguments

<?php

# CommentModel.php

class CommentModel extends AbstractModel
{
    protected $user;

    protected $content;

    public function __construct(UserModel $user)
    {
        $this->user = $user;    
    }
}
<?php
$user = new UserModel();
$date = array('content' => 'Lorem Ipsum');
$comment = CommentModel::create($data, array($user));

$comment->getUser(); # return UserModel instance.