Skip to content

API Model

Dory Azar edited this page Nov 15, 2026 · 2 revisions

Caligrafy\Model

Caligrafy Quill 2.26.0. Generated from the source by php qa/build-api-reference.php; the guides explain how to use these classes, this reference says exactly what each public method takes and returns. Back to the API reference.

Part of Data.

namespace Caligrafy;

class Model extends \stdClass

Source: framework/core/Model.php:20

Class Model is the base model from which the application models can be extended

Public properties

  • public $table
  • public static $strictRelations = null: Lazy loading of relations (2.26.0). A model the query builder read among several rows (Book::where(...)->get(), paginate(), chunk()) that runs a relation method (hasOne, hasMany, belongsTo, belongsToMany) would run one query per row (N+1): in development that throws [ERROR 2004]; in production (or with STRICT_RELATIONS=false) it logs a warning once per request and relation. $book->lazy()->author() allows one call, #[Caligrafy\Lazy] on the relation method allows it always. A model read alone (find(), first(), the instance find() and all()) is not guarded.

Methods

->__construct()

public function __construct($table = null)

No doc comment.

Parameters

  • $table, default null

Source: framework/core/Model.php:47

->__invoke()

public function __invoke($table = null, $dynamicArgs = array())

No doc comment.

Parameters

  • $table, default null
  • $dynamicArgs, default array()

Source: framework/core/Model.php:52

->associate()

public function associate($table)

Associates a model with a table in the database

Parameters

  • $table

Source: framework/core/Model.php:66

->create()

public function create($inputs)

Basic CRUD functions on the associated table

Parameters

  • $inputs

Source: framework/core/Model.php:77

->update()

public function update($inputs, $id)

No doc comment.

Parameters

  • $inputs
  • $id

Source: framework/core/Model.php:99

->delete()

public function delete($id)

No doc comment.

Parameters

  • $id

Source: framework/core/Model.php:117

->__call()

public function __call($method, $args)

$book->find($id) (instance: SELECT * FROM <its table> WHERE id = ?, as before) and $book->where(...) etc.

Parameters

  • $method
  • $args

Source: framework/core/Model.php:150

Model::__callStatic()

public static function __callStatic($method, $args)

Book::query(), Book::find($id), Book::where(...), Book::with(...), Book::paginate(20)...

Parameters

  • $method
  • $args

Source: framework/core/Model.php:166

->all()

public function all($args = array())

Fetches all the records, optionally completed by SQL clauses

Parameters

  • $args array, default array(): 'condition' (ex: 'WHERE price > ?'), 'order' (ex: 'ORDER BY title'), 'limit' (ex: 'LIMIT 10') and 'bindings' (array of values bound to the ? placeholders). The clauses are inserted as is: never concatenate user input in them, use ? placeholders and 'bindings' instead.

Source: framework/core/Model.php:198

->search()

public function search($scope, $keywords)

Search functionality that searched records in a table

Parameters

  • $scope string: defines the columns to search in the database separated by commas
  • $keywords string: defines the LIKE pattern to look for (% and _ are wildcards). It is bound, never inlined.

Returns array: $result results found in the database

Source: framework/core/Model.php:227

->save()

public function save()

Save functionality applied directly to the object that could update or create depending on whether the object has an id or not

Source: framework/core/Model.php:307

->hasOne()

public function hasOne(
    $modelName,
    $joinedTableName,
    $foreignKey = null,
    $localKey = null,
    $functionName = null
)

Defines the hasOne relationship

Parameters

  • $modelName string: defines the name of the model to join to
  • $joinedTableName string: defines the name of the table to join to
  • $foreignKey string, default null: defines the identifier key of the key indexing the joined table
  • $localKey string, default null: defines the identifier key of the joined table
  • $functionName, default null

Returns Model: object containing the records from the join

Source: framework/core/Model.php:324

->hasMany()

public function hasMany(
    $modelName,
    $joinedTableName,
    $foreignKey = null,
    $localKey = null,
    $functionName = null
)

Defines the hasMany relationship

Parameters

  • $modelName string: defines the name of the model to join to
  • $joinedTableName string: defines the name of the table to join to
  • $foreignKey string, default null: defines the identifier key of the key indexing the joined table
  • $localKey string, default null: defines the identifier key of the joined table
  • $functionName, default null

Returns Model: object containing the records from the join

Source: framework/core/Model.php:355

->belongsTo()

public function belongsTo($modelName, $joinedTableName, $foreignKey = null, $localKey = null)

No doc comment.

Parameters

  • $modelName
  • $joinedTableName
  • $foreignKey, default null
  • $localKey, default null

Source: framework/core/Model.php:378

->belongsToMany()

public function belongsToMany(
    $modelName,
    $joinedTableName,
    $foreignKeyLocal = null,
    $foreignKeyJoin = null,
    $functionName = null
)

No doc comment.

Parameters

  • $modelName
  • $joinedTableName
  • $foreignKeyLocal, default null
  • $foreignKeyJoin, default null
  • $functionName, default null

Source: framework/core/Model.php:385

->attach()

public function attach($joinedTableName, $id)

No doc comment.

Parameters

  • $joinedTableName
  • $id

Source: framework/core/Model.php:452

->detach()

public function detach($joinedTableName, $id)

No doc comment.

Parameters

  • $joinedTableName
  • $id

Source: framework/core/Model.php:458

->lazy()

public function lazy()

Allows the next relation call of this model to load lazily, once: $book->lazy()->author() (a relation read on purpose for one of several rows)

Returns $this

Source: framework/core/Model.php:499

Model::collected()

public static function collected(array $models, array $loaded = array())

Marks the models a query read together (Query::get() with more than one row): their relation methods are then guarded against lazy loading. Called by the query builder.

Parameters

  • $models array: the models of one result
  • $loaded array, default array(): the relations the query loaded with with()

Source: framework/core/Model.php:512

Model::strictRelations()

public static function strictRelations()

Whether a lazy relation call throws: false in production (APP_ENV=production) and with STRICT_RELATIONS=false, true otherwise. Decided once; Model::$strictRelations = true|false forces it, null decides again.

Source: framework/core/Model.php:526

Model::forgetLazyWarnings()

public static function forgetLazyWarnings()

Forgets the lazy loading warnings given so far (a new request: Query::forgetShapes() calls it)

Source: framework/core/Model.php:539

->arrayToModel()

public function arrayToModel($array)

Turns an array into a Model

Parameters

  • $array array: takes the array that needs to be converted to a model

Returns Model: object containing the modified object

Source: framework/core/Model.php:613

->toArray()

public function toArray()

Turns Model into an array

Returns array: representation of the model

Source: framework/core/Model.php:628

->tableExists()

public function tableExists($tablename)

Checks if a table exists

Parameters

  • $tablename

Returns boolean: of the result

Source: framework/core/Model.php:668

Caligrafy: Describe it. Approve it. Ship it. Your idea turned into a complete app: every feature built once, for web pages, Vue apps, a REST API and AI agents, and online.

Start here

Why Caligrafy: the benefits, the measurements, and what it is not yet

  1. Quickstart with Claude: describe it, approve it, ship it

    or the Quickstart: the same three steps by hand, in 5 minutes

  2. How Caligrafy works: the ideas in 5 minutes
  3. Your first capability: build a feature, page included

Capabilities

Experiences

Data and people

  1. Models
  2. Relationships
  3. Queries: filters, paging, related rows
  4. Sign-in, users and tokens

Built-in features

Put it online

Build with AI

Reference

Clone this wiki locally