Skip to content

Commit

Permalink
Merge pull request #861 from doctrine/cleanup-doc
Browse files Browse the repository at this point in the history
remove obsolete annotation mapping doc, modernize code in examples
  • Loading branch information
dbu committed Jan 12, 2024
2 parents 7df4651 + 94fb4bf commit 917fc65
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 891 deletions.
33 changes: 14 additions & 19 deletions docs/en/cookbook/custom_documentclass_mapper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,30 @@ An example mapper from the `symfony cmf sandbox`_

use Doctrine\ODM\PHPCR\DocumentClassMapper;
use Doctrine\ODM\PHPCR\DocumentManager;
use Doctrine\ODM\PHPCR\Document\Generic;

use PHPCR\NodeInterface;
use PHPCR\PropertyType;

class MagnoliaDocumentClassMapper extends DocumentClassMapper
{
private $templateMap;

/**
* @param array $templateMap map from mgnl:template values to document class names
*/
public function __construct($templateMap)
{
$this->templateMap = $templateMap;
public function __construct(
/**
* @var array<string, string> map from mgnl:template values to document class names
*/
private array $templateMap
) {
}

/**
* Determine the class name from a given node
*
* @param DocumentManager
* @param NodeInterface $node
* @param string $className
*
* @return string
*
* @throws \RuntimeException if no class name could be determined
*/
public function getClassName(DocumentManager $dm, NodeInterface $node, $className = null)
public function getClassName(DocumentManager $dm, NodeInterface $node, string $className = null): string
{
$className = parent::getClassName($dm, $node, $className);
if ('Doctrine\ODM\PHPCR\Document\Generic' == $className) {
if (Generic::class === $className) {
if ($node->hasNode('MetaData')) {
$metaData = $node->getNode('MetaData');
if ($metaData->hasProperty('mgnl:template')) {
Expand All @@ -70,7 +63,7 @@ custom mapper::
/* prepare the doctrine configuration */
$config = new \Doctrine\ODM\PHPCR\Configuration();
$map = array(
'standard-templating-kit:pages/stkSection' => 'Sandbox\MagnoliaBundle\Document\Section',
'standard-templating-kit:pages/stkSection' => \Sandbox\MagnoliaBundle\Document\Section::class,
);
$mapper = new MagnoliaDocumentClassMapper($map);
$config->setDocumentClassMapper($mapper);
Expand Down Expand Up @@ -127,6 +120,8 @@ of instantiating the default one. An example from the `symfony cmf sandbox`_
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Sandbox\MagnoliaBundle\Document\MagnoliaDocumentClassMapper;
use Sandbox\MagnoliaBundle\Document\Section;
$container
->register('doctrine.odm_configuration', '%doctrine_phpcr.odm.configuration.class%')
Expand All @@ -136,10 +131,10 @@ of instantiating the default one. An example from the `symfony cmf sandbox`_
;
$container ->setDefinition('sandbox_amgnolia.odm_mapper', new Definition(
'Sandbox\MagnoliaBundle\Document\MagnoliaDocumentClassMapper',
MagnoliaDocumentClassMapper::class,
array(
array(
'standard-templating-kit:pages/stkSection' => 'Sandbox\MagnoliaBundle\Document\Section',
'standard-templating-kit:pages/stkSection' => Section::class,
),
),
));
Expand Down
46 changes: 13 additions & 33 deletions docs/en/cookbook/last-modified.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,22 @@ you can get timestamps on your documents by simply adding the mixins:

.. code-block:: php
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;
/**
* @PHPCR\Document(
* mixins={"mix:created", "mix:lastModified"}
* )
*/
#[PHPCR\Document(mixins: ["mix:created", "mix:lastModified"])]
class SomeDocument
{
/**
* @PHPCR\Field(type="date", property="jcr:created")
*/
private $created;
#[PHPCR\Field(type: 'date', property: 'jcr:created')]
private ?\DateTimeInterface $created;
/**
* @PHPCR\Field(type="string", property="jcr:createdBy")
*/
private $createdBy;
#[PHPCR\Field(type: 'string', property: 'jcr:createdBy')]
private ?string $createdBy;
/**
* @PHPCR\Field(type="date", property="jcr:lastModified")
*/
private $lastModified;
#[PHPCR\Field(type: 'date', property: 'jcr:lastModified')]
private ?\DateTimeInterface $lastModified;
/**
* @PHPCR\Field(type="string", property="jcr:lastModifiedBy")
*/
private $lastModifiedBy;
#[PHPCR\Field(type: 'string', property: 'jcr:lastModifiedBy')]
private ?string $lastModifiedBy;
}
.. code-block:: xml
Expand Down Expand Up @@ -113,29 +101,21 @@ date, write an event listener as follows and register it with the EventManager::
*/
class LastModified
{
/**
* @param LifecycleEventArgs $e
*/
public function prePersist(LifecycleEventArgs $e)
public function prePersist(LifecycleEventArgs $e): void
{
$this->updateLastModifiedProperty($e);
}

/**
* @param LifecycleEventArgs $e
*/
public function preUpdate(LifecycleEventArgs $e)
public function preUpdate(LifecycleEventArgs $e): void
{
$this->updateLastModifiedProperty($e);
}

/**
* If the document has the mixin mix:lastModified then update the field
* that is mapped to jcr:lastModified.
*
* @param LifecycleEventArgs $e
*/
protected function updateLastModifiedProperty(LifecycleEventArgs $e)
private function updateLastModifiedProperty(LifecycleEventArgs $e): void
{
$document = $e->getObject();

Expand Down
1 change: 0 additions & 1 deletion docs/en/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ Mapping Objects onto a Document Repository

* **Mapping Driver References**:
:doc:`PHP Attributes <reference/attributes-mapping>` |
:doc:`Docblock Annotations (deprecated) <reference/annotations-mapping>` |
:doc:`XML <reference/xml-mapping>` |
:doc:`YAML <reference/yml-mapping>` |
:doc:`Metadata Drivers <reference/metadata-drivers>`
Expand Down
Loading

0 comments on commit 917fc65

Please sign in to comment.