PHP code example of soneritics / framework

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

    

soneritics / framework example snippets



new Framework\Bootstrap;

// Define the tables we have as Table extends
class Father extends Table {}
class Mother extends Table {}
class Child extends Table {}

// Use the Child table as a base for the queries
$child = new Child;

// Select everything from the children table
$child
    ->select()
    ->execute();

// Join a child with it's parents
$child
    ->select()
    ->leftJoin(new Father, 'Father.id = father_id')
    ->leftJoin(new Mother, 'Mother.id = mother_id')
    ->execute();

// A new child has been born!
$child
    ->insert()
    ->values([
        'firstname' => 'first name',
        'lastname' => 'last name',
        'father_id' => 1,
        'mother_id' => 1
    ])
    ->execute();

// Typo in the baby's name :-)
$child
    ->update()
    ->set('firstname', 'new first name')
    ->where([
        'firstname' => 'first name',
        'lastname' => 'last name'
    ])
    ->execute();

// Typo in the first and lastname of the baby :O
$child
    ->update()
    ->set(['firstname' => 'new first name', 'lastname' => 'new last name'])
    ->where([
        'firstname' => 'first name',
        'lastname' => 'last name'
    ])
    ->execute();

// Selecting with some sorting and limiting
$child
    ->select()
    ->leftJoin(new Father, 'Father.id = father_id')
    ->leftJoin(new Mother, 'Mother.id = mother_id')
    ->orderAsc('lastname')
    ->orderAsc('firstname')
    ->limit(25)
    ->execute();

    // Render the form's start tag <form>
    $this->form
        ->start()
        ->setMethod('post')
        ->setAction('/add-user')
        ->setParam('id', 'loginForm')
        ->setClass('form-horizontal')
        ->render();

    /* Render the inputs for the user's email and name.
     * When the user submits a form that is invalid, the
     * object automatically assigns an error class.
     * By using the setValueFromPost() function, the 
     * previously entered value gets set.
     */
    $this->form
        ->text('User.email')
        ->setParam('id', 'email')
        ->setParam('placeholder', 'Email address here')
        ->setClass('form-control')
        ->setValueFromPost()
        ->render();

    $this->form
        ->text('User.name')
        ->setParam('placeholder', 'Your name')
        ->setClass('form-control')
        ->setValueFromPost()
        ->render();

    $this->form->submit()->render();

    // Form's end tag
    $this->form->end()->render();

class User extends Controller
{
    public function addAction()
    {
        return new View('User/add');
    }

    public function addPost()
    {
        // Create the model, based on the Table object
        $userModel = new Models\User;

        // Validate data
        if (!$userModel->validates($this->fromPost('User'))) {
            return $this->addAction();
        }

        // Insert data and get the last insert id
        $userId = $userModel
            ->insert()
            ->set($this->fromPost('User'))
            ->execute();

        /* The user is now added to the database.
         * The following code can fetch the user data:
         * $userData = $userModel->select()->where(['id' => $userId])->execute()->get();
         *
         * This will return the following array:
         * [
         *     'User' => [
         *         'id' => 1,
         *         'email' => 'User email address',
         *         'name' => 'User name'
         *     ]
         * ]
         */

        // Done, redirect to the homepage
        new Redirect('/');
    }
}