[ Avaa Bypassed ]




Upload:

Command:

hmhc3928@18.116.38.30: ~ $
Metadata Drivers
================

The heart of an object relational mapper is the mapping information
that glues everything together. It instructs the EntityManager how
it should behave when dealing with the different entities.

Core Metadata Drivers
---------------------

Doctrine provides a few different ways for you to specify your
metadata:


-  **XML files** (XmlDriver)
-  **Class DocBlock Annotations** (AnnotationDriver)
-  **YAML files** (YamlDriver)
-  **PHP Code in files or static functions** (PhpDriver)

Something important to note about the above drivers is they are all
an intermediate step to the same end result. The mapping
information is populated to ``Doctrine\ORM\Mapping\ClassMetadata``
instances. So in the end, Doctrine only ever has to work with the
API of the ``ClassMetadata`` class to get mapping information for
an entity.

.. note::

    The populated ``ClassMetadata`` instances are also cached
    so in a production environment the parsing and populating only ever
    happens once. You can configure the metadata cache implementation
    using the ``setMetadataCacheImpl()`` method on the
    ``Doctrine\ORM\Configuration`` class:

    .. code-block:: php

        <?php
        $em->getConfiguration()->setMetadataCacheImpl(new ApcuCache());


If you want to use one of the included core metadata drivers you
just need to configure it. All the drivers are in the
``Doctrine\ORM\Mapping\Driver`` namespace:

.. code-block:: php

    <?php
    $driver = new \Doctrine\ORM\Mapping\Driver\XmlDriver('/path/to/mapping/files');
    $em->getConfiguration()->setMetadataDriverImpl($driver);

Implementing Metadata Drivers
-----------------------------

In addition to the included metadata drivers you can very easily
implement your own. All you need to do is define a class which
implements the ``Driver`` interface:

.. code-block:: php

    <?php
    namespace Doctrine\ORM\Mapping\Driver;
    
    use Doctrine\ORM\Mapping\ClassMetadataInfo;
    
    interface Driver
    {
        /**
         * Loads the metadata for the specified class into the provided container.
         * 
         * @param string $className
         * @param ClassMetadataInfo $metadata
         */
        function loadMetadataForClass($className, ClassMetadataInfo $metadata);
    
        /**
         * Gets the names of all mapped classes known to this driver.
         * 
         * @return array The names of all mapped classes known to this driver.
         */
        function getAllClassNames(); 
    
        /**
         * Whether the class with the specified name should have its metadata loaded.
         * This is only the case if it is either mapped as an Entity or a
         * MappedSuperclass.
         *
         * @param string $className
         * @return boolean
         */
        function isTransient($className);
    }

If you want to write a metadata driver to parse information from
some file format we've made your life a little easier by providing
the ``AbstractFileDriver`` implementation for you to extend from:

.. code-block:: php

    <?php
    class MyMetadataDriver extends AbstractFileDriver
    {
        /**
         * {@inheritdoc}
         */
        protected $_fileExtension = '.dcm.ext';
    
        /**
         * {@inheritdoc}
         */
        public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
        {
            $data = $this->_loadMappingFile($file);
    
            // populate ClassMetadataInfo instance from $data
        }
    
        /**
         * {@inheritdoc}
         */
        protected function _loadMappingFile($file)
        {
            // parse contents of $file and return php data structure
        }
    }

.. note::

    When using the ``AbstractFileDriver`` it requires that you
    only have one entity defined per file and the file named after the
    class described inside where namespace separators are replaced by
    periods. So if you have an entity named ``Entities\User`` and you
    wanted to write a mapping file for your driver above you would need
    to name the file ``Entities.User.dcm.ext`` for it to be
    recognized.


Now you can use your ``MyMetadataDriver`` implementation by setting
it with the ``setMetadataDriverImpl()`` method:

.. code-block:: php

    <?php
    $driver = new MyMetadataDriver('/path/to/mapping/files');
    $em->getConfiguration()->setMetadataDriverImpl($driver);

ClassMetadata
-------------

The last piece you need to know and understand about metadata in
Doctrine 2 is the API of the ``ClassMetadata`` classes. You need to
be familiar with them in order to implement your own drivers but
more importantly to retrieve mapping information for a certain
entity when needed.

You have all the methods you need to manually specify the mapping
information instead of using some mapping file to populate it from.
The base ``ClassMetadataInfo`` class is responsible for only data
storage and is not meant for runtime use. It does not require that
the class actually exists yet so it is useful for describing some
entity before it exists and using that information to generate for
example the entities themselves. The class ``ClassMetadata``
extends ``ClassMetadataInfo`` and adds some functionality required
for runtime usage and requires that the PHP class is present and
can be autoloaded.

You can read more about the API of the ``ClassMetadata`` classes in
the PHP Mapping chapter.

Getting ClassMetadata Instances
-------------------------------

If you want to get the ``ClassMetadata`` instance for an entity in
your project to programmatically use some mapping information to
generate some HTML or something similar you can retrieve it through
the ``ClassMetadataFactory``:

.. code-block:: php

    <?php
    $cmf = $em->getMetadataFactory();
    $class = $cmf->getMetadataFor('MyEntityName');

Now you can learn about the entity and use the data stored in the
``ClassMetadata`` instance to get all mapped fields for example and
iterate over them:

.. code-block:: php

    <?php
    foreach ($class->fieldMappings as $fieldMapping) {
        echo $fieldMapping['fieldName'] . "\n";
    }



Filemanager

Name Type Size Permission Actions
advanced-configuration.rst File 16.18 KB 0644
annotations-reference.rst File 37.16 KB 0644
architecture.rst File 7.52 KB 0644
association-mapping.rst File 31.32 KB 0644
basic-mapping.rst File 17.24 KB 0644
batch-processing.rst File 5.77 KB 0644
best-practices.rst File 3.61 KB 0644
caching.rst File 13.24 KB 0644
change-tracking-policies.rst File 5.13 KB 0644
configuration.rst File 4.29 KB 0644
dql-doctrine-query-language.rst File 60.95 KB 0644
events.rst File 30.98 KB 0644
faq.rst File 9.95 KB 0644
filters.rst File 3.38 KB 0644
improving-performance.rst File 2.66 KB 0644
inheritance-mapping.rst File 20.79 KB 0644
installation.rst File 131 B 0644
limitations-and-known-issues.rst File 7.66 KB 0644
metadata-drivers.rst File 6.02 KB 0644
namingstrategy.rst File 4.38 KB 0644
native-sql.rst File 34.31 KB 0644
partial-objects.rst File 3.52 KB 0644
php-mapping.rst File 8.82 KB 0644
query-builder.rst File 20.6 KB 0644
second-level-cache.rst File 24.02 KB 0644
security.rst File 4.68 KB 0644
tools.rst File 16.8 KB 0644
transactions-and-concurrency.rst File 13.9 KB 0644
unitofwork-associations.rst File 2.74 KB 0644
unitofwork.rst File 6.73 KB 0644
working-with-associations.rst File 22.24 KB 0644
working-with-objects.rst File 31.61 KB 0644
xml-mapping.rst File 27.11 KB 0644
yaml-mapping.rst File 4.42 KB 0644