camna / codeigniter-doctrine
该包最新版本(1.x-dev)没有提供许可证信息。
Doctrine ORM 的 CodeIgniter 包装器
1.x-dev
2014-09-01 16:03 UTC
Requires
- composer/installers: ~1.0
- doctrine/orm: 2.3.*
This package is not auto-updated.
Last update: 2024-09-23 11:27:38 UTC
README
这是一个 CodeIgniter 的包装器库,用于 Doctrine ORM。
安装
如果您使用 Composer,请在您的 composer.json 文件中添加以下代码,并运行 composer install
。
{ "require": { "camna/codeigniter-doctrine": "1.*@dev" } }
使用方法
在 application/models
中创建一个名为 entities
的新文件夹。您将在此存储所有 Doctrine 实体。这些实体几乎与现有的 CodeIgniter 模型相同,除了少数例外。
以下是一个示例实体
application/models/entities/rsvp.php
<?php use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; /** * RSVP * * @Table(name="rsvp") * @Entity */ class RSVP extends Doctrine_entity { /** * @var integer * * @Column(name="rsvp_id", type="integer") * @Id * @GeneratedValue(strategy="AUTO") */ private $id; /** * @var boolean * * @Column(name="attending", type="boolean") */ private $attending; /** * @var string * * @Column(name="name", type="string", length=255) */ private $name; function __construct() { parent::__construct(); } /** * Get id * * @return integer */ public function getID() { return $this->id; } /** * Set attending * * @param boolean $attending * @return RSVP */ public function setAttending($attending) { $this->attending = $attending; return $this; } /** * Get attending * * @return boolean */ public function getAttending() { return $this->attending; } /** * Set name * * @param string $name * @return RSVP */ public function setName($name) { $this->name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->name; } }
首先,每个实体中都需要有 use
语句。其次,类需要扩展 Doctrine_entity
。这将允许它继承所有标准 Doctrine 函数。
您需要运行以下命令以在数据库中生成模式
php application/libraries/doctrine/console orm:schema-tool:create
使用 php application/libraries/doctrine/console
将在运行任何 Doctrine CLI 命令之前加载 CodeIgniter 数据库配置。
从您的控制器或模型中,您可以如此保存数据
// Load the required libraries. $this->load->library('doctrine'); $this->load->library('doctrine/doctrine_entity'); $this->load->model('entities/rsvp'); // Create a generic RSVP object. $rsvp = new RSVP(); $rsvp->setName('David Barratt'); $rsvp->setAttending(FALSE); // Persist the object, $this->doctrine->em->persist($rsvp); // Flush Doctrine, this is where the SQL Insert is performed. $this->doctrine->em->flush();
您可以如此查询数据
// Load the required libraries. $this->load->library('doctrine'); $this->load->library('doctrine/doctrine_entity'); $this->load->model('entities/rsvp'); // Load the RSVP Object with an ID of 3 $rsvp = $this->rsvp->repository->find(3); // Get the Name $name = $rsvp->getName();
如果您想更新对象(上面),您可以这样做
// Set the Name $name->setName('Jon Stewart'); // Flush Doctrine, this is where the SQL Update is performed. $this->doctrine->em->flush();
有关更多信息,请参阅 Doctrine ORM 文档