Skip to content

Instantly share code, notes, and snippets.

@chobie
Created December 10, 2010 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chobie/736402 to your computer and use it in GitHub Desktop.
Save chobie/736402 to your computer and use it in GitHub Desktop.
Doctrator meets cakephp
#controller/doctrine_controller.php
<?php
class DoctrineController extends AppController{
public $name = "Doctrine";
public function index()
{
//AppModelで登録してあるようなのでひっぱる
$em = $this->Doctrine->doctrineConn;
$article = new Model\Article();
$article->setTitle("HelloWorld");
$article->setContent("Uhi World");
//$article->save();
$ar = $em->getRepository("Model\\Article");
$this->set("posts",$ar->findAll());
}
}
?>
#models/app_model.php
<?php
/*
git clone git://github.com/doctrine/doctrine2.git vendors/doctrine
cd vendors/doctrine;
git submodule init && git submodule update
あとdoctratorでつくったやつもvendorsにおいとく
*/
class AppModel extends Model
{
public $useTable = false;
public $manager;
public $doctrineConn;
public function __construct($id = false, $table = null, $ds = null)
{
parent::__construct($id,$table,$ds);
$this->__configureDoctrine();
}
public function __configureDoctrine()
{
require_once __DIR__ . '/../vendors/doctrine/lib/vendor/doctrine-common/lib/Doctrine/Common/ClassLoader.php';
$classLoader = new \Doctrine\Common\ClassLoader('Doctrator',"../vendors/doctrator/lib");
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine',"../vendors/doctrine/lib");
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Symfony',"../vendors/doctrine/lib/vendor");
$classLoader->register();
//DoctratorでつくったModel
$classLoader = new \Doctrine\Common\ClassLoader('Model',"../models");
$classLoader->register();
$this->__getDoctrineCompatibleDataSource();
}
public function __getDoctrineCompatibleDataSource()
{
$configuration = new \Doctrine\ORM\Configuration();
$configuration->setMetadataDriverImpl(new \Doctrator\Driver\DoctratorDriver('./Model'));
$configuration->setProxyDir('./Proxy');
$configuration->setProxyNamespace('Proxy');
$configuration->setAutoGenerateProxyClasses(true);
//cakephpのつなぎ方よくわかりません><
$em = \Doctrine\ORM\EntityManager::create(array('driver' => 'pdo_sqlite', 'path' => '/home/chobie/servers/localhost/cake.localhost/app/database.sqlite',), $configuration);
// add the entity manager to the doctrator entity manager container
\Doctrator\EntityManagerContainer::setEntityManager($em);
$this->doctrineConn = $em;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment