PHP code example of datado / data

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

    

datado / data example snippets


    
    /**
     * This is the entity we are going to use in this example.
     */
    class File {
        public $id;
        public $filePath;
        public $size;
        public $fileName;
    }
    
    // First we will have to connect to out database
    $pdo = new PDO('mysql:host=localhost;dbname=demo', 'username', 'secret_password');
    
    /* Then we create the repository we mentioned earlier. To be able to do this we need 
     * some information:
     * 
     * - The class of the entity we want to store or get from this repository
     * - The PDO connection to the database
     * - The name of the property that identifies this entity. This will be used when 
     *     updating and inserting objects
     */
    $repository = new Repository(File::class, $pdo, 'id');


    $repository->checkDatabase();


    // Let's create a file object for this current script
    $file = new File();
    $file->filePath = __FILE__;
    $file->size = filesize(__FILE__);
    $file->fileName = basename(__FILE__);
    
    // Now we can use the repository we created earlier to save this file to the database.
    $repository->save($file);
    
    // Now note that this file has been assigned an id
    echo $file->id;


    // Here's a simple example
    $myFile = $repository->getByFilePath(__FILE__);
    
    // Let's print the result
    var_dump($myFile);


    // The 'find' query will return a list of all entities that match your query
    $files = $repository->findByFileName('index.php');
    
    /* You can also request only specific fields. You will then still get a File 
     * object but only the requested fields will be filled out.
     */
    $paths = $repository->findFilePathByFileName();
    
    /* The 'get' query if very much like the 'find' query except this query will 
     * only return the first match.
     */
    $myFile = $repository->getFileByFileSizeAndFileNameLike(1032, '%.php');
     
     /* Then finally there is the 'delete' query. This query does not accept the
      * selection fields. (The bit before the By keyword). It will delete all
      * matches of the query.
      * The return value of this query is the number of affected rows.
      */
     
     // Delete all files
     echo $repository->delete(); // Or $repository->deleteAll();
     
     // Delete specific files
     $repository->deleteById(235);


$repository->getIdAndFilePathAndFileSize();