| Methods |
public
|
__construct(Persistence $persistence = null, array<string, mixed> $defaults = [])
Creation of the new model can be done in two ways:.
Creation of the new model can be done in two ways:.
$m = new Model($db);
or
$m = new Model();
$m->setPersistence($db);
The second use actually calls add() but is preferred usage because:
- it's shorter
- type hinting will work;
- you can specify string for a table
Overriden by
|
#
|
public
|
isEntity(): bool
|
#
|
public
|
assertIsModel(self $expectedModelInstance = null): void
|
#
|
public
|
assertIsEntity(self $expectedModelInstance = null): void
|
#
|
public
|
getModel(bool $allowOnModel = false): static
|
#
|
public
|
__clone()
|
#
|
protected
|
getModelOnlyProperties(): array<string, true>
|
#
|
public
|
createEntity(): static
|
#
|
protected
|
init(): void
Extend this method to define fields of your choice.
Extend this method to define fields of your choice.
Overriden by
|
#
|
public
|
assertIsInitialized(): void
|
#
|
private
|
initEntityIdAndAssertUnchanged(): void
|
#
|
private
|
initEntityIdHooks(): void
|
#
|
public
|
add(Field|Reference|Join $obj, array<string, mixed> $defaults = []): void
|
#
|
public
|
_addIntoCollection(string $name, object $item, string $collection): object
|
#
|
public
&
|
getDataRef(): array<string, mixed>
|
#
|
public
&
|
getDirtyRef(): array<string, mixed>
|
#
|
public
|
validate(string $intent = null): array<string, string>
Perform validation on a currently loaded values, must return Array in format:
['field' => 'must be 4 digits exactly']…
Perform validation on a currently loaded values, must return Array in format:
['field' => 'must be 4 digits exactly'] or empty array if no errors were present.
You may also use format:
['field' => ['must not have character [ch]', 'ch' => $badCharacter]] for better localization of error message.
Always use
return array_merge(parent::validate($intent), $errors);
Parameters
| $intent |
by default only 'save' is used (from beforeSave) but you can use other intents yourself
|
Returns
|
#
|
protected
|
fieldFactory(array<mixed> $seed = []): Field
Given a field seed, return a field object.
Given a field seed, return a field object.
|
#
|
public
|
addField(string $name, array<mixed>|object $seed = []): Field
Adds new field into model.
Adds new field into model.
Overriden by
|
#
|
public
|
addFields(array<string, array<mixed>|object>|array<int, string> $fields, array<mixed> $seed = []): $this
Adds multiple fields into model.
Adds multiple fields into model.
|
#
|
public
|
removeField(string $name): $this
Remove field that was added previously.
Remove field that was added previously.
|
#
|
public
|
hasField(string $name): bool
|
#
|
public
|
getField(string $name): Field
|
#
|
public
|
setOnlyFields(array<int, string>|null $fields): $this
Sets which fields we will select.
Sets which fields we will select.
|
#
|
private
|
assertOnlyField(string $field): void
|
#
|
public
|
isDirty(string $field): bool
Will return true if specified field is dirty.
Will return true if specified field is dirty.
|
#
|
public
|
getFields(string|array<int, string>|null $filter = null): array<string, Field>
|
#
|
public
|
set(string $field, mixed $value): $this
Set field value.
|
#
|
public
|
setNull(string $field): $this
Unset field value even if null value is not allowed.
Unset field value even if null value is not allowed.
|
#
|
public
|
setMulti(array<string, mixed> $fields): $this
Helper method to call self::set() for each input array element.
Helper method to call self::set() for each input array element.
This method does not revert the data when an exception is thrown.
|
#
|
public
|
get(string $field = null): ($field is null ? array<string, mixed> : mixed)
Returns field value.
If no field is passed, then returns array of all field values.
Returns field value.
If no field is passed, then returns array of all field values.
|
#
|
private
|
assertHasIdField(): void
|
#
|
public
|
getId(): mixed
|
#
|
public
|
setId(mixed $value, bool $allowNull = true): $this
|
#
|
public
|
getModelCaption(): string
Return (possibly localized) $model->caption.
If caption is not set, then generate it from model class name.
Return (possibly localized) $model->caption.
If caption is not set, then generate it from model class name.
|
#
|
public
|
getTitle(): mixed
Return value of $model->get($model->titleField). If not set, returns id value.
Return value of $model->get($model->titleField). If not set, returns id value.
|
#
|
public
|
getTitles(): array<int|string, mixed>
Returns array of model record titles [id => title].
Returns array of model record titles [id => title].
|
#
|
public
|
compare(string $name, mixed $value): bool
|
#
|
public
|
_isset(string $name): bool
Does field exist?
|
#
|
public
|
_unset(string $name): $this
Remove current field value and use default.
Remove current field value and use default.
|
#
|
public
|
scope(): RootScope
Get the scope object of the Model.
Get the scope object of the Model.
|
#
|
public
|
addCondition(
AbstractScope|array<int, AbstractScope|Expressionable|array{(string|Expressionable), 1?: mixed, 2?: mixed}>|string|Expressionable $field,
($field is string|Expressionable ? ($value is null ? mixed : string) : never) $operator = null,
($operator is string ? mixed : never) $value = null,
): $this
Narrow down data-set of the current model by applying
additional condition. There is no way to remove
condition once…
Narrow down data-set of the current model by applying
additional condition. There is no way to remove
condition once added, so if you need - clone model.
This is the most basic for defining condition:
->addCondition('my_field', $value);
This condition will work across all persistence drivers universally.
In some cases a more complex logic can be used:
->addCondition('my_field', '>', $value);
->addCondition('my_field', '!=', $value);
->addCondition('my_field', 'in', [$value1, $value2]);
Second argument could be '=', '>', '<', '>=', '<=', '!=', 'in', 'like' or 'regexp'.
Those conditions are still supported by most of persistence drivers.
There are also vendor-specific expression support:
->addCondition('my_field', $expr);
->addCondition($expr);
Conditions on referenced models are also supported:
$contact->addCondition('company/country', 'US');
where 'company' is the name of the reference
This will limit scope of $contact model to contacts whose company country is set to 'US'
Using # in conditions on referenced model will apply the condition on the number of records:
$contact->addCondition('tickets/#', '>', 5);
This will limit scope of $contact model to contacts that have more than 5 tickets
To use those, you should consult with documentation of your
persistence driver.
|
#
|
public
|
addCteModel(string $name, self $model, bool $recursive = false): $this
Adds WITH/CTE model.
|
#
|
public
|
setOrder(
string|array<int, string|array{string, 1?: ('asc'|'desc')}>|array<string, 'asc'|'desc'> $field,
($field is array ? never : 'asc'|'desc') $direction = 'asc',
): $this
Set order for model records. Multiple calls are allowed.
Set order for model records. Multiple calls are allowed.
|
#
|
public
|
setLimit(int $count = null, int $offset = 0): $this
Set limit of DataSet.
|
#
|
public
|
issetPersistence(): bool
|
#
|
public
|
getPersistence(): Persistence
|
#
|
public
|
setPersistence(Persistence $persistence): $this
|
#
|
public
|
assertHasPersistence(string $methodName = null): void
|
#
|
public
|
isLoaded(): bool
Is entity loaded?
|
#
|
public
|
assertIsLoaded(): void
|
#
|
public
|
unload(): $this
|
#
|
private
|
remapIdLoadToPersistence(mixed $id): mixed
|
#
|
private
|
_load(
($fromTryLoad is true ? false : bool) $fromReload,
bool $fromTryLoad,
mixed $id,
): ($fromTryLoad is true ? static|null : static)
|
#
|
public
|
tryLoad(mixed $id): static|null
Try to load record. Will not throw an exception if record does not exist.
Try to load record. Will not throw an exception if record does not exist.
|
#
|
public
|
load(mixed $id): static
Load one record by an ID.
Load one record by an ID.
|
#
|
public
|
tryLoadOne(): static|null
Try to load one record. Will throw if more than one record exists, but not if there is no record.
Try to load one record. Will throw if more than one record exists, but not if there is no record.
|
#
|
public
|
loadOne(): static
Load one record. Will throw if more than one record exists.
Load one record. Will throw if more than one record exists.
|
#
|
public
|
tryLoadAny(): static|null
Try to load any record. Will not throw an exception if record does not exist.
Try to load any record. Will not throw an exception if record does not exist.
If only one record should match, use checked "tryLoadOne" method.
|
#
|
public
|
loadAny(): static
Load any record.
Load any record.
If only one record should match, use checked "loadOne" method.
|
#
|
public
|
reload(): $this
Reload model by taking its current ID.
Reload model by taking its current ID.
|
#
|
public
|
duplicate(): static
Keeps the model data, but wipes out the ID so
when you save it next time, it ends up as a new
record in the database.
Keeps the model data, but wipes out the ID so
when you save it next time, it ends up as a new
record in the database.
|
#
|
public
|
saveAndUnload(array<string, mixed> $data = []): $this
Store the data into database, but will never attempt to
reload the data. Additionally any data will be unloaded.
Use…
Store the data into database, but will never attempt to
reload the data. Additionally any data will be unloaded.
Use this instead of save() if you want to squeeze a
little more performance out.
|
#
|
public
|
withPersistence(Persistence $persistence): static
Create new model from the same base class as $this.
|
#
|
private
|
temporaryMutateScopeFieldsBackup(): array<string, array{bool, mixed}>
TODO https://github.com/atk4/data/issues/662.
|
#
|
private
|
temporaryMutateScopeFieldsRestore(array<string, array{bool, mixed}> $backup): void
|
#
|
private
|
_loadBy(
bool $fromTryLoad,
AbstractScope|array<int, AbstractScope|Expressionable|array{(string|Expressionable), 1?: mixed, 2?: mixed}>|string|Expressionable $field,
($field is string|Expressionable ? ($value is null ? mixed : string) : never) $operator = null,
($operator is string ? mixed : never) $value = null,
): ($fromTryLoad is true ? static|null : static)
|
#
|
public
|
loadBy(
AbstractScope|array<int, AbstractScope|Expressionable|array{(string|Expressionable), 1?: mixed, 2?: mixed}>|string|Expressionable $field,
($field is string|Expressionable ? ($value is null ? mixed : string) : never) $operator = null,
($operator is string ? mixed : never) $value = null,
): static
Load one record by additional condition. Will throw if more than one record exists.
Load one record by additional condition. Will throw if more than one record exists.
|
#
|
public
|
tryLoadBy(
AbstractScope|array<int, AbstractScope|Expressionable|array{(string|Expressionable), 1?: mixed, 2?: mixed}>|string|Expressionable $field,
($field is string|Expressionable ? ($value is null ? mixed : string) : never) $operator = null,
($operator is string ? mixed : never) $value = null,
): static|null
Try to load one record by additional condition. Will throw if more than one record exists, but not if there is no…
Try to load one record by additional condition. Will throw if more than one record exists, but not if there is no record.
|
#
|
protected
|
validateEntityScope(): void
|
#
|
private
|
assertIsWriteable(): void
|
#
|
public
|
save(array<string, mixed> $data = []): $this
Save record.
|
#
|
protected
|
_insert(array<string, mixed> $row): void
|
#
|
public
|
insert(array<string, mixed> $row): mixed
|
#
|
public
|
import(array<int, array<string, mixed>> $rows): $this
|
#
|
public
|
export(
array<int, string>|null $fields = null,
string $keyField = null,
bool $typecast = true,
): ($keyField is string ? array<mixed, array<string, mixed>> : array<int, array<string, mixed>>)
Export DataSet as array of hashes.
Export DataSet as array of hashes.
Parameters
| $fields |
Names of fields to export
|
| $keyField |
Optional name of field which value we will use as array key
|
| $typecast |
Should we typecast exported data
|
|
#
|
final
public
|
getIterator(): Traversable<static>
Create iterator (yield values).
Create iterator (yield values).
You can return false in afterLoad hook to prevent to yield this data row, example:
$model->onHook(self::HOOK_AFTER_LOAD, static function (Model $m) {
if ($m->get('date') < $m->dateFrom) {
$m->breakHook(false);
}
})
You can also use breakHook() with specific object which will then be returned
as a next iterator value.
Implements
|
#
|
public
|
createIteratorBy(
AbstractScope|array<int, AbstractScope|Expressionable|array{(string|Expressionable), 1?: mixed, 2?: mixed}>|string|Expressionable $field,
($field is string|Expressionable ? ($value is null ? mixed : string) : never) $operator = null,
($operator is string ? mixed : never) $value = null,
): Traversable<static>
Create iterator (yield values) by additional condition.
Create iterator (yield values) by additional condition.
|
#
|
public
|
delete(mixed $id = null): static
Delete record with a specified id. If no ID is specified
then current record is deleted.
Delete record with a specified id. If no ID is specified
then current record is deleted.
|
#
|
public
|
atomic<T>(Closure(): T $fx): T
Atomic executes operations within one begin/end transaction, so if
the code inside callback will fail, then all of the…
Atomic executes operations within one begin/end transaction, so if
the code inside callback will fail, then all of the transaction
will be also rolled back.
|
#
|
public
|
action(string $mode, array<mixed> $args = []): Query
Create persistence action.
Create persistence action.
TODO Rename this method to stress this method should not be used
for anything else then reading records as insert/update/delete hooks
will not be called.
Overriden by
|
#
|
public
|
executeCountQuery(): int
|
#
|
public
|
addExpression(string $name, array{expr: mixed} $seed): CallbackField|SqlExpressionField
Add expression field.
|
#
|
public
|
addCalculatedField<T is self>(string $name, array{expr: (Closure(T): mixed)} $seed): CallbackField
Add expression field which will calculate its value by using callback.
Add expression field which will calculate its value by using callback.
|
#
|
public
|
__isset(string $name): bool
|
#
|
public
&
|
__get(string $name): mixed
|
#
|
public
|
__set(string $name, mixed $value): void
|
#
|
public
|
__unset(string $name): void
|
#
|
public
|
__debugInfo(): array<string, mixed>
|
#
|