atk4\data\Persistence_SQL::tryLoad PHP Method

tryLoad() public method

Tries to load data record, but will not fail if record can't be loaded.
public tryLoad ( Model $m, mixed $id ) : array
$m Model
$id mixed
return array
    public function tryLoad(Model $m, $id)
    {
        $load = $this->action($m, 'select');
        $load->where($m->getElement($m->id_field), $id);
        $load->limit(1);
        // execute action
        try {
            $data = $this->typecastLoadRow($m, $load->getRow());
        } catch (\PDOException $e) {
            throw new Exception(['Unable to load due to query error', 'query' => $load->getDebugQuery(false), 'model' => $m, 'conditions' => $m->conditions], null, $e);
        }
        if (!$data) {
            return;
        }
        if (isset($data[$m->id_field])) {
            $m->id = $data[$m->id_field];
        } else {
            throw new Exception(['ID of the record is unavailable. Read-only mode is not supported', 'model' => $m, 'id' => $id, 'data' => $data]);
        }
        return $data;
    }