Doctrine 2 Stable Release!!

2010-12-21 這天 Doctrin2 釋出正式穩定版.
Doctrine 2 First Stable Release
這個版本工程之浩大於2008就開始開發,歷經2年半的時間努力.核心開發團隊五人於最後一年最後整合應用保有 Doctrine 1功能並重構並且導入新的DataMapper.

What is new in Doctrine 2?

* DQL is now a real language inside Doctrine, based on an EBNF that is parsed and transformed to SQL. Benefits of this refactoring are readable error messages, the generation of an AST that allows us to support many different vendors and powerful hooks for developers to modify and extend the DQL language to their needs. DQL can either be written as a string or be generated using a powerful QueryBuilder object.
* Your persistent objects (called entities in Doctrine 2) are not required to extend an abstract base class anymore. Doctrine 2 allows you to use Plain old PHP Objects.
* The UnitOfWork is not an alibi-pattern as implemented in Doctrine 1. It is the most central pattern in Doctrine 2. Instead of calling save() or delete() methods on your Doctrine_Record instances you now pass objects to the data mapper object called EntityManager and it keeps track of all changes until you request a synchronisation between database and the current objects in memory. This process is very efficient and has consistent semantics. This is a significant improvement over Doctrine 1 in terms of performance and developer ease-of-use.
* There are no code-generation steps from YAML to PHP involved in the library anymore. YAML, XML, PHP and Doc-Block Annotations are four first-class citizens for defining the metadata mapping between objects and database. A powerful caching layer allows Doctrine 2 to use runtime metadata without relying on code-generation.
* A clean architecture and powerful algorithms make Doctrine 2 magnitudes faster than Doctrine 1.
* Doctrine 2 supports an API that allows you to transform an arbitrary SQL statements into an object-structure. This feature is used by the Doctrine Query Language itself and is a first-class citizen of the library. It essentially allows you to make use of powerful vendor-specific features and complex SQL statements without having to cirumvent the ORM completely.
* Inheritance is not akward anymore. There are now three different types of inheritance to choose from: Mapped Superclasses, Single-Table- and Joined-Table-Inheritance.
* Many more features, just see the reference guide on what is possible with Doctrine 2.

官方有說明為何搞這摸久!!

包含使用PHP5.3特性~~
Doctrine 2 now we feel this code is more stable and much more maintainable than Doctrine 1. The ORM itself has about 1000 tests where half of these are functional tests that successfully run against all the supported database vendors MySQL, SQLite, PostgreSQL, Oracle and MSSQL. The database access layer and common libraries come with an additional 400 tests.

如何開始

You can download Doctrine 2 from our downloads section, install it via PEAR or find it in the Github repository. Symfony 2 also ships with a current version of Doctrine 2. After you installed Doctrine 2 you can go to the documentation and start reading the reference guide or the tutorial.

探索

分別下載Doctrine 1 &Doctrine 2 來看看接構上有何不同
Doctrine 1:

Doctrine 2:

可以看到很大不同!! Doctrine 1以一般Framework常見的CLASS作為目錄分類規劃.
然而Doctrine 2卻像一般軟體常見的安排方式,其中奧妙在,Doctrine 1使用autoloader 物件來負責讀取物件相關檔案.
Doctrine 2使用PHP5.3最新特性Namespaces!!
範例:doctrine-orm\Doctrine\ORM\Configuration.php

<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.doctrine-project.org>.
 */
 
namespace Doctrine\ORM;
 
use Doctrine\Common\Cache\Cache,
    Doctrine\ORM\Mapping\Driver\Driver;

Using namespaces: Aliasing/Importing
namespace下不用依賴目錄結構做讀取.
不過因為用此方式還有結構關悉.官方有佳段註解:

Warning:
Lazy load proxies always contain an instance of Doctrine’s EntityManager and all its dependencies. Therefore a var_dump() will possibly dump a very large recursive structure which is impossible to render and read. You have to use Doctrine\Common\Util\Debug::dump() to restrict the dumping to a human readable level. Additionally you should be aware that dumping the EntityManager to a Browser may take several minutes, and the Debug::dump() method just ignores any occurrences of it in Proxy instances.

Doctrine’s EntityManager 到處都是~~ 反正不要用var_dump()倒出物件來看你會爆炸XDD.
請用~~Debug::dump()

Start!!

範例1:

<?php
// test.php
 
require '/path/to/libraries/Doctrine/Common/ClassLoader.php';
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine', '/path/to/libraries');
$classLoader->register(); // register on SPL autoload stack

範例2:

<?php
// test.php
 
$lib = '/path/to/doctrine2-orm/lib/';
require $lib . 'vendor/doctrine-common/lib/Doctrine/Common/ClassLoader.php';
 
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine\Common', $lib . 'vendor/doctrine-common/lib');
$classLoader->register();
 
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine\DBAL', $lib . 'vendor/doctrine-dbal/lib');
$classLoader->register();
 
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine\ORM', $lib);
$classLoader->register();

完整範例:

<?php
// Setup Autoloader (1)
// See :doc:`Configuration <../reference/configuration>` for up to date autoloading details.
 
$config = new Doctrine\ORM\Configuration(); // (2)
 
// Proxy Configuration (3)
$config->setProxyDir(__DIR__.'/lib/MyProject/Proxies');
$config->setProxyNamespace('MyProject\Proxies');
$config->setAutoGenerateProxyClasses((APPLICATION_ENV == "development"));
 
// Mapping Configuration (4)
$driverImpl = new Doctrine\ORM\Mapping\Driver\XmlDriver(__DIR__."/config/mappings/xml");
//$driverImpl = new Doctrine\ORM\Mapping\Driver\XmlDriver(__DIR__."/config/mappings/yml");
//$driverImpl = $config->newDefaultAnnotationDriver(__DIR__."/entities");
$config->setMetadataDriverImpl($driverImpl);
 
// Caching Configuration (5)
if (APPLICATION_ENV == "development") {
    $cache = new \Doctrine\Common\Cache\ArrayCache();
} else {
    $cache = new \Doctrine\Common\Cache\ApcCache();
}
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
 
// database configuration parameters (6)
$conn = array(
    'driver' => 'pdo_sqlite',
    'path' => __DIR__ . '/db.sqlite',
);
 
// obtaining the entity manager (7)
$evm = new Doctrine\Common\EventManager()
$entityManager = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);

以下是必要的屬性:

$config->setProxyDir($dir);
$config->setProxyNamespace($namespace);
$config->setMetadataDriverImpl($driver);

以下是建議的屬性:

$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);

以下是額外的屬性:

$config->setSQLLogger($logger);
$config->setAutoGenerateProxyClasses($bool);

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *