molten/core

A CRUD database manipulation tool.


License
Other

Documentation

Molten Core

Molten Core is libary I wrote for practice/teaching purposes. Here's how it works.

Composer Setup

Run this command to install via composer.

composer require molten/core

Create a class

Anywhere you keep models in your project, create a class like so.

<?php

namespace Name\Space\Model;

use Molten\Core\Model\Base;

class Email extends Base
{
    const TABLE_NAME = 'email';
    const VARIABLES = [
        'address' => [
            'type' => 'string',        
        ],
    ];

    protected $address;

    public function getAddress() : string
    {
        return $this->address;
    }

    public function setAddress(string $address) : bool
    {
        return $this->validateAndSet('address', $address);
    }
}

The 'TABLE_NAME' constant is self-explanatory; it's what table to look in.

The 'VARIABLES' constant is used to define what columns will be in the table.

Currently, this field is used by the validateAndSet function to set the data.

Later on, I also want to use this to generate tables/column and populate them with mock data.

Read an object

You can read an object from the database using the following syntax. I

use Name\Space\Model\Email;

/** @var Email $email */
$email = Email::read(1); 

Create an object

You can create an object simply too.

use Name\Space\Model\Email;

/** @var Email $email */
$email = Email::create(
    [
        'address' => 'joe@providenceri.com',
    ]
);

Assuming all the types match those defined in the class' VARIABLES constant, it'll create.

Update an object

With an object instantiated, call the update function.

use Name\Space\Model\Email;

/** @var Email $email */
$email = Email::read(1); 

$email->setAddress('peter@happy-go-lucky.com');
$email->update();

Delete an object

// With an object instantiated, call the delete function.

use Name\Space\Model\Email;

/** @var Email $email */
$email = Email::read(1); 

$email->delete();

Querying

You can query objects in what I think is a cool way too...

$results = Email::find()->where('address')->is('cleveland@usps.com')->fetch();

if ($results->getCount() === 1) {
    $email = $results->grabFirst();
} else if ($results->getCount() > 1) {
    /** @var Email $email */
    foreach ($results->grab() as $email) {
        echo $email->getAddress();
    }
}