使用CodeIgniter工作几个月,newbe与Doctrine一起工作。
我最近使用https://github.com/wildlyinaccurate/CodeIgniter-2-with-Doctrine-2作为基础,并使用generate_classes()
函数( application / libraries / Doctrine.php)在mysql表/列中定义实体,在构造函数结束前调用一次,在创建实体后对其进行注释):
应用/库/ 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);
是单行,哪个函数/语法返回所有行(和相关的)?
答案 0 :(得分:1)
以下代码应该为您提供表中的所有行。
$em->getRepository('Customers')->findAll();