Skip to content

Idea: Improve Model & Repositories integration #20

@JasonTheAdams

Description

@JasonTheAdams

Presently, the library has a concept of Models and Repositories (which result in Models), but the integration is still largely left to the implementor to connect. Most notably, there are repository interfaces and an abstract class, but the model's corresponding CRUD methods require manual connection.

I think it's worth keeping the distinction that a Model doesn't have to have a Repository, but make it very easy to connect the two when it does. Therefore, I propose adding a HasRepository Trait, something like:

trait HasRepository {
	abstract protected static function getRepository(): Repository;

	public function find( $id ): ?Model {
		return static::getRepository()->getById( $id );
	}

	// other CRUD methods here
}

I'm inclined to either make it so the Repository abstract class implement the existing interfaces, or introduce a new RepositoryCrud interface that does it all. We could check in each CRUD method that the repository has the given interface, but what I don't like about that is we'd only catch that at runtime. It's safer to just be able to make the assumption that the repository will handle all the CRUD operations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions