Permalink
Browse files

Adding BaseEntity::getRepository() method

  • Loading branch information...
1 parent c9bbec8 commit c23d0e825060fdf31e2f976c4f526f25eddacc74 @mariano committed Jan 10, 2012
Showing with 19 additions and 1 deletion.
  1. +10 −0 README.md
  2. +9 −1 models/BaseEntity.php
View
@@ -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
@@ -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].
View
@@ -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() {
@@ -78,6 +77,15 @@ public static function getEntityManager() {
}
/**
+ * 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
*
* @param object $eventArgs Event arguments

0 comments on commit c23d0e8

Please sign in to comment.