Skip to content

Instantly share code, notes, and snippets.

@khenam
Forked from bluecloudy/yiidoctrine2_LICENSE
Created July 3, 2016 19:43
Show Gist options
  • Save khenam/901b4d1ca2267fe7cccb991ff781cf84 to your computer and use it in GitHub Desktop.
Save khenam/901b4d1ca2267fe7cccb991ff781cf84 to your computer and use it in GitHub Desktop.
This is an extension for the Yii Framework that integrates Doctrine 2 ORM & ODM projects
<?php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
Yii::import('YiiDoctrine.repositories.*');
Yii::import('YiiDoctrine.controllers.*');
class YDComponent extends CApplicationComponent
{
private $em = null;
private $basePath;
private $proxyPath;
private $entityPath;
private $db;
public function init()
{
// Add alias path
Yii::setPathOfAlias('Doctrine', $this->getBasePath() . '/vendor/doctrine');
Yii::setPathOfAlias('Symfony', $this->getBasePath() . '/vendor/Symfony');
// Init DoctrineORM
$this->initDoctrine();
parent::init();
}
public function initDoctrine()
{
$cache = new Doctrine\Common\Cache\FilesystemCache($this->getBasePath() . '/../public/cache');
$config = Setup::createAnnotationMetadataConfiguration($this->entityPath, true);
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
$config->setProxyDir($this->getProxyPath());
$config->setProxyNamespace('Proxies');
$config->setAutoGenerateProxyClasses(true);
$this->em = EntityManager::create($this->db, $config);
}
public function setBasePath($basePath)
{
$this->basePath = $basePath;
}
public function getBasePath()
{
return $this->basePath;
}
public function setEntityPath($entityPath)
{
$this->entityPath = $entityPath;
}
public function getEntityPath()
{
return $this->entityPath;
}
public function setProxyPath($proxyPath)
{
$this->proxyPath = $proxyPath;
}
public function getProxyPath()
{
return $this->proxyPath;
}
public function setDb($info)
{
$this->db = $info;
}
public function getDb()
{
return $this->db;
}
/**
* @return EntityManager
*/
public function getEntityManager()
{
return $this->em;
}
}
{
"name": "bluecloudy/yiidoctrine2",
"type": "library",
"description": "This is an extension for the Yii Framework that integrates Doctrine 2 ORM & ODM projects ",
"keywords": ["Extension", "Doctrine", "Yii", "ORM"],
"homepage": "https://github.com/bluecloudy/yiidoctrine2",
"license": "MIT",
"authors": [
{
"name": "Giang Nguyen",
"email": "tn.hoanggiang@gmail.com"
}
],
"require": {
"php": ">=5.3.2",
"doctrine/orm": "2.4.*",
"symfony/yaml": "2.*"
}
}
<?php
/**
* Created by PhpStorm.
* Author: Giang Nguyen
* Email: tn.hoanggiang@gmail.com
* Date: 5/4/14
* Time: 3:07 PM
*/
use Symfony\Component\Console\Helper\HelperSet;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
// Get EntityManager
$entityManager = Yii::app()->doctrine->getEntityManager();
/**
* @var Doctrine\ORM\EntityManager $entityManager
*/
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($entityManager->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($entityManager)
));
$commands = array();
if (!($helperSet instanceof HelperSet))
{
foreach ($GLOBALS as $helperSetCandidate)
{
if ($helperSetCandidate instanceof HelperSet)
{
$helperSet = $helperSetCandidate;
break;
}
}
}
\Doctrine\ORM\Tools\Console\ConsoleRunner::run($helperSet, $commands);
<?php
/**
* Controller is the customized base controller class.
* All controller classes for this application should extend from this base class.
*/
class YiiDoctrine2Controller extends CController
{
/**
* @var Doctrine\ORM\EntityManager $entityManager
*/
private $entityManager = null;
/**
* @return CHttpRequest
*/
public function getRequest(){
return Yii::app()->request;
}
/**
* @return Doctrine\ORM\EntityManager
*/
public function getEntityManager()
{
if(is_null($this->entityManager)){
$this->entityManager = Yii::app()->doctrine->getEntityManager();
}
return $this->entityManager;
}
/**
* @return BaseRepository
*/
public function getRepository(){
$class = get_class($this);
return $this->getEntityManager()->getRepository(substr($class, 0, (strlen($class) - 10)));
}
}
The MIT License (MIT)
Copyright (c) 2014 bluecloudy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Yii Doctrine 2

This is an extension for the Yii Framework that integrates Doctrine 2 ORM & ODM projects

<?php
use Doctrine\ORM\EntityRepository;
abstract class YDBaseRepository extends EntityRepository implements IDataProvider
{
protected $_id;
private $_data;
private $_keys;
private $_totalItemCount;
private $_sort;
private $_pagination;
public $modelClass;
public $model;
public $keyAttribute;
private $_criteria;
private $_countCriteria;
public $data;
abstract protected function fetchData();
abstract protected function calculateTotalItemCount();
public function getId(){ }
public function getPagination($className='CPagination'){ }
public function setPagination($value){ }
public function setSort($value){ }
public function getData($refresh=false){ }
public function setData($value){ }
public function getKeys($refresh=false){ }
public function setKeys($value){ }
public function getItemCount($refresh=false){ }
public function getTotalItemCount($refresh=false){ }
public function setTotalItemCount($value){ }
public function getCriteria(){ }
public function setCriteria($value){ }
public function getCountCriteria(){ }
public function setCountCriteria($value){ }
public function getSort($className='CSort'){ }
protected function fetchKeys(){ }
private function _getSort($className){ }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment