PHP code example of glynnforrest / active-doctrine

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

    

glynnforrest / active-doctrine example snippets


//create a Doctrine database connection to use
$config = [
    'driver' => 'pdo_mysql',
    'host' => 'localhost',
    'user' => 'user',
    'password' => 'password',
    'dbname' => 'app',
];

$conn = Doctrine\DBAL\DriverManager::getConnection($config);

// insert and update
$author = new App\Entity\Author($conn);

$author->name = 'Glynn';
$author->age = 102;

// insert
$author->save();

$author->age = 100;

// update
$author->save();

// selecting
// SELECT * FROM authors WHERE age > ?
$old_authors = Author::select($conn)
    ->where('age', '>', 100)
    ->execute();

foreach ($old_authors as $author) {
    echo $author->name;
    // books are loaded lazily
    // SELECT * FROM books WHERE authors_id = ?
    foreach ($author->books as $book) {
        echo $book->name;
        echo $book->page_count;
    }
}

// selecting with eager loading
// SELECT * FROM authors WHERE age > ?
// SELECT * FROM books WHERE id IN (?, ?, ?, ?) AND page_count > ?
$old_authors = Author::select($conn)
    ->where('age', '>', 100)
    ->with('books', function($s) {
        $s->where('page_count', '>', 100);
    })
    ->execute();

// deleting
$old_authors->delete();