0
使用CodeIgniter工作幾個月,newbe with Doctrine。 我https://github.com/wildlyinaccurate/CodeIgniter-2-with-Doctrine-2最近的工作爲基礎,定義與generate_classes()
功能(應用程序/庫/ Doctrine.php,調用一次構造函數結束之前,創建實體後發表評論吧)從MySQL表/實體列:Doctrine 2和codeigniter 2,如何使用生成的代碼
應用/libraries/Doctrine.php
<?php
use Doctrine\Common\ClassLoader,
Doctrine\ORM\Configuration,
Doctrine\ORM\EntityManager,
Doctrine\Common\Cache\ArrayCache,
Doctrine\DBAL\Logging\EchoSQLLogger,
Doctrine\ORM\Mapping\Driver\DatabaseDriver,
Doctrine\ORM\Tools\DisconnectedClassMetadataFactory,
Doctrine\ORM\Tools\EntityGenerator;
/**
* CodeIgniter Smarty Class
*
* initializes basic doctrine settings and act as doctrine object
*
* @final Doctrine
* @category Libraries
* @author Md. Ali Ahsan Rana
* @link http://codesamplez.com/
*/
class Doctrine
{
/**
* @var EntityManager $em
*/
public $em = null;
/**
* constructor
*/
public function __construct()
{
// load database configuration from CodeIgniter
require APPPATH . 'config/database.php';
// Set up class loading. You could use different autoloaders, provided by your favorite framework,
// if you want to.
require_once APPPATH . 'libraries/Doctrine/Common/ClassLoader.php';
$doctrineClassLoader = new ClassLoader('Doctrine', APPPATH . 'libraries');
$doctrineClassLoader->register();
$entitiesClassLoader = new ClassLoader('models', rtrim(APPPATH, "/"));
$entitiesClassLoader->register();
$proxiesClassLoader = new ClassLoader('Proxies', APPPATH . 'models');
$proxiesClassLoader->register();
// Set up caches
$config = new Configuration;
$cache = new ArrayCache;
$config->setMetadataCacheImpl($cache);
$driverImpl = $config->newDefaultAnnotationDriver(array(
APPPATH . 'models/Entities'
));
$config->setMetadataDriverImpl($driverImpl);
$config->setQueryCacheImpl($cache);
// Proxy configuration
$config->setProxyDir(APPPATH . 'models/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' => $db['default']['username'],
'password' => $db['default']['password'],
'host' => $db['default']['hostname'],
'dbname' => $db['default']['database']
);
// Create EntityManager
$this->em = EntityManager::create($connectionOptions, $config);
/* Generate entity objects automatically from mysql db tables. Run once.
* http://codesamplez.com/development/using-doctrine-with-codeigniter
*/
//$this->generate_classes();
}
/**
* generate entity objects automatically from mysql db tables
* @return none
*/
function generate_classes()
{
$this->em->getConfiguration()->setMetadataDriverImpl(new DatabaseDriver($this->em->getConnection()->getSchemaManager()));
$cmf = new DisconnectedClassMetadataFactory();
$cmf->setEntityManager($this->em);
$metadata = $cmf->getAllMetadata();
$generator = new EntityGenerator();
$generator->setUpdateEntityIfExists(true);
$generator->setGenerateStubMethods(true);
$generator->setGenerateAnnotations(true);
$generator->generate($metadata, APPPATH . "models/Entities");
}
}
生成的文件:
應用程序/模型/實體/ Customers.php
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* Customers
*
* @ORM\Table(name="customers")
* @ORM\Entity
*/
class Customers
{
/**
* @var integer $idCustomer
*
* @ORM\Column(name="id_customer", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idCustomer;
/**
* @var string $customerName
*
* @ORM\Column(name="customer_name", type="string", length=62, nullable=false)
*/
private $customerName;
/**
* @var CustomerAges
*
* @ORM\ManyToOne(targetEntity="CustomerAges")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_customer_age", referencedColumnName="id_customer_age")
* })
*/
private $idCustomerAge;
/**
* Get idCustomer
*
* @return integer
*/
public function getIdCustomer()
{
return $this->idCustomer;
}
/**
* Set customerName
*
* @param string $customerName
* @return Customer
*/
public function setCustomerName($customerName)
{
$this->customerName = $customerName;
return $this;
}
/**
* Get customerName
*
* @return string
*/
public function getCustomerName()
{
return $this->customerName;
}
/**
* Set idCustomerAge
*
* @param CustomerAges $idCustomerAge
* @return Customers
*/
public function setIdCustomerAge(\CustomerAges $idCustomerAge = null)
{
$this->idCustomerAge = $idCustomerAge;
return $this;
}
/**
* Get idCustomerAge
*
* @return CustomerAges
*/
public function getIdCustomerAge()
{
return $this->idCustomerAge;
}
}
應用程序/模型/實體/ Customer_ages.php
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* CustomerAges
*
* @ORM\Table(name="customer_Ages")
* @ORM\Entity
*/
class CustomerAges
{
/**
* @var integer $idCustomerAge
*
* @ORM\Column(name="id_customer_Age", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idCustomerAge;
/**
* @var integer $AgeNumber
*
* @ORM\Column(name="Age_number", type="integer", nullable=false)
*/
private $AgeNumber;
/**
* Get idCustomerAge
*
* @return integer
*/
public function getIdCustomerAge()
{
return $this->idCustomerAge;
}
/**
* Set AgeNumber
*
* @param integer $AgeNumber
* @return CustomerAges
*/
public function setAgeNumber($AgeNumber)
{
$this->AgeNumber = $AgeNumber;
return $this;
}
/**
* Get AgeNumber
*
* @return integer
*/
public function getAgeNumber()
{
return $this->AgeNumber;
}
}
編輯:我如何使用它CRUD operations與相關表?我知道,閱讀用戶指南,但(例如)如果$post = $entityManager->find("Customers", $idCustomer);
是單行,哪個函數/語法返回所有行(和相關)?
這不是教義2.教義2有一個完全不同的方式來定義實體.. – Broncha
@Broncha謝謝你,編輯和改進的問題。 – quantme
您應該真正閱讀用戶指南和doctrine的文檔。您可以實現用於處理多行的存儲庫。請參閱http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#by-simple-conditions – Broncha