Skip to content

Commit c59a5fd

Browse files
authored
Merge pull request #2 from cbikash/query-builder
added functions which helps to return a query builder with associated with table
2 parents 002b95d + 092e425 commit c59a5fd

File tree

4 files changed

+127
-8
lines changed

4 files changed

+127
-8
lines changed

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,39 @@ public function index(Request $request)
145145
## Available Repository Functions
146146
Here are some of the key functions available within your repositories:
147147
```php
148+
/**
149+
* Get the current model instance.
150+
*
151+
* This method returns the model associated with the repository.
152+
* The returned object could either be an instance of the Model class or any object that extends Model.
153+
* Useful when you need to access the model for further operations in the repository.
154+
*
155+
* @return Model|mixed The model instance being used by the repository.
156+
*/
157+
public function getModel();
158+
159+
/**
160+
* Get the name of the table associated with the model.
161+
*
162+
* This method retrieves the table name from the model instance.
163+
* It assumes that the model has a method `getTable()` which returns the database table's name associated with the model.
164+
* Useful when you need to dynamically reference the model's table in queries.
165+
*
166+
* @return string The table name associated with the current model.
167+
*/
168+
public function getTable();
169+
170+
/**
171+
* Create a new query builder instance for the current table.
172+
*
173+
* This method initiates a query builder for the table associated with the current model.
174+
* It uses the `connection` property to access the database connection and starts a new query on the table retrieved from `getTable()`.
175+
* Useful when you need to build complex queries programmatically in the repository.
176+
*
177+
* @return \Illuminate\Database\Query\Builder A new query builder instance for the table.
178+
*/
179+
public function createQueryBuilder();
180+
148181
/**
149182
* Find a single record based on criteria.
150183
* @param array $criteria
@@ -164,7 +197,7 @@ public function findAll(): mixed;
164197
* @param array $orders
165198
* @return mixed
166199
*/
167-
public function findBy(array $filters = [], array $orders = []): mixed;
200+
public function findBy(array $filters, array $orders = []): mixed;
168201
169202
/**
170203
* Retrieve a record by its ID.

src/Command/CreateRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private function getContent(string $modelName, string $modelClass, string $repos
112112
*
113113
* @method {$modelName}|null findOneBy(array \$criteria) Retrieve a single {$modelName} record based on criteria
114114
* @method {$modelName}|null getById(mixed \$id) Retrieve a {$modelName} record by ID
115-
* @method Collection|{$modelName}[]|null findBy(array \$filters = [], array \$orders = []) Retrieve records based on filters and ordering
115+
* @method Collection|{$modelName}[]|null findBy(array \$filters, array \$orders = []) Retrieve records based on filters and ordering
116116
* @method {$modelName} create(array \$data) Create a new {$modelName} record
117117
* @method {$modelName} update(mixed \$id, array \$data) Update an existing {$modelName} record by ID
118118
* @method void delete(mixed \$id) Delete a {$modelName} record by ID

src/EntityRepository.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,47 @@ public function __construct($entityClass = null, null|string $name = null)
6060
$this->connection = DB::connection($name);
6161
}
6262

63+
/**
64+
* Get the current model instance.
65+
*
66+
* This method returns the model associated with the repository.
67+
* The returned object could either be an instance of the Model class or any object that extends Model.
68+
* Useful when you need to access the model for further operations in the repository.
69+
*
70+
* @return Model|mixed The model instance being used by the repository.
71+
*/
72+
public function getModel()
73+
{
74+
return $this->model;
75+
}
76+
77+
/**
78+
* Get the name of the table associated with the model.
79+
*
80+
* This method retrieves the table name from the model instance.
81+
* It assumes that the model has a method `getTable()` which returns the database table's name associated with the model.
82+
* Useful when you need to dynamically reference the model's table in queries.
83+
*
84+
* @return string The table name associated with the current model.
85+
*/
86+
public function getTable(){
87+
return $this->model->getTable();
88+
}
89+
90+
/**
91+
* Create a new query builder instance for the current table.
92+
*
93+
* This method initiates a query builder for the table associated with the current model.
94+
* It uses the `connection` property to access the database connection and starts a new query on the table retrieved from `getTable()`.
95+
* Useful when you need to build complex queries programmatically in the repository.
96+
*
97+
* @return \Illuminate\Database\Query\Builder A new query builder instance for the table.
98+
*/
99+
public function createQueryBuilder()
100+
{
101+
return $this->connection->table($this->getTable());
102+
}
103+
63104
/**
64105
* Get the repository for a specific entity (model).
65106
*
@@ -109,7 +150,7 @@ public function findAll(): Collection|array
109150
*
110151
* @return Collection|array - The filtered and sorted model instances.
111152
*/
112-
public function findBy(array $filters = [], array $orders = []): Collection|array
153+
public function findBy(array $filters, array $orders = []): Collection|array
113154
{
114155
$qb = $this->model->query();
115156
foreach ($filters as $key => $value)

src/Interfaces/EntityManagerInterface.php

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,59 @@
2020
interface EntityManagerInterface
2121
{
2222
/**
23-
* @param string $entityClass
23+
* Get the repository for a specific entity (model).
24+
*
25+
* @param string $entityClass - The fully qualified class name of the entity (model).
26+
*
27+
* @throws \Exception - If the repository class does not exist.
28+
*
29+
* <code>
30+
* $object = $this->entityManager->getRepository(Model::class)->find($id)
31+
* </code>
32+
*
33+
* @inheritDoc
2434
*/
2535
public function getRepository(string $entityClass): mixed;
2636

2737
/**
28-
* @param array $criteria
29-
* @return Model|array|Builder|null Find One result according to given criteria
30-
* Find One result according to given criteria
38+
* Get the current model instance.
39+
*
40+
* This method returns the model associated with the repository.
41+
* The returned object could either be an instance of the Model class or any object that extends Model.
42+
* Useful when you need to access the model for further operations in the repository.
43+
*
44+
* @return Model|mixed The model instance being used by the repository.
45+
*/
46+
public function getModel();
47+
48+
/**
49+
* Get the name of the table associated with the model.
50+
*
51+
* This method retrieves the table name from the model instance.
52+
* It assumes that the model has a method `getTable()` which returns the database table's name associated with the model.
53+
* Useful when you need to dynamically reference the model's table in queries.
54+
*
55+
* @return string The table name associated with the current model.
56+
*/
57+
public function getTable();
58+
59+
/**
60+
* Create a new query builder instance for the current table.
61+
*
62+
* This method initiates a query builder for the table associated with the current model.
63+
* It uses the `connection` property to access the database connection and starts a new query on the table retrieved from `getTable()`.
64+
* Useful when you need to build complex queries programmatically in the repository.
65+
*
66+
* @return \Illuminate\Database\Query\Builder A new query builder instance for the table.
67+
*/
68+
public function createQueryBuilder();
69+
70+
/**
71+
* Find a single record that matches the provided criteria.
72+
*
73+
* @param array $criteria - Filters to apply to the query.
74+
*
75+
* @return Model|Builder|null - The found model instance or null if not found.
3176
*/
3277
public function findOneBy(array $criteria): Model|array|Builder|null;
3378

@@ -44,7 +89,7 @@ public function findAll(): mixed;
4489
*
4590
* Return values according to filter or criteria, it also accepts orders parameter and return order according to it
4691
*/
47-
public function findBy(array $filters = [], array $orders = []): mixed;
92+
public function findBy(array $filters, array $orders = []): mixed;
4893

4994
/**
5095
* @param $id

0 commit comments

Comments
 (0)