PHP code example of tarasmatskovich / house-orm

1. Go to this page and download the library: Download tarasmatskovich/house-orm 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/ */

    

tarasmatskovich / house-orm example snippets




use houseorm\mapper\annotations\Gateway;
use houseorm\mapper\annotations\Field;

/**
 * Class User
 * @package house\Entities\User
 * @Gateway(type="datatable.users")
 */
class User implements UserInterface
{

    /**
     * @var int
     * @Field(map="id")
     */
    private $id;

    /**
     * @var null|string
     * @Field(map="name")
     */
    private $name;

    /**
     * User constructor.
     * @param null $name
     */
    public function __construct($name = null)
    {
        $this->name = $name;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

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

* @Gateway(type="datatable.tablename")

* @Gateway(type="datatable.users")

* @Field(map="column_name")

`   /**
     * @Field(map="name")
     */
    private $name;



namespace house\Repositories\UserRepository;

use houseorm\mapper\DomainMapper;

/**
 * Class UserRepository
 * @package house\Repositories
 */
class UserRepository extends DomainMapper implements UserRepositoryInterface
{

}


use houseorm\config\Config;

 $configParams = [
        'driver' => Config::DRIVER_MYSQL,
        'host'=> '127.0.0.1',
        'database' => 'orm',
        'user' => 'root',
        'password' => ''
];

Config::DRIVER_MYSQL

Config::DRIVER_MEMORY


use houseorm\config\Config;
use houseorm\EntityManager;
use house\Repositories\UserRepository;

 $configParams = [
        'driver' => Config::DRIVER_MYSQL,
        'host'=> '127.0.0.1',
        'database' => 'orm',
        'user' => 'root',
        'password' => ''
];

$config = new Config($configParams);
$houseEntityManager = new EntityManager($config);
$houseEntityManager->setMapper('User', new UserRepository(User::class));

$userRepository = $entityManager->getMapper('User');

$user = $userRepository->find(5);
if (null !== $user) {
    $user->getId(); // 5
    $user->getName(); // my name
}

$user = $userRepository->findOneBy(['name' => 'Test name');
...
$users = $userRepository->findBy([['name', 'LIKE', '%name%']);
foreach ($users as $user) {
    //
}

$user = new User();
$user->setName('Test name');
$userRepository->save($user);
$user->getId() // 6
$user->getName() // Test name

$user->setName('Updated name');
$userRepository->save($user);
...
$user = $userRepository->find(6);
$user->getName(); // Updated name

$user = $userRepository->find(6);
if (null !== $user) {
    $userRepository->delete($user);
}

use houseorm\mapper\annotations\Relation;

/**
 * Class User
 * @package house\Entities\User
 * @Gateway(type="datatable.users")
 */
class User implements UserInterface {

    /**
     * @var int
     * @Field(map="id")
     * @Relation(entity="Comment", key="userId")
     */
    private $id;
    ...
}

* @Relation(entity="Entity key in entity manager", key="related field in this entity")



use houseorm\mapper\annotations\Gateway;
use houseorm\mapper\annotations\Field;
use houseorm\mapper\annotations\Relation;


/**
 * Class Comment
 * @package house\Entities\Comment
 * @Gateway(type="datatable.comments")
 */
class Comment implements CommentInterface
{
    ...
    /**
     * @var null|int
     * @Field(map="user_id")
     * @Relation(entity="User", key="id")
     */
    private $userId;
    ...
}


use houseorm\mapper\annotations\ViaRelation;

/**
 * Class User
 * @package house\Entities\User
 * @Gateway(type="datatable.users")
 * @ViaRelation(entity="Role", via="UserRole", firstLocalKey="id", firstForeignKey="userId", secondLocalKey="id", secondForeignKey="roleId")
 */
class User implements UserInterface



namespace house\Entities\UserRole;

use houseorm\mapper\annotations\Gateway;
use houseorm\mapper\annotations\Field;
use houseorm\mapper\annotations\Relation;

/**
 * Class UserRole
 * @package house\Entities\UserRole
 * @Gateway(type="datatable.user_roles")
 */
class UserRole implements UserRoleInterface
{

    /**
     * @var int
     * @Field(map="id")
     */
    private $id;

    /**
     * @var int
     * @Field(map="user_id")
     * @Relation(entity="User", key="id")
     */
    private $userId;

    /**
     * @var int
     * @Field(map="role_id")
     * @Relation(entity="Role", key="id")
     */
    private $roleId;
    ...

}




namespace house\Entities\Role;

use houseorm\mapper\annotations\Gateway;
use houseorm\mapper\annotations\Field;

/**
 * Class Role
 * @package house\Entities\Role
 * @Gateway(type="datatable.roles")
 */
class Role implements RoleInterface
{

    /**
     * @var int
     * @Field(map="id")
     */
    private $id;

    /**
     * @var string
     * @Field(map="title")
     */
    private $title;
    ...
}


use houseorm\Cache\Config\CacheConfig;
use houseorm\config\Config;
use houseorm\EntityManager;

$config = new Config($configParams);
$config->setCacheConfig(new CacheConfig(CacheConfig::MEMORY_DRIVER));
$entityManager = new EntityManager($config);

use houseorm\Cache\Config\CacheConfig;
use houseorm\config\Config;
use houseorm\EntityManager;

$config = new Config($configParams);
// driver - CacheConfig::REDIS_DRIVER
// cache lifetime - 10
// Redis host - 127.0.0.1
$config->setCacheConfig(new CacheConfig(CacheConfig::REDIS_DRIVER, 10, '127.0.0.1'));
$entityManager = new EntityManager($config);