Skip to content
comhon-project edited this page Jun 18, 2020 · 16 revisions

Table of contents

  1. Preamble
  2. Dataset
  3. Simple requester
  4. Complex requester
    1. Intermediate request format
    2. Advanced request format
    3. Order, offset and limit
    4. Count

Preamble

Requester permit to retrieve any comhon object with a serialization set. It allow to build complex requests in unique Comhon! request format and it will transform requests in language understandable by your "serialization system" (for example SQL database).

First, the request must be built, and second, it must be executed. The execution will return a ComhonObject (for simple requests) or a ComhonArray (for complex requests).
Requests might be executed in public or private context. In public context, some settings might be not available to protect some private data or to prevent requests too complex with too long execution time.

If any error appears, for example a malformed request, a ComhonException will be thrown (All error codes are listed in class Comhon\Exception\ConstantException).

Dataset

To have a better comprehension we will define some manifest and serialized object

Person

Manifest

<root name="Test\Person" version="3.0">
    <properties>
        <property name="id" is_id="1" auto="incremental" __inheritance__="Comhon\Manifest\Property\Index"/>
        <property name="firstName" __inheritance__="Comhon\Manifest\Property\String"/>
        <property name="lastName" __inheritance__="Comhon\Manifest\Property\String"/>
        <property name="birthPlace" model="\Test\Place" is_foreign="1" __inheritance__="Comhon\Manifest\Property\Object"/>
        <property name="father" model="\Test\Person\Man" is_foreign="1" __inheritance__="Comhon\Manifest\Property\Object"/>
        <property name="mother" model="\Test\Person\Woman" is_foreign="1" __inheritance__="Comhon\Manifest\Property\Object"/>
        <property name="children" __inheritance__="Comhon\Manifest\Property\Aggregation">
            <values name="child" model="\Test\Person"/>
            <aggregations>
                <aggregation>mother</aggregation>
                <aggregation>father</aggregation>
            </aggregations>
        </property>
        <property name="houses" __inheritance__="Comhon\Manifest\Property\Aggregation">
            <values name="house" model="\Test\House"/>
            <aggregations>
                <aggregation>owner</aggregation>
            </aggregations>
        </property>
    </properties>
</root>

Serialization

<root name="Test\Person" version="3.0">
    <serialization>
        <foreign_settings id="person" __inheritance__="Comhon\SqlTable"/>
    </serialization>
    <properties>
        <firstName serialization_name="first_name"/>
        <lastName serialization_name="last_name"/>
        <birthPlace serialization_name="birth_place_id"/>
        <father serialization_name="father_id"/>
        <mother serialization_name="mother_id"/>
    </properties>
</root>

Serialized object

id first_name last_name birth_place_id father_id mother_id
1 john doe 1
2 jane doe 2
3 marie doe 3 1 2
4 philippe doe 3 1 2
5 emilie doe 2 1
6 walter doe 2 5
7 jesse doe 2 5

House

Manifest

<root name="Test\House" version="3.0">
    <properties>
        <property name="id" is_id="1" auto="incremental" __inheritance__="Comhon\Manifest\Property\Index"/>
        <property name="surface" __inheritance__="Comhon\Manifest\Property\Float"/>
        <property name="garden" __inheritance__="Comhon\Manifest\Property\Boolean"/>
        <property name="owner" model="\Test\Person" is_foreign="1" __inheritance__="Comhon\Manifest\Property\Object"/>
    </properties>
</root>

Serialization

<root name="Test\House" version="3.0">
    <serialization>
        <foreign_settings id="house" __inheritance__="Comhon\SqlTable"/>
    </serialization>
    <properties>
        <owner serialization_name="owner_id"/>
    </properties>
</root>

Serialized object

id surface garden owner_id
1 110 false 1
2 130 true 2
3 120 true 2

Place

Manifest

