Table Relationship
When you create a CRUD component on NexoPOS, you can establish a relationship with existing tables to pull data from them simultaneously
In a concrete example, if you create a CRUD for books, you might want to assign the author (in our case, the person who made the entry, not the book author) or assign it to a category. You can configure the various relationships from your CRUD class: Inner Join, Left Join, or Right Join. The impact on the display depends strictly on the type of relationship you choose.
Inner Join
In this example, as with any database inner join strategy, both resources must exist to provide a result. It might be convenient to show books that have an author or a category that actually exists.
<?php
namespace Modules\YourModule\Crud;
class BookCrud extends CrudService
{
// ....
public $relations = [
[ 'nexopos_users as user', 'mymodule_books.author', '=', 'user.id' ]
];
// ....
}
Here, we're defining an Inner Join relationship. We'll assign existing users to the book author. If the user doesn't exist (or has been deleted), the book will not be listed.
Left Join
Alternatively, you might want to give priority to either table. With left Join, the priority is given to the table set on the left part of the relationship. This means that if the entry on the right side doesn't exist, the value will be null, and there will be a result.
<?php
namespace Modules\YourModule\Crud;
class BookCrud extends CrudService
{
// ....
public $relations = [
'leftJoin' => [
[ 'nexopos_users as user', 'mymodule_books.author', '=', 'user.id' ]
]
];
// ....
}
With this approach, we have created a left join query on the user's table. If the users don't exist, we will still have an entry for the book. However, all the user information will be set to null.
Right Join
The Right Join method is the opposite of the Left Join. In our example, instead of giving priority to the books table, we'll now give priority to the user's table. This means that if the book is missing (or has been deleted), there will still be an entry.
<?php
namespace Modules\YourModule\Crud;
class BookCrud extends CrudService
{
// ....
public $relations = [
'rightJoin' => [
[ 'nexopos_users as user', 'mymodule_books.author', '=', 'user.id' ]
]
];
// ....
}