setStateRepository($stateRepository) ->setTable('attachments'); } protected State\Attachment $stateRepository; public function getStateRepository(): State\Attachment { return $this->stateRepository; } public function setStateRepository(State\Attachment $repository): Attachment { $this->stateRepository = $repository; return $this; } protected function idField(): string { return 'filename'; } protected function fieldsForInsert(): array { return array_merge($this->fieldsForUpdate(), ['filename']); } protected function fieldsForUpdate(): array { return [ 'message_id' ]; } protected function fieldsForCreate(): array { return $this->fieldsForInsert(); } protected function valuesForUpdate(ModelInterface $model): array { return [ $model->getMessage()->getId(), $model->getFilename() ]; } protected function valuesForInsert(ModelInterface $model): array { return $this->valuesForUpdate($model); } protected function defaultFind(ModelInterface $model): ModelInterface { return $this->fetchByFilename($model->getFilename()); } protected function valuesForCreate(array $data): array { return [ $data['message_id'], $data['filename'] ]; } protected function defaultSearch(array $data): ModelInterface { return $this->fetchByFilename($data['filename']); } public function load(array $row): \ProVM\Emails\Model\Attachment { $model = new \ProVM\Emails\Model\Attachment(); $model ->setFilename($row['filename']) ->setStateRepository($this->getStateRepository()); return $model; } public function fetchByFilename(string $filename): \ProVM\Emails\Model\Attachment { $query = "SELECT * FROM {$this->getTable()} WHERE filename = ?"; return $this->fetchOne($query, [$filename]); } public function fetchByMessage(int $message_id): array { $query = "SELECT * FROM {$this->getTable()} WHERE message_id = ?"; return $this->fetchMany($query, [$message_id]); } }