我已经下载了学说 2.2 orm。我已经阅读了它的安装指南,但我无法正确理解它的文档。有人可以指导我完成学说的设置过程。我以前一直在使用 java 中的 Hibernate ORM 框架。他们有优秀的文档,对于初学者来说很容易理解。我发现教义的文档没有达到那个水平。有人可以提供一些关于教义的示例项目吗?
问问题
5586 次
2 回答
1
有几种方法可以将教义安装到您的网站项目中。我将向您展示一个简单的替代方案:
在您的服务器中下载学说包并解压缩。现在您的目录如下所示:
localhost/Doctrine
localhost/Doctrine/Common
localhost/Doctrine/ORM
localhost/Doctrine/DBAL您需要创建两个附加文件夹以存储您的模型(持久性实体)和代理:
本地主机/模型
本地主机/代理创建一个类,该类将负责创建EntityManager对象和与数据库的连接。让我们创建一个名为 Doctrine 的魔法类:
本地主机/doctrine.php
设置属性:
<?php
use Doctrine\Common\ClassLoader,
Doctrine\ORM\Configuration,
Doctrine\ORM\EntityManager,
Doctrine\Common\Cache\ArrayCache,
Doctrine\DBAL\Logging\EchoSQLLogger;
class Doctrine{
public $em = null;
public function __construct()
{
require_once 'Doctrine/Common/ClassLoader.php';
$doctrineClassLoader = new ClassLoader('Doctrine', '/');
$doctrineClassLoader->register();
$entitiesClassLoader = new ClassLoader('models', '/models/');
$entitiesClassLoader->register();
$proxiesClassLoader = new ClassLoader('Proxies', '/proxies/');
$proxiesClassLoader->register();
// Set up caches
$config = new Configuration;
$cache = new ArrayCache;
$config->setMetadataCacheImpl($cache);
$driverImpl = $config->newDefaultAnnotationDriver(array('/models/Entities'));
$config->setMetadataDriverImpl($driverImpl);
$config->setQueryCacheImpl($cache);
$config->setQueryCacheImpl($cache);
// Proxy configuration
$config->setProxyDir('/proxies');
$config->setProxyNamespace('Proxies');
// Set up logger
$logger = new EchoSQLLogger;
//$config->setSQLLogger($logger);
$config->setAutoGenerateProxyClasses( TRUE );
// Database connection information
$connectionOptions = array(
'driver' => 'pdo_mysql',
'user' => 'USER',
'password' => 'PASS',
'host' => 'HOST',
'dbname' => 'DB_NAME'
);
// Create EntityManager
$this->em = EntityManager::create($connectionOptions, $config);
}
}
现在,一旦包含它,您就可以在您的网站中使用 entityManager。
$doctrine = new Doctrine();
$user = new models\User;
$doctrine->em->persist($user);
$doctrine->em->flush();
至少这篇文章可以帮助您了解如何安装和使用学说
于 2012-06-26T01:03:37.890 回答
1
多特林 2.5.4
使用命名空间
namespace DB;
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
class Doctrine
{
public $em;
public function __construct()
{
$paths = array("src/Application/Models/Entity");
$isDevMode = false;
// the connection configuration
$dbParams = array(
'driver' => 'pdo_mysql',
'user' => 'USER',
'password' => 'PASS',
'host' => 'HOST',
'dbname' => 'DB_NAME',
);
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, null, null, false);
$this->em = EntityManager::create($dbParams, $config);
}
}
在 src\Application\Models\Entity 中创建文章实体
namespace Application\Models\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
* @ORM\Table(name="article")
* @ORM\Entity(repositoryClass="Application\Models\Entity\ArticleRepository")
*/
class Article
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
private $id;
/**
* @var string $title
*
* @ORM\Column(type="text", length=255, nullable=true)
*/
protected $title;
/**
* @var text $body
*
* @ORM\Column(type="text", nullable=true)
*/
protected $body;
/**
* @var datetime $createdAt
*
* @ORM\Column(type="datetime", name="created_at")
*/
private $createdAt;
现在从您的类中调用实体管理器:
$doctrine = new Doctrine();
$article = $doctrine->em->find('Application\Models\Entity\Article', 1);
于 2016-03-22T11:59:39.153 回答