GithubHelp home page GithubHelp logo

nette-orm's Introduction

Nette ORM

GitHub branch checks state

install

Create ORM model

<?php 
/**
 * @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,
        ];
    }
}

Create ORM service

<?php

class ServiceEvent extends AbstractService {

    public function getNextEvents(): TypedTableSelection {
        return $this->getTable()->where('begin > NOW()');
    }
}

Register extension

orm:
    <table_name>:
        service: 'FQN of service'
        model: 'FQN of model'
    <another_table_name>:
        service: 'FQN of another service'
        model: 'FQN of another model'
extensions:
    orm: Fykosak\NetteORM\ORMExtension

Examples

TypedTableSelection is a regular selection you can use all methods like in nette DB Selection.

$query= $sericeEvent->getNextEvent();
$query->where('name','My cool event');

TypedTableSelection return ORM model instead of ActiveRow, but ORM model is a descendant of a ActiveRow.

$query= $sericeEvent->getNextEvent();
foreach($query as $event){
$event // event is a ModelEvent
}

$model = $sericeEvent->getNextEvent()->fetch(); // Model is a ModelEvent too.

Take care GroupedSelection still return ActiveRow, you can use static method createFromActiveRow

$query= $sericeEvent->getParticipants();
foreach($query as $row){
// $row is a ActiveRow
$participant = ModelParticipant::createFromActiveRow($row);
}

Define relations between Models by methods

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

Now you can use ReferencedAccessor to access Model

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.