Skip to content

Commit

Permalink
Adding BaseEntity::getRepository() method
Browse files Browse the repository at this point in the history
  • Loading branch information
mariano committed Jan 10, 2012
1 parent c9bbec8 commit c23d0e8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -199,6 +199,9 @@ named `getEntityManager()` (which uses a static property inherited from
$em = User::getEntityManager();
```

`BaseEntity` also offers a `getRepository()` method which will return the
repository for the model (see the section *Fetching record* below.)

### Fetching records ###

Once you have the entity manager, you can fetch a user with ID 1 (notice how
Expand All @@ -215,6 +218,13 @@ or using model repositories:
$user = $em->getRepository('app\models\User')->findOneById(1);
```

If your model extends from `BaseEntity`, then the above could be retwritten
as:

```php
$user = User::getRepository()->findOneById(1);
```

If you want to find out more about querying models with Doctrine, go through
its [Querying guide] [doctrine-querying-guide].

Expand Down
10 changes: 9 additions & 1 deletion models/BaseEntity.php
Expand Up @@ -65,7 +65,6 @@ public function __construct() {
* Get the entity manager linked to the connection defined in the property
* `$connectionName`
*
* @see BaseEntity::$validates
* @return EntityManager entity manager
*/
public static function getEntityManager() {
Expand All @@ -77,6 +76,15 @@ public static function getEntityManager() {
return $entityManager;
}

/**
* Get the repository for this model
*
* @return EntityRepository entity manager
*/
public static function getRepository() {
return static::getEntityManager()->getRepository(get_called_class());
}

/**
* Doctrine callback executed after a record was loaded
*
Expand Down

0 comments on commit c23d0e8

Please sign in to comment.