campi e metodi di model Un campo è un dato semplice sul valore, letto con un punto e senza parentesi: $value.name. Un metodo richiede argomenti o esegue un'operazione, e si chiama con ->: $value->name(arguments), oppure passando il valore con la pipe: $value |> name(arguments).
Da KScript.Data Richiedono <import package="KScript.Data" />import KScript.Data . Vedi KScript.Data .
Ogni record (esclusi quelli eliminati in modo logico).
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->all()->map(fn(t) => $t.title) }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo Task->all()->map(fn(t) => $t.title)# model ->average (column: Self.fields ) → number KScript.Data La media di una colonna, oppure null quando nessuna riga corrisponde.
Parametro Accetta column obbligatorio Self.fields
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->average(points)->round(2) }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo Task->average(points)->round(2)# model ->chunk (size: number , work: fn ) KScript.Data Legge le righe un blocco alla volta e chiama work con ciascun blocco (e il suo numero, a partire da 0): utile per scorrere più righe di quante ne entrino in memoria. Restituisci false da work per interrompere.
Parametro Accetta size required number work obbligatorio fn
Gli argomenti possono essere denominati: ->chunk(…, work: …).
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<fn name="show" params="batch: list, n: int">
<echo>{ $n }: { $batch->map(fn(t) => $t.title)->join(', ') }</echo>
</fn>
<do>Task->order_by(id)->chunk(2, @show)</do>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
fn show(batch: list, n: int) {
echo "{ $n }: { $batch->map(fn(t) => $t.title)->join(', ') }"
}
Task->order_by(id)->chunk(2, @show)Quante righe corrispondono.
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->count() } tasks, { Task->where(done: false)->count() } to do</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo "{ Task->count() } tasks, { Task->where(done: false)->count() } to do"# model ->create (values...: Self ) → Self KScript.Data Controlla i valori rispetto al modello, salva un nuovo record e lo restituisce: User->create({ name: 'Joan', email: 'joan@example.com' }) oppure User->create(name: 'Joan', ...). I campi protetti vengono esclusi.
Parametro Accetta valuesSelf , quanti ne vuoi
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Note" fields="text: string, pinned: bool = false" />
<do>$db->create_table(Note)</do>
<let name="note" value="Note->create(text: 'Buy milk')" />
<echo>{ $note.id }: { $note.text }, pinned: { $note.pinned }</echo>.kscript XML Copia
import KScript.Data
model Note {
text: string
pinned: bool = false
}
$db->create_table(Note)
$note = Note->create(text: 'Buy milk')
echo "{ $note.id }: { $note.text }, pinned: { $note.pinned }"# model ->cursor_paginate (per_page?: number , after? , key?: string , descending?: bool ) → map KScript.Data Legge una pagina usando una chiave unica e non nulla (per impostazione predefinita la chiave del modello). Restituisce { items, has_more, next }; passa next come after per continuare.
Parametro Accetta per_page facoltativo number after opzionale qualsiasi valore key facoltativo string descending facoltativo un flag: descending oppure not descending
Gli argomenti possono essere con nome: ->cursor_paginate(…, after: …, key: …).
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<let name="page" value="Task->cursor_paginate(2)" />
<echo>{ $page.items->map(fn(t) => $t.title) }, more: { $page.has_more }</echo>
<let name="rest" value="Task->cursor_paginate(2, after: $page.next)" />
<echo>{ $rest.items->map(fn(t) => $t.title) }, more: { $rest.has_more }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
$page = Task->cursor_paginate(2)
echo "{ $page.items->map(fn(t) => $t.title) }, more: { $page.has_more }"
$rest = Task->cursor_paginate(2, after: $page.next)
echo "{ $rest.items->map(fn(t) => $t.title) }, more: { $rest.has_more }"# model ->decrement (column: Self.fields , by?: number ) → number KScript.Data Sottrae da una colonna numerica in ogni riga che corrisponde (1 per impostazione predefinita). Restituisce quante righe sono cambiate.
Parametro Accetta column obbligatorio Self.fields by facoltativo number
Gli argomenti possono essere con nome: ->decrement(…, by: …).
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<do>Task->where(title: 'Ship it')->decrement(points)</do>
<echo>{ Task->where(title: 'Ship it')->value(points) }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
Task->where(title: 'Ship it')->decrement(points)
echo Task->where(title: 'Ship it')->value(points)Elimina ogni riga corrispondente: i modelli che estendono SoftDeletes vengono solo contrassegnati come eliminati. Restituisce quante.
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->where(done: true)->delete() } deleted</echo>
<echo>{ Task->count() } left, { Task->with_trashed()->count() } kept to restore</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo "{ Task->where(done: true)->delete() } deleted"
echo "{ Task->count() } left, { Task->with_trashed()->count() } kept to restore"Esclude le righe che ripetono una precedente, nelle colonne selezionate.
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->select(done)->distinct()->count() }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo Task->select(done)->distinct()->count()Vero quando almeno una riga corrisponde.
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->where(done: true)->exists() } { Task->where(points: 99)->exists() }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo "{ Task->where(done: true)->exists() } { Task->where(points: 99)->exists() }"# model ->fake (values...: Self ) → Self KScript.Data Salva un record con valori fittizi per i campi che ne richiedono uno, e con questi valori: User->fake(role: 'admin'). I campi facoltativi e quelli con valori predefiniti sono lasciati al modello, e una relazione obbligatoria riceve anch'essa un record fittizio. Per test e seeder.
Parametro Accetta valuesSelf , quanti ne vuoi
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Person" fields="name: string, email: email" />
<do>$db->create_table(Person)</do>
<let name="joan" value="Person->fake(name: 'Joan')" />
<echo>{ $joan.name } { $joan.email->ends_with('@example.com') }</echo>.kscript XML Copia
import KScript.Data
model Person {
name: string
email: email
}
$db->create_table(Person)
$joan = Person->fake(name: 'Joan')
echo "{ $joan.name } { $joan.email->ends_with('@example.com') }"# model ->fake_many (count: number , values...: Self ) → Self[] KScript.Data Salva count record fittizi, ciascuno con questi valori, e li restituisce: Post->fake_many(3, author: $joan).
Parametro Accetta count obbligatorio number valuesSelf , quanti ne vuoi
Gli argomenti possono essere con nome: ->fake_many(…, values: …).
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Person" fields="name: string, email: email" />
<do>$db->create_table(Person)</do>
<let name="people" value="Person->fake_many(3)" />
<echo>{ $people->count() } made, { Person->count() } saved</echo>.kscript XML Copia
import KScript.Data
model Person {
name: string
email: email
}
$db->create_table(Person)
$people = Person->fake_many(3)
echo "{ $people->count() } made, { Person->count() } saved"Il record con questa chiave, o null.
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->find(1).title }</echo>
<echo>{ Task->find(99) ?? 'none' }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo Task->find(1).title
echo Task->find(99) ?? 'none'# model ->find_or_fail (id ) → Self KScript.Data Il record con questa chiave, oppure un errore ModelNotFound (un 404 in una route).
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<try>
<do>Task->find_or_fail(99)</do>
<catch type="ModelNotFound" as="e">
<echo>{ $e.type }: { $e.message }</echo>
</catch>
</try>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
try {
Task->find_or_fail(99)
} catch ModelNotFound as e {
echo "{ $e.type }: { $e.message }"
}La prima riga, oppure null se nessuna corrisponde.
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<let name="task" value="Task->order_by(points)->first()" />
<echo>{ $task.title }</echo>
<echo>{ Task->where(points: 99)->first() ?? 'none' }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
$task = Task->order_by(points)->first()
echo $task.title
echo Task->where(points: 99)->first() ?? 'none'# model ->first_or_create (match: map , values?: map ) → Self KScript.Data Il primo record che corrisponde, oppure un nuovo record creato dalle due mappe.
Parametro Accetta match obbligatorio map values facoltativo map
Gli argomenti possono essere con nome: ->first_or_create(…, values: …).
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<let name="plan" value="Task->first_or_create({ title: 'Plan' }, { points: 2 })" />
<let name="again" value="Task->first_or_create({ title: 'Plan' })" />
<echo>{ $plan.points } points; same record: { $again.id == $plan.id }; { Task->count() } tasks</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
$plan = Task->first_or_create({ title: 'Plan' }, { points: 2 })
$again = Task->first_or_create({ title: 'Plan' })
echo "{ $plan.points } points; same record: { $again.id == $plan.id }; { Task->count() } tasks"# model ->first_or_fail () → Self KScript.Data La prima riga, oppure un errore ModelNotFound (un 404 in una route) se nessuna corrisponde.
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<try>
<do>Task->where(points: 99)->first_or_fail()</do>
<catch type="ModelNotFound" as="e">
<echo>{ $e.type }</echo>
</catch>
</try>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
try {
Task->where(points: 99)->first_or_fail()
} catch ModelNotFound as e {
echo $e.type
}# model ->first_or_new (match: map , values?: map ) → Self KScript.Data Il primo record che corrisponde, oppure uno nuovo creato dalle due mappe che non è ancora salvato: chiama ->save() per conservarlo.
Parametro Accetta match obbligatorio map values facoltativo map
Gli argomenti possono essere con nome: ->first_or_new(…, values: …).
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<let name="plan" value="Task->first_or_new({ title: 'Plan' }, { points: 2 })" />
<echo>{ $plan->is_saved() }</echo>
<do>$plan->save()</do>
<echo>{ Task->count() } tasks</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
$plan = Task->first_or_new({ title: 'Plan' }, { points: 2 })
echo $plan->is_saved()
$plan->save()
echo "{ Task->count() } tasks"Elimina ogni riga che corrisponde, anche per i modelli che estendono SoftDeletes. Restituisce quante ne ha eliminate.
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->where(done: true)->force_delete() } removed</echo>
<echo>{ Task->with_trashed()->count() } left in the table</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo "{ Task->where(done: true)->force_delete() } removed"
echo "{ Task->with_trashed()->count() } left in the table"Esegue la query: un elenco di record (mappe, per una tabella senza modello).
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<for each="task" in="Task->where(done: false)->order_by(title)->get()">
<echo>{ $task.title }: { $task.points }</echo>
</for>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
for task in Task->where(done: false)->order_by(title)->get() {
echo "{ $task.title }: { $task.points }"
}Divide le righe in gruppi con gli stessi valori in queste colonne, per ottenere i totali per gruppo: Order->group_by(status)->count() restituisce { open: 3, paid: 5 }. Con due colonne ogni totale si trova in una mappa dentro una mappa.
Parametro Accetta columnsSelf.fields , quanti ne vuoi
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->group_by(done)->count() }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo Task->group_by(done)->count()Legge le righe in ordine casuale, ogni volta un ordine diverso: Quote->in_random_order()->first().
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->in_random_order()->count() } tasks, in a new order each time</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo "{ Task->in_random_order()->count() } tasks, in a new order each time"# model ->increment (column: Self.fields , by?: number ) → number KScript.Data Aggiunge a una colonna numerica su ogni riga corrispondente (1 per impostazione predefinita). Restituisce quante righe sono cambiate.
Parametro Accetta column obbligatorio Self.fields by facoltativo number
Gli argomenti possono essere con nome: ->increment(…, by: …).
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<do>Task->where(title: 'Ship it')->increment(points, by: 2)</do>
<echo>{ Task->where(title: 'Ship it')->value(points) }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
Task->where(title: 'Ship it')->increment(points, by: 2)
echo Task->where(title: 'Ship it')->value(points)Aggiunge righe: una mappa, oppure un elenco di mappe. Su un modello ciascuna viene controllata come con ->create(). Restituisce quante ne sono state aggiunte.
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->insert([{ title: 'Plan' }, { title: 'Review', points: 2 }]) } added</echo>
<echo>{ Task->count() } tasks</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo "{ Task->insert([{ title: 'Plan' }, { title: 'Review', points: 2 }]) } added"
echo "{ Task->count() } tasks"# model ->latest (column?: Self.fields ) → query KScript.Data Ordina dal più recente per la colonna indicata. Per impostazione predefinita usa created_at, oppure la chiave primaria per un modello che non estende Timestamps.
Parametro Accetta column facoltativo Self.fields
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->latest(points)->value(title) }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo Task->latest(points)->value(title)Legge al massimo questo numero di righe.
Parametro Accetta count obbligatorio number
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->order_by(title)->limit(2)->pluck(title) }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo Task->order_by(title)->limit(2)->pluck(title)Blocca le righe lette fino alla fine della transazione, così nessun altro le modifica prima. Usalo all'interno di una transazione. Su SQLite, che invece blocca l'intero database per le scritture di una transazione, non cambia nulla.
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<transaction>
<let name="next" value="Task->where(done: false)->order_by(id)->lock_for_update()->first()" />
<do>$next->update(done: true)</do>
</transaction>
<echo>{ Task->where(done: true)->count() } done</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
transaction {
$next = Task->where(done: false)->order_by(id)->lock_for_update()->first()
$next->update(done: true)
}
echo "{ Task->where(done: true)->count() } done"# model ->max (column: Self.fields ) → Self[column] KScript.Data Il valore più grande di una colonna, o null quando nessuna riga corrisponde.
Parametro Accetta column obbligatorio Self.fields
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->max(points) }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo Task->max(points)# model ->min (column: Self.fields ) → Self[column] KScript.Data Il valore più piccolo di una colonna, o null quando nessuna riga corrisponde.
Parametro Accetta column obbligatorio Self.fields
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->min(points) }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo Task->min(points)Salta prima questo numero di righe.
Parametro Accetta count obbligatorio number
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->order_by(title)->offset(1)->limit(1)->pluck(title) }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo Task->order_by(title)->offset(1)->limit(1)->pluck(title)# model ->oldest (column?: Self.fields ) → query KScript.Data Ordina dal più vecchio per la colonna indicata. Per impostazione predefinita usa created_at, oppure la chiave primaria per un modello che non estende Timestamps.
Parametro Accetta column facoltativo Self.fields
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->oldest(points)->value(title) }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo Task->oldest(points)->value(title)Per i modelli che estendono SoftDeletes: legge solo i record eliminati.
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<do>Task->where(done: true)->delete()</do>
<echo>{ Task->only_trashed()->pluck(title) }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
Task->where(done: true)->delete()
echo Task->only_trashed()->pluck(title)# model ->or_where (conditions...: Self ) → query KScript.Data Come ->where(), unito con or: righe che soddisfano finora le condizioni, oppure queste.
Parametro Accetta conditionsSelf , quanti ne vuoi
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->where(done: true)->or_where(points: 5)->pluck(title) }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo Task->where(done: true)->or_where(points: 5)->pluck(title)# model ->order_by (columns...: Self.fields ) → query KScript.Data Ordina le righe: ->order_by(name), ->order_by(created_at: desc, name: asc).
Parametro Accetta columnsSelf.fields , quanti ne vuoi
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->order_by(points: desc)->pluck(title) }</echo>
<echo>{ Task->order_by(done, title)->pluck(title) }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo Task->order_by(points: desc)->pluck(title)
echo Task->order_by(done, title)->pluck(title)# model ->paginate (per_page?: number , page? ) → map KScript.Data Una pagina di righe: { items, page, per_page, total, last_page, from, to, has_more }. per_page è 15 per impostazione predefinita; page (1 per impostazione predefinita) può essere un numero o un testo come $query.page, e un testo che non è un numero equivale alla pagina 1.
Parametro Accetta per_page facoltativo number page facoltativo qualsiasi valore
Gli argomenti possono essere denominati: ->paginate(…, page: …).
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<let name="page" value="Task->order_by(id)->paginate(2, page: 1)" />
<echo>{ $page.items->map(fn(t) => $t.title) }: page { $page.page } of { $page.last_page }, { $page.total } in all</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
$page = Task->order_by(id)->paginate(2, page: 1)
echo "{ $page.items->map(fn(t) => $t.title) }: page { $page.page } of { $page.last_page }, { $page.total } in all"# model ->pluck (column: Self.fields ) → Self[column][] KScript.Data Una colonna di ogni riga che corrisponde, come elenco: User->pluck(email).
Parametro Accetta column obbligatorio Self.fields
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->order_by(title)->pluck(title) }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo Task->order_by(title)->pluck(title)Una query sul modello senza ancora condizioni, da cui partire.
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<let name="query" value="Task->query()" />
<let name="query" value="$query->where(done: false)" />
<echo>{ $query->count() } to do</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
$query = Task->query()
$query = $query->where(done: false)
echo "{ $query->count() } to do"Per i modelli che estendono SoftDeletes: ripristina i record eliminati che corrispondono. Restituisce quanti sono.
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<do>Task->where(done: true)->delete()</do>
<echo>{ Task->only_trashed()->restore() } restored, { Task->count() } tasks</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
Task->where(done: true)->delete()
echo "{ Task->only_trashed()->restore() } restored, { Task->count() } tasks"# model ->select (columns...: Self.fields ) → query KScript.Data Legge solo queste colonne: ->select(id, name). Per gli altri campi i record ottengono null.
Parametro Accetta columnsSelf.fields , quanti ne vuoi
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<let name="task" value="Task->select(id, title)->first()" />
<echo>{ $task.title }: { $task.points ?? 'not read' }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
$task = Task->select(id, title)->first()
echo "{ $task.title }: { $task.points ?? 'not read' }"Con ->lock_for_update(): salta le righe bloccate da un'altra transazione, invece di attendere. Utile per prendere lavori da una coda. Su SQLite, che non ha blocchi di riga, non cambia nulla.
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<transaction>
<let name="next" value="Task->where(done: false)->order_by(id)->lock_for_update()->skip_locked()->first()" />
<echo>{ $next.title }</echo>
</transaction>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
transaction {
$next = Task->where(done: false)->order_by(id)->lock_for_update()->skip_locked()->first()
echo $next.title
}La unica riga che corrisponde: se nessuna corrisponde viene generato un errore ModelNotFound (un 404 in una route), se ne corrispondono più di una un errore MultipleRecordsFound.
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->where(title: 'Fix bug')->sole().points }</echo>
<try>
<do>Task->where(done: false)->sole()</do>
<catch type="MultipleRecordsFound" as="e">
<echo>{ $e.type }</echo>
</catch>
</try>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo Task->where(title: 'Fix bug')->sole().points
try {
Task->where(done: false)->sole()
} catch MultipleRecordsFound as e {
echo $e.type
}La SQL che la query esegue e i suoi parametri, senza eseguirla: { sql, params }.
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<let name="query" value="Task->where(done: false)->order_by(title)->sql()" />
<echo>{ $query.sql }</echo>
<echo>{ $query.params->convert(to: json) }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
$query = Task->where(done: false)->order_by(title)->sql()
echo $query.sql
echo $query.params->convert(to: json)Il totale di una colonna sulle righe che corrispondono (0 quando nessuna corrisponde).
Parametro Accetta column obbligatorio Self.fields
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->sum(points) } points, { Task->where(done: false)->sum(points) } left</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo "{ Task->sum(points) } points, { Task->where(done: false)->sum(points) } left"Imposta le colonne su ogni riga che corrisponde, senza leggerle: ->update(active: false). Restituisce quante righe sono cambiate.
Parametro Accetta valuesSelf , quanti ne vuoi
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->where(done: false)->update(done: true) } changed</echo>
<echo>{ Task->where(done: true)->count() } done</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo "{ Task->where(done: false)->update(done: true) } changed"
echo "{ Task->where(done: true)->count() } done"# model ->update_or_create (match: map , values?: map ) → Self KScript.Data Aggiorna il primo record che corrisponde con i valori, oppure ne crea uno da entrambe le mappe. Restituisce il record: Product->update_or_create({ sku: $item.sku }, { price: $item.price }).
Parametro Accetta match obbligatorio map values facoltativo map
Gli argomenti possono essere denominati: ->update_or_create(…, values: …).
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<let name="ship" value="Task->update_or_create({ title: 'Ship it' }, { points: 8 })" />
<let name="plan" value="Task->update_or_create({ title: 'Plan' }, { points: 1 })" />
<echo>{ $ship.points } points; { Task->count() } tasks</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
$ship = Task->update_or_create({ title: 'Ship it' }, { points: 8 })
$plan = Task->update_or_create({ title: 'Plan' }, { points: 1 })
echo "{ $ship.points } points; { Task->count() } tasks"# model ->upsert (row: map , key?: string ) → number KScript.Data Inserisce atomicamente una riga oppure aggiorna i campi forniti in caso di conflitto su un valore unico. La chiave deve indicare una colonna unica. Il conteggio restituito è il numero di righe interessate indicato dal provider.
Parametro Accetta row obbligatorio map key facoltativo string
Gli argomenti possono essere denominati: ->upsert(…, key: …).
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<do>Task->upsert({ title: 'Ship it', points: 8 }, key: 'title'); Task->upsert({ title: 'Plan', points: 1 }, key: 'title')</do>
<echo>{ Task->where(title: 'Ship it')->value(points) } points, { Task->count() } tasks</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
Task->upsert({ title: 'Ship it', points: 8 }, key: 'title')
Task->upsert({ title: 'Plan', points: 1 }, key: 'title')
echo "{ Task->where(title: 'Ship it')->value(points) } points, { Task->count() } tasks"# model ->value (column: Self.fields ) → Self[column] KScript.Data Una colonna della prima riga, oppure null.
Parametro Accetta column obbligatorio Self.fields
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->where(done: true)->value(title) }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo Task->where(done: true)->value(title)# model ->when (test , then: fn , otherwise?: fn ) → query KScript.Data Modifica la query solo quando la condizione è vera: ->when($search != null, fn(q) => $q->where(fn(u) => $u.name->contains($search))).
Parametro Accetta test obbligatorio qualsiasi valore then obbligatorio fn otherwise facoltativo fn
Gli argomenti possono essere denominati: ->when(…, then: …, otherwise: …).
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<let name="search" value="'docs'" />
<echo>{ Task->when($search != null, fn(q) => $q->where(fn(t) => $t.title->contains($search)))->pluck(title) }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
$search = 'docs'
echo Task->when($search != null, fn(q) => $q->where(fn(t) => $t.title->contains($search)))->pluck(title)# model ->where (conditions...: Self ) → query KScript.Data Mantiene le righe che corrispondono: una lambda compilata in SQL, fn(u) => $u.age >= 18 and $u.name->starts_with('J'), oppure nomi e valori, role: admin, team_id: 3 (un elenco significa uno qualsiasi di essi; null significa nessuno).
Parametro Accetta conditionsSelf , quanti ne vuoi
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->where(done: false)->pluck(title) }</echo>
<echo>{ Task->where(fn(t) => $t.points >= 3)->count() }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo Task->where(done: false)->pluck(title)
echo Task->where(fn(t) => $t.points >= 3)->count()# model ->where_not (conditions...: Self ) → query KScript.Data Esclude le righe che corrispondono.
Parametro Accetta conditionsSelf , quanti ne vuoi
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ Task->where_not(done: true)->pluck(title) }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo Task->where_not(done: true)->pluck(title)# model ->with (relations...: Self.fields(relations: only) ) → query KScript.Data Carica le relazioni insieme ai record, con una query per relazione anziché una per record: ->with(author, tags), ->with(posts.tags), oppure ->with(posts: fn(q) => $q->where(published: true)) per caricarne solo alcune.
Parametro Accetta relationsSelf.fields(relations: only) , quante ne vuoi
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Author" fields="name: string, posts: Post[]" />
<model name="Post" fields="title: string, published: bool = false, author: Author" />
<do>$db->create_table(Author); $db->create_table(Post)</do>
<let name="ada" value="Author->create(name: 'Ada')" />
<do>Post->create(title: 'Notes', published: true, author: $ada); Post->create(title: 'Draft', author: $ada)</do>
<for each="post" in="Post->with(author)->order_by(title)->get()">
<echo>{ $post.title } by { $post.author.name }</echo>
</for>.kscript XML Copia
import KScript.Data
model Author {
name: string
posts: Post[]
}
model Post {
title: string
published: bool = false
author: Author
}
$db->create_table(Author)
$db->create_table(Post)
$ada = Author->create(name: 'Ada')
Post->create(title: 'Notes', published: true, author: $ada)
Post->create(title: 'Draft', author: $ada)
for post in Post->with(author)->order_by(title)->get() {
echo "{ $post.title } by { $post.author.name }"
}# model ->with_count (relations...: Self.fields(relations: only) ) → query KScript.Data Conta una relazione di tipo lista per ogni record, come campo con il suo nome: ->with_count(posts) assegna a ogni utente posts_count. ->with_count(posts: fn(q) => $q->where(published: true)) ne conta solo alcune.
Parametro Accetta relationsSelf.fields(relations: only) , quante ne vuoi
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Author" fields="name: string, posts: Post[]" />
<model name="Post" fields="title: string, published: bool = false, author: Author" />
<do>$db->create_table(Author); $db->create_table(Post)</do>
<let name="ada" value="Author->create(name: 'Ada')" />
<do>Post->create(title: 'Notes', published: true, author: $ada); Post->create(title: 'Draft', author: $ada)</do>
<let name="author" value="Author->with_count(posts)->first()" />
<echo>{ $author.name } has { $author.posts_count } posts</echo>.kscript XML Copia
import KScript.Data
model Author {
name: string
posts: Post[]
}
model Post {
title: string
published: bool = false
author: Author
}
$db->create_table(Author)
$db->create_table(Post)
$ada = Author->create(name: 'Ada')
Post->create(title: 'Notes', published: true, author: $ada)
Post->create(title: 'Draft', author: $ada)
$author = Author->with_count(posts)->first()
echo "{ $author.name } has { $author.posts_count } posts"Per i modelli che estendono SoftDeletes: mantiene i record eliminati, che le query escludono per impostazione predefinita.
Esempio
.kscript XML Copia
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<do>Task->where(done: true)->delete()</do>
<echo>{ Task->count() } of { Task->with_trashed()->count() }</echo>.kscript XML Copia
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
title: string
done: bool = false
points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
Task->where(done: true)->delete()
echo "{ Task->count() } of { Task->with_trashed()->count() }"Metodi da any