Link Search Menu Expand Document

class Sivujetti\StoredObjects\StoredObjectsRepository

Luokka, jolla voi tallentaa tietoa tietokantaan. Samankaltainen kuin Wordpressin Options API.

Synopsis

final class StoredObjectsRepository {

    /* Metodit */
    public putEntry(string $objectName, array<string, mixed> $data): string|false
    public find(string $objectName): MySelect
    public updateEntry(string $objectName, array<string, mixed> $data): MyUpdate
}

Metodit

putEntry()

Tallentaa objektin tietokantaan, palauttaa id:n.

Signature

public function putEntry(string $objectName, array<string, mixed> $data): string|false

Esimerkit

$insertId = $storedObjectsRepo->putEntry("JetForms:submissions", [
    "sentAt" => time(),
    "sentFromPage" => "/page",
    "answers" => ["name" => "Submit Te'r", "message" => "Hello"],
]);

find()

Palauttaa query-builderin, joka palauttaa tallennettuja objekteja tietokannasta.

Signature

public function find(string $objectName): MySelect

Esimerkit

// Yksi
$entry = $storedObjectsRepo->find("JetForms:mailSendSettings")->fetch() ?? null;
echo $entry instanceof \Sivujetti\StoredObjects\Entities\Entry ? "y" : "n";

// Monta
$entries = $storedObjectsRepo->find("JetForms:submissions")->fetchAll();

// Lisäfilttereillä
$entries = $storedObjectsRepo->find("JetForms:submissions")->where("id > ?", ["4"])->fetchAll();

updateEntry()

Palauttaa query-builderin, joka yliajaa objekteja tietokantaan.

Signature

public function updateEntry(string $objectName, array<string, mixed> $data): MyUpdate

Esimerkit

$updated = [
    "sendingMethod" => $req->body->sendingMethod,
    "SMTP_host" => $req->body->SMTP_host ?? null,
    ...
];

// Kaikki rivit, joilla key "JetForms:mailSendSettings"
$numAffectedRows = $storedObjectsRepo
    ->updateEntry("JetForms:mailSendSettings", $updated)
    ->execute();

// Vain yksi rivi
$numAffectedRows = $storedObjectsRepo
    ->updateEntry("JetForms:mailSendSettings", $updated)
    ->where("id = ?", ["2"], "AND")
    ->execute();

© Copyright 2021-present ut4 (CC BY-SA).