Relations

Overview #

In database, we have learnt about the relationship between entities, the primary key and foreign key. Here, in the same manner, we have relations between Resources. In each Resource, we can define its relations to other Resources.

There are two basic types of relationship: BelongsTo and HasMany.

BelongsTo #

The BelongsTo relationship is when a Resource belongs to another resource. This Resource has foreign key referred to primary key of the other resource. For example, the Order resource belongs to Customer resource. The Order resource has customerNumber referred to customerNumber primary key of Customer resource.

Example #

<?php
use \koolreport\dashboard\admin\Resource;
use \koolreport\dashboard\admin\relations\BelongsTo;

class Order extends Resource
{
    protected function relations()
    {
        return [
            BelongsTo::resource(Customer::class)
                ->link([
                    "customerNumber"=>"customerNumber"
                ]),
        ];
    }
}

As you see above, the Order resource is defined that it belongs to Customer class and link between them is from orders.customerNumber to the customers.customerNumber.

Properties #

Nametypedefaultdescription
linkarrayAn associate array containing linked keys from this resource to the other
titlestringSet title of relation
showOnDetailbooltrueGet/set whether table of related sources will be shown on detail screen

Example:

BelongsTo::resource(Customer::class)
    ->link([
        "customerNumber"=>"customerNumber"
    ])
    ->title("Belong to Customer")
    ->showOnDetail(true)

HasMany #

The HasMany relationship is when a Resource has many another resource. The other resource has foreign key referred to primary key of this Resource. For example, Customer has many Order. The Order resource has customerNumber referred to customerNumber primary key of Customer resource.

Example #

<?php
use \koolreport\dashboard\admin\Resource;
use \koolreport\dashboard\admin\relations\HasMany;

class Customer extends Resource
{
    protected function relations()
    {
        return [
            HasMany::resource(Order::class)
                ->link([
                    "customerNumber"=>"customerNumber"
                ]),
        ];
    }
}

As you see above, the Customer resource "has many" Order resource and link between them is from customers.customerNumber to orders.customerNumber.

Properties #

Nametypedefaultdescription
viaTablestring/arraySet in-between table or list of in-between tables
linkarrayAn associate array containing linked keys from this resource to the other
titlestringSet title of relation
showOnDetailbooltrueGet/set whether table of related sources will be shown on detail screen

Example:

HasMany::resource(Order::class)
    ->link([
        "customerNumber"=>"customerNumber"
    ])
    ->title("Has orders")
    ->showOnDetail(true),

viaTable #

Let say the Customer resource has many Order, the Order has many OrderDetail, OrderDetail has many Product and you want to specify the relationship between Customer and Product, you do:

class Customer extends Resource
{
    protected function relations()
    {
        return [
            HasMany::resource(Order::class)
                ->link(["customerNumber"=>"customerNumber"]),

            HasMany::resource(OrderDetail::class)
                ->viaTable("orders")
                ->link([
                    "customerNumber"=>"customerNumber",
                    "orderNumber"=>"orderNumber"
                ]),

            HasMany::resource(Product::class)
                ->viaTable(["orders","orderdetails"])
                ->link([
                    "customerNumber"=>"customerNumber",     // link customers -> orders
                    "orderNumber"=>"orderNumber",           // link orders -> orderdetails
                    "productCode"=>"productCode"            // link orderdetails -> products
                ]),
        ]
    }
}

Get started with KoolReport

KoolReport will help you to construct good php data report by gathering your data from multiple sources, transforming them into valuable insights, and finally visualizing them in stunning charts and graphs.