<root name="Test\Place" version="3.0">
    <properties>
        <property name="id" is_id="1" auto="incremental" __inheritance__="Comhon\Manifest\Property\Index"/>
        <property name="number" __inheritance__="Comhon\Manifest\Property\Integer"/>
        <property name="type" __inheritance__="Comhon\Manifest\Property\String"/>
        <property name="name" __inheritance__="Comhon\Manifest\Property\String"/>
        <property name="town" __inheritance__="Comhon\Manifest\Property\String"/>
    </properties>
</root>

Serialization

<root name="Test\Place" version="3.0">
    <serialization>
        <foreign_settings id="place" __inheritance__="Comhon\SqlTable"/>
    </serialization>
</root>

Serialized object

id number type name town
1 16 street main street New York
2 3 street second street New York
3 10 avenue Jean Moulin Paris

Simple requester

Simple requester permit to retrieve one object by its model name and its id. The entrypoint is Comhon\Request\SimpleRequester. It manage all types of serialization. You cannot use it if your model doesn't have id property.

$modelName = 'Test\Person';
$id = 1;
$filterProperties = null;
$isPrivate = true;

$requester = SimpleRequester::build($modelName, $id, $filterProperties, $isPrivate);
$result = $requester->execute(); // return ComhonObject or null if not found

If your model has several id properties you must specify them in a json encoded array, and order of values must be the same as order of id properties in manifest.

$modelName = 'Test\MyModel';
$id = '[1,"id2",456]';
$filterProperties = null;
$isPrivate = true;

$requester = SimpleRequester::build($modelName, $id, $filterProperties, $isPrivate);
$result = $request->execute(); // return ComhonObject or null if not found

Complex requester

Complex requester permit to retrieve, in same time, several objects that match with some given filters. Only objects with an SQL database serialization (Comhon\SqlTable) might be requested, and filter's models must be linked to the same SQL database.
The entrypoint to build and execute complex requests is Comhon\Request\ComplexRequester.

$requester = ComplexRequester::build($request, $isPrivate);
$result = $requester->execute(); // return ComhonArray

The first parameter might be a comhon object or an interfaced object (StdClass, array, DomNode, SimpleXMLElement). if an interfaced object is given, it will be imported in comhon object.
Model of comhon object request must be either Comhon\Request\Intermediate or Comhon\Request\Complex.

  • Requests manifests are defined here.
  • Logical filters manifests are defined here.
  • Model nodes manifests are defined here.

Intermediate request format

The intermediate request format correspond to model Comhon\Request\Intermediate.
You can see associated manifest here.

Use case 1

States

I want houses
-that have a surface supperior than 90 m“ or with a garden
-and that have an owner called marie or jane, and born in second street New York

Request

{
    "root": 1,
    "filter": 0,
    "models": [
        {
            "id": 0,
            "model": "Test\\Person"
        },
        {
            "id": 1,
            "model": "Test\\House"
        },
        {
            "id": 2,
            "model": "Test\\Place"
        }
    ],
    "simple_collection": [
        {
            "id": 0,
            "elements": [
                1,
                2,
                3,
                4
            ],
            "type": "conjunction",
            "__inheritance__": "Comhon\\Logic\\Simple\\Clause"
        },
        {
            "id": 1,
            "elements": [
                5,
                6
            ],
            "type": "disjunction",
            "__inheritance__": "Comhon\\Logic\\Simple\\Clause"
        },
        {
            "id": 2,
            "node": 0,
            "property": "firstname",
            "operator": "IN",
            "values": ["marie","jane"],
            "__inheritance__": "Comhon\\Logic\\Simple\\Literal\\Set\\String"
        },
        {
            "id": 3,
            "node": 2,
            "property": "name",
            "operator": "=",
            "value": "second street",
            "__inheritance__": "Comhon\\Logic\\Simple\\Literal\\String"
        },
        {
            "id": 4,
            "node": 2,
            "property": "town",
            "operator": "=",
            "value": "New York",
            "__inheritance__": "Comhon\\Logic\\Simple\\Literal\\String"
        },
        {
            "id": 5,
            "node": 1,
            "property": "surface",
            "operator": ">",
            "value": 90,
            "__inheritance__": "Comhon\\Logic\\Simple\\Literal\\Numeric\\Float"
        },
        {
            "id": 6,
            "node": 1,
            "property": "garden",
            "operator": "=",
            "value": true,
            "__inheritance__": "Comhon\\Logic\\Simple\\Literal\\Boolean"
        }
    ],
    "__inheritance__": "Comhon\\Request\\Intermediate"
}

Result

Stringified ComhonArray :

[
    {
        "id": 2,
        "surface": 130,
        "garden": true,
        "owner": 2
    },
    {
        "id": 3,
        "surface": 120,
        "garden": true,
        "owner": 2
    }
]

Explanations

In intermediate request you don't have to specify relations between differents models, the requester will find them automatically (relations between models permit to construct SQL joins between tables).

  • Your request must have at least :
    • root : The requested objects model. It is a reference to a model node in models list.
    • models : The list of all model nodes (requested model and models filter)
  • Your request may have following filter informations :
    • filter : The filter to apply. It is a reference to a literal or a clause in simple_collection.
    • simple_collection : The list of all literals and clauses
    • having_collection : The list of all having literals and having clauses (explain in next request)
Literal

A literal represent a condition in data base request, something like WHERE (foo = 'bar').
In intermediate request, literal must have following properties :

  • id: the literal identifier
  • node: a reference to a model node in models list
  • property: the property name
  • operator: =,<>,<,>,<=,>=, IN, NOT IN
  • value or values: the filter value
  • __inheritance__ : the formula's model name
Clause

A clause is a group of several literals and/or sub clauses, something like WHERE ... ((foo = 'bar') AND (bar = 'foo')).

  • id: the clause identifier
  • elements: list of references to existing literals or clauses
  • type: it determine if elements in clause are linked by AND or OR
    • disjonction : OR
    • conjonction : AND
  • __inheritance__ : the formula's model name
Logical Form

the logical form of previous request would be : (a ∨ b) ∧ c ∧ d ∧ e

Use case 2

States

I want persons
-named Paul or john
-and which have between 2 and 6 grandchildren

Request

{
    "root": 0,
    "filter": 0,
    "models": [
        {
            "id": 0,
            "model": "Test\\Person"
        }
    ],
    "simple_collection": [
        {
            "id": 0,
            "elements": [
                1,
                2
            ],
            "type": "conjunction",
            "__inheritance__": "Comhon\\Logic\\Simple\\Clause"
        },
        {
            "id": 1,
            "node": 0,
            "property": "firstName",
            "operator": "IN",
            "values": ["Paul","john"],
            "__inheritance__": "Comhon\\Logic\\Simple\\Literal\\Set\\String"
        },
        {
            "id": 2,
            "node": 0,
            "queue": ["children", "children"],
            "having": 0,
            "__inheritance__": "Comhon\\Logic\\Simple\\Having"
        }
    ],
    "having_collection": [
        {
            "id": 0,
            "elements": [
                1,
                2
            ],
            "type": "conjunction",
            "__inheritance__": "Comhon\\Logic\\Having\\Clause"
        },
        {
            "id": 1,
            "operator": ">",
            "value": 2,
            "__inheritance__": "Comhon\\Logic\\Having\\Literal\\Count"
        },
        {
            "id": 2,
            "operator": "<",
            "value": 6,
            "__inheritance__": "Comhon\\Logic\\Having\\Literal\\Count"
        }
    ],
    "__inheritance__": "Comhon\\Request\\Intermediate"
}

Result

Stringified ComhonArray :

[
    {
        "id": 1,
        "firstName": "john",
        "lastName": "doe",
        "birthPlace": "1"
    }
]

Explanations

Now let's explain literal having. A literal having is described by manifest here.
It contains having literal or having clause. Notice the difference between "literal having" that might be find in simple_collection and "having literal" or "having clause" that might be find in having_collection.

A literal having must have :

  • id: the literal identifier
  • node: a reference to a model node in models list
  • queue: properties stack that begin from node model
  • having: a reference to a having literal or a having clause in having_collection.
  • __inheritance__ : the formula's model name (one possible value : Comhon\Logic\Simple\Having)
