Skip to content

Entities

Jason Napolitano edited this page Jan 10, 2023 · 37 revisions

DatabaseFactory uses the entity system to model and modify data. An entity is an object representation of a database table within the database you're working with. When working with entities, we are using these objects to model, store, or modify table information based on that objects properties.

Create a new entity

// this class must extend the base entity class
class User extends DatabaseFactory\Entity 
{
    // all fields you want to work with are properties 
    // within this class
    public string $name; 
    public string $email; 
}

Table Assignment

When using entities (EG: while using the ORM), there is a background process that runs which attempts to intelligently guess the name of the table that we're working with. For example (and on all subsequent examples) we are working with the User entity class. The internal entity system will assume that this entity belongs to the users table and attempt to work on that table, if it exists. If you'd like to work on a different table and treat the entity as more of a model, you can simply assign the $table property within your entity class.

class User extends DatabaseFactory\Entity 
{
    // to use a different table than the auto-detected
    // table name, we assign it a new value here
    protected string $table = 'custom_table';

    // ...
}

Now that we have an entity class, we can modify our data, and use the entity to retrieve data while using the ORM. It's important to note, that in order to use the Query Builder, entities are not required.

Clone this wiki locally