DB.PHP - PHP ORM

Set enumeration

Enumeration is additional attribute to relation. As without it regular relation relates one object to one other object. With enumerated relation many objects can be related to one object.

Let us extend regular relation example. Imagine we have product class and we want to put products in cart.

class product
{
    public $id;
    public $title;
}

And we have a cart class:

class cart
{
    public $id;
    /**
    * @var product
    */
    public $product;
}

This is regular relation and in this case we have $cart->product->title for example. But as we add enum as product property attribute:

class cart
{
    public $id;
    /**
    * enum
    * @var product
    */
    public $product;
}

Than we will have:

$cart->product[1]->title;

Where key of an enumerated array represents id of related object (id of product).

And how it is saved in table?

Products table:

shop_product
----------------------------
id | title
----------------------------
1  | Apple
2  | Banana

Carts table:

shop_cart
----------------------------
id | product
----------------------------
1  | |1|2|

By default enumerated property objects are loaded with single query on object load but if you dont want to waste additional resources on object load you can use lazy keyword.

class cart
{
    public $id;
    /**
    * lazy
    * enum
    * @var product
    */
    public $product;
}

As a result values of array $cart->product will represents id's of related objects:

echo $cart->product[2];

Will output 2

Enumerated field is always char type. You can set only length for that property field. Enumerated property values are separated with symbol |. Typical enumerated value in table looks like: |23|2|45|1| or if enumerated property is related to class having char as primary property field type then: |EUR|USD|GEL|.

In common cases you can query this field with like:cart.product like '%|24|%'.