Created
June 14, 2012 17:41
-
-
Save Alan01252/2931685 to your computer and use it in GitHub Desktop.
Repository code suggestion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//These classes instead of being in customRepositories are now in /bundles/myBundle/model/ | |
class Staff extends Repository | |
{ | |
} | |
class Account extends Repository | |
{ | |
//this method doesn't have to have anything to do with the Repository class we're extending from. | |
//and stops us having to do $account->loadCustomMethods('account')->doSomethingUniqueToAnAccountObject(); | |
public function doSomethingUniqueToAnAccountObject() | |
{ | |
} | |
} | |
//This class can go in the models bundle you already have | |
//Ideally all the EntityManager and RepositoryManager stuff would be moved into this one Repostiory object, but for now to save time lets keep it like this. | |
class Repository | |
{ | |
public static function findOne($id) | |
{ | |
$repo = EntityManager::getInstance()->getRepository(get_called_class()); //get_called_class returns account | |
$object = $repo->findOne($id); | |
//Do some conversion here to make $object a new Account class | |
//Use the repository manager and the entitity manager to find our object based on | |
return $object; | |
} | |
protected function update() | |
{ | |
//Use entity manager to update document | |
} | |
//All the other methods | |
} | |
//Allowing us to do | |
$account = Account::findOne(1); | |
$account->setName("my new name"); | |
$account->update(); | |
$account->doSomethingUniqueToAnAccountObject(); | |
//Notice we've not had to redefine the findOne method in our Staff class, and all our repostiory methods are in one place making changes easier at a later date. | |
$staff = Staff::findOne(2); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment