Created
October 6, 2015 21:50
-
-
Save MadFaill/8945811899c1c9fdce01 to your computer and use it in GitHub Desktop.
Прикручивание Donctrine ORM к сайлекс без сервиса
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__.'/../vendor/autoload.php'; | |
require_once __DIR__.'/boot.php'; | |
use Silex\Application; | |
use Symfony\Component\HttpFoundation\Request; | |
use Synfony\Component\HttpFoundation\Response; | |
$app = new Application(); | |
$app->get('/', function() use ($app, $em) { | |
$post = new App\Model\Post(); | |
$post->setBody('test'); | |
$post->setTitle('test'); | |
$em->persist($post); | |
$em->flush(); | |
return 'home page' ; | |
}); | |
$app->get('/all', function() use ($app, $em) { | |
$posts = $em->getRepository('App\Model\Post')->findAll(); | |
ob_start(); | |
print_r($posts); | |
$content = ob_get_contents(); | |
ob_end_clean(); | |
return $content; | |
}); | |
return $app; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Doctrine\ORM\Tools\Setup, | |
Doctrine\ORM\EntityManager, | |
Doctrine\Common\EventManager as EventManager, | |
Doctrine\ORM\Events, | |
Doctrine\ORM\Configuration, | |
Doctrine\Common\Cache\ArrayCache as Cache, | |
Doctrine\Common\Annotations\AnnotationRegistry, | |
Doctrine\Common\Annotations\AnnotationReader, | |
Doctrine\Common\ClassLoader;; | |
$loader = require __DIR__.'/../vendor/autoload.php'; | |
$loader->add('App', __DIR__.'/'); | |
//doctrine | |
$config = new Configuration(); | |
$config->setProxyDir('/tmp'); | |
$config->setProxyNamespace('EntityProxy'); | |
$config->setAutoGenerateProxyClasses(true); | |
AnnotationRegistry::registerFile(__DIR__. '/../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'); | |
$driver = new Doctrine\ORM\Mapping\Driver\AnnotationDriver( | |
new Doctrine\Common\Annotations\AnnotationReader(), | |
array(__DIR__ ) | |
); | |
$config->setMetadataDriverImpl($driver); | |
//getting the EntityManager | |
$em = EntityManager::create( | |
array( | |
'driver' => 'pdo_mysql', | |
'host' => 'localhost', | |
'port' => '3306', | |
'user' => 'root', | |
'password' => 'password', | |
'dbname' => 'silex', | |
), | |
$config | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment