doctrine2.2ormをダウンロードしました。インストールガイドを読みましたが、ドキュメントを正しく理解できません。誰かが教義の設定プロセスを案内してくれますか?私は以前、JavaでHibernateORMフレームワークを使用していました。彼らは初心者にとって理解しやすい優れたドキュメントを持っています。私は教義の文書がそのレベルであるとは思いません。誰かが最初に教義に関するいくつかのサンプルプロジェクトを提供できますか?
2 に答える
あなたのウェブサイトプロジェクトに教義をインストールする方法にはいくつかの方法があります。簡単な代替案を紹介します。
doctrineパッケージをダウンロードし、サーバーに圧縮解除します。これで、ディレクトリは次のようになります。
localhost / Doctrine
localhost / Doctrine / Common
localhost / Doctrine / ORM
localhost / Doctrine / DBALモデル(永続エンティティ)とプロキシを保存するには、2つの追加フォルダーを作成する必要があります。
localhost / models
localhost /proxiesEntityManagerオブジェクトの作成とデータベースへの接続を担当するクラスを作成します。Doctrineという名前の魔法のクラスを作成しましょう:
localhost / 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を含めると、WebサイトでentityManagerを使用できるようになります。
$doctrine = new Doctrine();
$user = new models\User;
$doctrine->em->persist($user);
$doctrine->em->flush();
少なくともこの投稿は、Doctrineをインストールして使用する方法についてのアイデアを得るのに役立ちます
Dotrine 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にArticleエンティティを作成します
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);