PHP code example of fykosak / nette-orm

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

    

fykosak / nette-orm example snippets


 
/**
 * @property-read string name
 * @property-read int event_id
 * @property-read \DateTimeInterface begin
 * @property-read \DateTimeInterface end
 * @note better typehinting for your IDE
 */
class ModelEvent extends AbstractModel {
    // you can define realtions
    public function getParticipants(): GroupedSelection {
        return $this->related('participant', 'event_id');
    }
    // you can define own metods
    public function __toArray(): array {
        return [
            'eventId' => $this->event_id,          
            'begin' => $this->begin ? $this->begin->format('c') : null,
            'end' => $this->end ? $this->end->format('c') : null,          
            'name' => $this->name,
        ];
    }
}



class ServiceEvent extends AbstractService {

    public function getNextEvents(): TypedTableSelection {
        return $this->getTable()->where('begin > NOW()');
    }
}
 
$query= $sericeEvent->getNextEvent();
$query->where('name','My cool event');
 
$query= $sericeEvent->getNextEvent();
foreach($query as $event){
$event // event is a ModelEvent
}

$model = $sericeEvent->getNextEvent()->fetch(); // Model is a ModelEvent too.
 
$query= $sericeEvent->getParticipants();
foreach($query as $row){
// $row is a ActiveRow
$participant = ModelParticipant::createFromActiveRow($row);
}

class ModelParticipant extends AbstractModel {
    // use ActiveRow to resolve relations and next create a Model.
    public function getEvent(): ModelEvent {
        return  ModelEvent::createFromActiveRow($this->event);
    }
}

$myModel // any model that has define single method returned ModelEvent
$modelEvent = ReferencedAccessor::accessModel($myModel,ModelEvent::class);