Doctrine笔记

原文地址
doctrine: getting-started

bootstrap.php

Doctrine’s public interface is the EntityManager, it provides the access point to the complete lifecycle management of your entities and transforms entities from and back to persistence. You have to configure and create it to use your entities with Doctrine 2. I will show the configuration steps and then discuss them step by step:

<?php
// bootstrap.php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
require_once "vendor/autoload.php";
// Create a simple "default" Doctrine ORM configuration for Annotations
$isDevMode = true;
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src"), $isDevMode);
// or if you prefer yaml or XML
//$config = Setup::createXMLMetadataConfiguration(array(__DIR__."/config/xml"), $isDevMode);
//$config = Setup::createYAMLMetadataConfiguration(array(__DIR__."/config/yaml"), $isDevMode);
// database configuration parameters
$conn = array(
    'driver' => 'pdo_sqlite',
    'path' => __DIR__ . '/db.sqlite',
);
// obtaining the entity manager
$entityManager = EntityManager::create($conn, $config);

创建一个实体

<?php
// src/Product.php
class Product
{
    /**
     * @var int
     */
    protected $id;
    /**
     * @var string
     */
    protected $name;
    public function getId()
    {
        return $this->id;
    }
    public function getName()
    {
        return $this->name;
    }
    public function setName($name)
    {
        $this->name = $name;
    }
}

所有的fields都设为protected, 并且除了$id外都通过getter和setter进行读取和写入.

我们通过metadata语言设定各个fields的数据类型等信息.

<?php
// src/Product.php
/**
 * @Entity @Table(name="products")
 **/
class Product
{
    /** @Id @Column(type="integer") @GeneratedValue **/
    protected $id;
    /** @Column(type="string") **/
    protected $name;
    // .. (other code)
}

通过symfony在数据库中创建相应的table和column

写入products的脚本为:

<?php
// create_product.php
require_once "bootstrap.php";
$newProductName = $argv[1];
$product = new Product();
$product->setName($newProductName);
$entityManager->persist($product);
$entityManager->flush();
echo "Created Product with ID " . $product->getId() . "\n";

在终端运行:

$ php create_product.php ORM
$ php create_product.php DBAL

这里的$entityManagerbootstrap.php中定义
在symfony中, $entityManager可以直接定义:

$entityManager = $this->getDoctrine()->getManager();

当我们需要fetch所有产品信息时,脚本如下: ```php getRepository('Product'); $products = $productRepository->findAll(); foreach ($products as $product) { echo sprintf("-%s\n", $product->getName()); } ```

更新某个产品名:

<?php
// update_product.php <id> <new-name>
require_once "bootstrap.php";
$id = $argv[1];
$newName = $argv[2];
$product = $entityManager->find('Product', $id);
if ($product === null) {
    echo "Product $id does not exist.\n";
    exit(1);
}
$product->setName($newName);
$entityManager->flush();


## An Example Model: Bug Tracker 前面我们已经建立了products数据库,这一节将建立bug数据库用于存储产品的bug信息
  • A Bug has a description, creation date, status, reporter and engineer
  • A Bug can occur on different Products
  • Bug reporters and engineers are both Users of the system.
  • A User can create new Bugs.
  • The assigned engineer can close a Bug.
  • A User can see all his reported or assigned Bugs.
  • Bugs can be paginated through a list-view.(分页导航)
<?php
// src/Bug.php
class Bug
{
    // ... (previous code)
    protected $engineer;
    protected $reporter;
    public function setEngineer($engineer)
    {
        $engineer->assignedToBug($this);
        $this->engineer = $engineer;
    }
    public function setReporter($reporter)
    {
        $reporter->addReportedBug($this);
        $this->reporter = $reporter;
    }
    public function getEngineer()
    {
        return $this->engineer;
    }
    public function getReporter()
    {
        return $this->reporter;
    }
}
<?php
// src/User.php
class User
{
    // ... (previous code)
    private $reportedBugs = null;
    private $assignedBugs = null;
    public function addReportedBug($bug)
    {
        $this->reportedBugs[] = $bug;
    }
    public function assignedToBug($bug)
    {
        $this->assignedBugs[] = $bug;
    }
}
posted @ 2015-04-27 15:38  zjuhjm  阅读(333)  评论(0编辑  收藏  举报