PHP code example of thewunder / corma

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

    

thewunder / corma example snippets


namespace YourNamespace\Dataobjects;

use Corma\Relationship\ManyToMany;
use Corma\Relationship\OneToMany;
use Corma\Relationship\OneToOne;

class YourDataObject {
    protected $id;

    //If the property name == column name on the table your_data_objects it will be saved
    protected $myColumn;

    protected ?int $otherObjectId = null;
    
    #[OneToOne]
    protected ?OtherObject $otherObject = null;
    
    #[OneToMany(AnotherObject::class)]
    protected ?array $anotherObjects = null;
    
    #[ManyToMany(DifferentObject::class, 'your_data_object_different_link_table')]
    protected ?array $differentObjects = null;
    //Getters and setters..
}

namespace YourNamespace\Dataobjects\Repository;

class YourDataObjectRepository extends ObjectRepository {
    //Override default behavior and add custom methods...
}

$db = DriverManager::getConnection(...); //see Doctrine DBAL docs
$orm = ObjectMapper::withDefaults($db, $container); //uses any PSR-11 compatible DI container

$object = $orm->create(YourDataObject::class);
//Call setters...
$orm->save($object);
//Call more setters...
$orm->save($object);

//Call more setters on $object...
$objects = [$object];
$newObject = $orm->create(YourDataObject::class);
//call setters on $newObject...
$objects[] = $newObject;

$orm->saveAll($objects);

//find existing object by id
$existingObject = $orm->find(YourDataObject::class, 5);

//find existing objects with myColumn >= 42 AND otherColumn = 1
$existingObjects = $orm->findBy(YourDataObject::class, ['myColumn >='=>42, 'otherColumn'=>1], ['sortColumn'=>'ASC']);

//load relationships
$orm->load($existingObjects, 'otherObject');
$orm->load($existingObjects, 'anotherObjects');
$orm->load($existingObjects, 'differentObjects');

//delete those
$orm->deleteAll($existingObjects);