PHP code example of jelix / dao

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

    

jelix / dao example snippets



use \Jelix\Database\AccessParameters;
use \Jelix\Database\Connection;
use \Jelix\Dao\DaoLoader;

// create a connector to the database
$accessParameters = new AccessParameters(
    array(
      'driver'=>'sqlite3',
      "database"=>"/...../tests.sqlite3",
    ), 
    array('charset'=>'UTF-8')
);

$connector = Connection::create($accessParameters);

// path to a directory where compiled class can be stored
$tempPath = '...'; 

// path to a directory where to find dao xml files
$daosDirectory = '...';

// instance of a dao loader, using a Context object
$loader = new DaoLoader(
    new \Jelix\Dao\Context(
        $connector,
        $tempPath,
        $daosDirectory
    )
);

$daoName = 'article'; // this the filename without path and extension

$dao = $loader->get($daoName);


// Storing a new object

$article = $dao->createRecord();
$article->title = "My title";
$article->content = "Lorem Ipsum";
$dao->insert($article);

echo "id of the new article: ".$article->id."\n";


// Query all records from the article table
$list = $dao->findAll();
foreach($list as $record) {
    echo $record->title;
}

// retrieve a single record
$artId = 1;
$article = $dao->get($artId);
echo $article->title;

// updating the record
$article->title = 'New title';
$dao->update($article);

// deleting a record
$dao->delete($article->id);

//...