-
-
Notifications
You must be signed in to change notification settings - Fork 101
Id generator (WIP) #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Doctrine\ODM\PHPCR\Id; | ||
|
||
use Doctrine\ODM\PHPCR\DocumentManager; | ||
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata; | ||
|
||
class AssignedPathGenerator extends IdGenerator | ||
{ | ||
/** | ||
* @param object $document | ||
* @param ClassMetadata $cm | ||
* @param DocumentManager $dm | ||
* @return string | ||
*/ | ||
public function generate($document, ClassMetadata $cm, DocumentManager $dm) | ||
{ | ||
$id = $cm->getFieldValue($document, $cm->path); | ||
if (!$id) { | ||
throw new \Exception("no id"); | ||
} | ||
return $id; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont get this id generator at all. You can with this generate a ndoe that is at any place in the object graph without it even being connected to that entities at that position. Plus you have to provide the full path, which means your domain code has to know the full path to the object and work with path + id/shortname. This is not really "hiding" persistence. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think there is a misunderstanding here. the path defines where the node is and obviously if you want to determine the full path, you need to factor in the parent path. like i said on the list the node path is: parent node path + relative node path |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Doctrine\ODM\PHPCR\Id; | ||
|
||
use Doctrine\ODM\PHPCR\DocumentManager; | ||
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata; | ||
|
||
/** | ||
* Used to abstract ID generation | ||
* | ||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL | ||
* @link www.doctrine-project.com | ||
* @since 1.0 | ||
* @author Benjamin Eberlei <kontakt@beberlei.de> | ||
* @author Lukas Kahwe Smith <smith@pooteeweet.org> | ||
*/ | ||
abstract class IdGenerator | ||
{ | ||
/** | ||
* @param int $generatorType | ||
* @return IdGenerator | ||
*/ | ||
static public function create($generatorType) | ||
{ | ||
switch ($generatorType) { | ||
case ClassMetadata::IDGENERATOR_ASSIGNED: | ||
$instance = new AssignedPathGenerator(); | ||
break; | ||
case ClassMetadata::IDGENERATOR_REPOSITORY: | ||
$instance = new RepositoryPathGenerator(); | ||
break; | ||
default: | ||
throw \Exception("ID Generator does not exist!"); | ||
} | ||
return $instance; | ||
} | ||
|
||
/** | ||
* @param object $document | ||
* @param ClassMetadata $cm | ||
* @param DocumentManager $dm | ||
*/ | ||
abstract public function generate($document, ClassMetadata $cm, DocumentManager $dm); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Doctrine\ODM\PHPCR\Id; | ||
|
||
use Doctrine\ODM\PHPCR\DocumentManager; | ||
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata; | ||
|
||
class RepositoryPathGenerator extends IdGenerator | ||
{ | ||
/** | ||
* @param object $document | ||
* @param ClassMetadata $cm | ||
* @param DocumentManager $dm | ||
* @return string | ||
*/ | ||
public function generate($document, ClassMetadata $cm, DocumentManager $dm) | ||
{ | ||
$repository = $dm->getRepository($cm->class); | ||
if (!($repository instanceof RepositoryPathInterface)) { | ||
throw new \Exception("no id"); | ||
} | ||
|
||
// TODO: should we have some default implementation (parent path + some md5/object id)? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not have the object provide some slug + the parent path? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think its nicer to have the logic on the repository rather than the document. the repository would then read the document instance and then do what it needs (aka read some date property, a title and the parent) to generate the path |
||
$id = $repository->generatePath($document); | ||
if (!$id) { | ||
throw new \Exception("no id"); | ||
} | ||
return $id; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Doctrine\ODM\PHPCR\Id; | ||
|
||
interface RepositoryPathGenerator | ||
{ | ||
/** | ||
* @param object $document | ||
* @return string | ||
*/ | ||
function generatePath($document); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inside the Doctrine code you can use $cm->reflFields->getValue($document, $cm->path); for performance reasons.