PHP code example of schorts / firestore-dao

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

    

schorts / firestore-dao example snippets


use Schorts\FirestoreDAO\FirestoreDAO;
use Schorts\SharedKernel\DAO\DeleteMode;

final class UserDAO extends FirestoreDAO
{
  // Extend with domain-specific methods if needed
}

$userDao = new UserDAO($firestore->collection('users'), DeleteMode::SOFT);

// Create
$userDao->create($user);

// Find
$found = $userDao->findByID($user->getId()->getValue());

// Update
$userDao->update($user);

// Delete (soft delete by default)
$userDao->delete($user);

// Restore
$userDao->restore($user);

$firestore->runTransaction(function ($transaction) use ($userDao) {
  $uow = new \Schorts\FirestoreDAO\UnitOfWork\FirestoreTransactionUnitOfWork($transaction);

  $user = $userDao->findByID('123', $uow);
  $user->setName('Updated Name');

  $userDao->update($user, $uow);
});