PHP code example of sinevia / php-library-schemaless

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

    

sinevia / php-library-schemaless example snippets


composer 

\Sinevia\Schemaless::createTables();

\Sinevia\Schemaless::deleteTables();

// 1. Create new person
$person = \Sinevia\Schemaless::createEntity([
    'Type' => 'Person',
    'Title' => 'Peter Pan'
]);

// 2. Check if successful
if (is_null($person)) {
    die('Entity failed to be created');
} else {
    echo 'New entity created with Id '.$person['Id']);
    var_dump($person);
}

// 3. Create new person with attributes
$person = \Sinevia\Schemaless::createEntity([
    'Type' => 'Person',
    'Title' => 'Peter Pan',
        ], [
    'FirstName' => 'Peter',
    'LastName' => 'Pan',
    'AddressLine1' => '23 Kings Way',
    'AddressLine2' => 'Wolves Den',
    'Postcode' => 'WWW 123 NET',
    'Country' => 'WW',    
]);

// 4. Check if successful
if (is_null($person)) {
    die('Entity failed to be created');
} else {
    echo 'New entity created with Id '.$person['Id']);
    var_dump($person);
}


// 1. Add attributes individually
\Sinevia\Schemaless::setAttribute($personId, 'AddressLine1', '45 Queens Road');
\Sinevia\Schemaless::setAttribute($personId, 'AddressLine2', 'Foxes Layer');

// 2. Add many attributes at once
\Sinevia\Schemaless::setAttributes($personId, [
    'AddressLine1' => '122 Shepherds Close',
    'AddressLine2' => 'Cows Barn',
]);

// 1. Retrieve entity by Id
$person = \Sinevia\Schemaless::getEntity($personId);
var_dump($person);

// 1. Retrieve entity by Id with Attributes
$person = \Sinevia\Schemaless::getEntity($personId, ['withAttributes' => true]);
var_dump($person);


// 2. Retrieve entities by search
$people = \Sinevia\Schemaless::getEntities([
    'Type' => 'Person',
    'IsActive' => 'Yes',
    'limitFrom' => 0,
    'limitTo' => 2,
    // Advanced querying
    'wheres' => [
        ['CreatedAt', '>', '2018-07-27 22:24:10'],
        ['CreatedAt', '<', '2018-07-27 23:31:50'],
        ['Attribute_FirstName', '=', 'Peter'],
        ['Attribute_LastName', 'LIKE', '%an%'],
    ],
    // Should returned entities contain also the attributes
    'withAttributes' => true,
]);

var_dump($people);


// 1. Retrieve and display entity attributes
echo \Sinevia\Schemaless::getAttribute($personId, 'AddressLine1');
echo \Sinevia\Schemaless::getAttribute($personId, 'AddressLine2');

// 2. Get all atributes at once
$attributes = \Sinevia\Schemaless::getAttributes($personId);

// 1. Update entity by Id
\Sinevia\Schemaless::updateEntity($personId,[
    'Title' => 'Updated'
]);

// 2. Update entity by Id with Attributes
\Sinevia\Schemaless::updateEntity($personId,[
    'Title' => 'Updated'
],[
    'Postcode' => 'New Postcode';
]);


$isDeleted = \Sinevia\Schemaless::setAttribute($personId,'AddressLine1','Updated Address 1');

$isDeleted = \Sinevia\Schemaless::deleteEntity($personId);

$isDeleted = \Sinevia\Schemaless::deleteAttribute($personId,'AddressLine1');

class MySchemaless extends Sinevia\Schemaless {
    public static $tableEntity = 'my_schemaless_entity';
    public static $tableAttribute = 'my_schemaless_attribute';

}

$page = MySchemaless::createEntity([
    'Type'=>'Page',
    'Title'=>'Home Page'
]);

class Person extends SchemalessDataObject {

    const TYPE = 'Person';

    function __construct() {
        $this->setType(self::TYPE);
    }

    public function getEmail() {
        return $this->getAttribute('Email');
    }

    public function setEmail($email) {
        $this->setAttribute("Email", $email);
    }

}

class PersonRepository extends SchemalessDataObjectRepository {

    public static function findPersonByEmail($email) {
        $entities = \Sinevia\Schemaless::getEntities([
                    'Type' => Person::TYPE,
                    'limitFrom' => 0,
                    'limitTo' => 1,
                    'wheres' => [
                        ['Attribute_Email', '=', $email]
                    ],
                    'withAttributes' => true,
        ]);

        if (count($entities) < 1) {
            return null;
        }

        $person = new Person;
        self::hydrateObject($user, $entities[0]);
        return $person;
    }
    
}

// Finding an object

$person = Person::findByEmail('[email protected]');
if(is_null($person)){
    echo $person->getEmail();
}