| | 51 | ## Custom Repository Class |
|---|
| | 52 | |
|---|
| | 53 | You can configure a custom repository class so you can add new methods for |
|---|
| | 54 | executing and retrieving queries. |
|---|
| | 55 | |
|---|
| | 56 | [yml] |
|---|
| | 57 | Models\User: |
|---|
| | 58 | type: entity |
|---|
| | 59 | table: user |
|---|
| | 60 | repositoryClass: UserRepository |
|---|
| | 61 | # ... |
|---|
| | 62 | |
|---|
| | 63 | Now define a `UserRepository` class: |
|---|
| | 64 | |
|---|
| | 65 | [php] |
|---|
| | 66 | class UserRepository extends EntityRepository |
|---|
| | 67 | { |
|---|
| | 68 | public function getActiveUsers() |
|---|
| | 69 | { |
|---|
| | 70 | $qb = $this->createQueryBuilder('u'); |
|---|
| | 71 | $q = $qb->getQuery(); |
|---|
| | 72 | |
|---|
| | 73 | return $q->execute(); |
|---|
| | 74 | } |
|---|
| | 75 | } |
|---|
| | 76 | |
|---|
| | 77 | Now you can use this method like the following: |
|---|
| | 78 | |
|---|
| | 79 | [php] |
|---|
| | 80 | $repository = $em->getRepository('Models\User'); |
|---|
| | 81 | $users = $repository->getActiveUsers(); |
|---|
| | 82 | |
|---|
| | 83 | If you utilize the `ActiveEntity` extension you can do the following: |
|---|
| | 84 | |
|---|
| | 85 | [php] |
|---|
| | 86 | $users = \Models\User::getActiveUsers(); |
|---|
| | 87 | |
|---|
| | 88 | In order to use the above syntax your `User` model needs to extend `sfDoctrineActiveEntity` |
|---|
| | 89 | like the following: |
|---|
| | 90 | |
|---|
| | 91 | [php] |
|---|
| | 92 | namespace Models; |
|---|
| | 93 | |
|---|
| | 94 | use sfDoctrineActiveEntity; |
|---|
| | 95 | |
|---|
| | 96 | class User extends sfDoctrineActiveEntity |
|---|
| | 97 | { |
|---|
| | 98 | // ... |
|---|
| | 99 | } |
|---|
| | 100 | |
|---|