Queue

A queue permit to know on which node you want to apply your having filter. The queue is a stack of properties name that begin from node model. In previous request example we start from the root model Test\Person and we apply having filter on children of children of first person, in other words on grandchildren.

Having clause

A having clause is like a simple clause but can only contain having literals and having clauses.

Having literal

A having literal represent a condition in data base request, something like HAVING ... COUNT(foo) = bar

There are two kind of having literal :

  • The count having literal, it must have following properties :
    • operator: =,<>,<,>,<=,>=
    • value: integer
  • The function having literals, it must have following properties :
    • function: the function to apply (SUM,AVG,MAX,MIN)
    • property: the property name
    • operator: =,<>,<,>,<=,>=
    • value: float

Limitations

  • Imagine we want to add a property address to model Test\House and this property would have type Test\Place (same type as property birthPlace in model Test\Person). Now we want a person born in certain place or a person that have a house in certain place. But this request case is impossible to resolve in intermediate request because we are not able to determine on which property apply the literal (literal only describe model). Fortunately we can manage this problem with advanced request.
  • sql request building via Intermadiate request can take more time than via advanced request. However that can be negligible compared sql query execution time. It depend on your database size and complexity.

Advanced request format

In advanced objects request you must define relations between properties linked to a literal. These relations are display in a graph (actually in a tree) which each node is a property except root node that is your requested model object. And each literal refer to a node of this graph.

Use case 1

States

I want persons
-that have a grandson named walter
-and that have a house without garden

Request

{
    "tree" : {
        "model"   : "Test\\Person",
        "id"      : "p1",
        "children" : [
            {
                "property" : "houses",
                "id"       : "houses"
            },
            {
                "property" : "children",
                "id"       : "p2",
                "children"  : [
                    {
                        "property" : "children",
                        "id"       : "p3"
                    }
                ]
            }
        ]
    },
    "filter" : {
        "type" : "conjunction",
        "elements" : [
            {
                "node"     : "p3",
                "property" : "firstName",
                "operator" : "=",
                "value"    : "walter"
            },
            {
                "node"     : "houses",
                "property" : "garden",
                "operator" : "=",
                "value"    : false
            }
        ]
    }
}

Explanations

The tree
visualization

To have a better compehension here is the tree in more visual way :

                  person (p1)
           ___________|__________
          |                      |
    children (p2)          houses (houses)
          |
    children (p3)
node

Each node tree has :

  • a property property or model
    • root node contain model that is the model name of requested objects.
    • other nodes contain property that is a property name of the model of the parent node.
  • a property id that identify current node (must be unique).
  • a property children (optional) that contain children properties of current model node

Note that in given example, you must differentiate property children of a node and the property name children of the model Test\Person.

Literal

As we can see in request, each node in tree has an id. Literals refer to this node id instead of model name like it's done in intermediate objects request. This permit to know exactly on which node we want to apply literal.

Order, offset and limit

  • We can order returned objects. Order can be applied on one or several properties of requested model. We can order in ascendant or descendant way (ASC or DESC).
  • We can apply a limit of returned objects
  • We can apply an offset to return objects from a certain range

To apply limit or offset we must specify order too. Actually if order is not specified, two same objects requests can return objects in different order, so returned objects may not be the same with a limit.

Example : I want five persons maximum, from third person, order by their firstname

{
    "model" : "Test\\Person",
    "limit" : 5,
    "offset" : 3,
    "order" : [{"property":"firstName", "type":"ASC"}]
}

Count

Instead of retrieve objects, it is possible to count objects that match built request. count function return the global count of object and ignore limit and offset even if they are set on built request. This function is usefull for pagination.

$request = ComplexLoadRequest::buildObjectLoadRequest($params);
$result = $request->count(); // return integer

It is possible to call count and execute on same built request.

$request = ComplexLoadRequest::buildObjectLoadRequest($params);
$collection = $request->execute(); // return ComhonArray
$count = $request->count(); // return integer

Clone this wiki locally