www.hubleto.eu Reddit User Guide Stack Overflow LinkedIn Report an issue

Understanding columns in a model

A model describes each column once, in describeColumns(). From this single description Hubleto creates the table in the database, the input in the form, the column in the table and the validation.

A column is an object

apps/Deals/Models/Deal.php (excerpt)
      'identifier' => (new Varchar($this, $this->translate('Deal Identifier')))->setCssClass('badge badge-info')->setDefaultVisible(),
      'title' => (new Varchar($this, $this->translate('Title')))->setRequired()->setDefaultVisible()->setCssClass('font-bold'),
      'id_customer' => (new Lookup($this, $this->translate('Customer'), Customer::class))->setDefaultValue($this->router()->urlParamAsInteger('idCustomer')),
      'id_contact' => (new Lookup($this, $this->translate('Contact'), Contact::class)),
      'id_lead' => (new Lookup($this, $this->translate('Lead'), Lead::class))->setReadonly(),
      'version' => (new Integer($this, $this->translate('Version'))),
      // 'price' => (new Decimal($this, $this->translate('Price')))->setDecimals(2),
      'price_excl_vat' => new Decimal($this, $this->translate('Price excl. VAT')),
      'price_incl_vat' => new Decimal($this, $this->translate('Price incl. VAT')),
      'id_currency' => (new Lookup($this, $this->translate('Currency'), Currency::class))->setFkOnUpdate('RESTRICT')->setFkOnDelete('SET NULL')->setReadonly(),
      'date_expected_close' => (new Date($this, $this->translate('Expected close date'))),
      'id_owner' => (new Lookup($this, $this->translate('Owner'), User::class))->setReactComponent('InputUserSelect')->setDefaultValue($this->authProvider()->getUserId()),

The constructor takes the model and the title (always translated). Every setter returns the column, so setters are chained.

Column types

Class Database type Default input
PrimaryKey int auto increment hidden
Varchar varchar text field
Text text text area
Integer int number (or enum)
Decimal decimal number
Currency decimal amount
Boolean tinyint checkbox
Date, DateTime, Time, Year date/time types date and time pickers
Lookup int + foreign key dropdown
Email varchar e-mail field
Password varchar password field
Color varchar color picker
Image, File varchar upload
Json json JSON editor
Virtual none (SQL expression) read-only

Column types of Hubleto\Framework\Db\Column.

The most used setters

Setter Effect
setRequired() The input cannot be empty.
setReadonly() The user cannot edit the value.
setDefaultValue($v) Value of a new record.
setDefaultVisible() Visible in the table without the user turning it on.
setEnumValues([...]), setEnumCssClasses([...]) A fixed list of values (stored as numbers) and their colors.
setReactComponent('InputXxx') Use a different input component.
setHint(), setPlaceholder(), setUnit(), setIcon() Texts around the input.
setCssClass(), setTableCssClass() CSS classes of the input and of the table cell.
setProperty('sql', '...') SQL of a Virtual column.

Setters.

Enumerations

apps/Deals/Models/Deal.php (excerpt)
      'deal_result' => (new Integer($this, $this->translate('Deal Result')))
        ->setEnumValues(array_map(fn($v) => $this->translate($v), self::ENUM_DEAL_RESULTS))
        ->setEnumCssClasses([
          self::RESULT_UNKNOWN => 'bg-yellow-100 text-yellow-800',
          self::RESULT_WON => 'bg-green-100 text-green-800',
          self::RESULT_LOST => 'bg-red-100 text-red-800',
        ])
        ->setDefaultValue(self::RESULT_UNKNOWN)
      ,

Lookups and relations

A Lookup column is a foreign key to another model and its name starts with id_. Its second parameter is the class of the other model. Declare the matching relation in $relations:

apps/Deals/Models/Deal.php (excerpt)
      'id_customer' => (new Lookup($this, $this->translate('Customer'), Customer::class))->setDefaultValue($this->router()->urlParamAsInteger('idCustomer')),

Virtual columns

A virtual column has no database column. It is calculated by SQL in the query:

apps/Deals/Models/Deal.php (excerpt)
      'virt_next_activity_date' => (new Virtual($this, $this->translate('Next activity')))->setDefaultVisible()
        ->setProperty('sql', "
          select `a`.`date_start`
          from `deal_activities` `a`
          where
            `a`.`completed` = 0
            and `a`.`id_deal` = `deals`.`id`
            and `a`.`date_start` >= date(now())
          order by
            `a`.`date_start` asc
          limit 1
        "),

Always merge with the parent

public function describeColumns(): array
{
  return array_merge([
    'name' => (new Varchar($this, $this->translate('Name')))->setRequired(),
  ], parent::describeColumns());
}

parent::describeColumns() adds the primary key id and the custom columns of the model (next page).

Related pages