Doctrine 入門(一)
by admin on 十二月.02, 2010, under Doctrine, Zend Framework
準備
依照之前整合的 Zend Framework 1.11.0 and Doctrine 1.2.3 and Smarty 3.0.4 環境,來做學習與測試.
更改index.php
<?php set_include_path(implode(PATH_SEPARATOR, array( realpath(APPLICATION_PATH . '/../library'), realpath(APPLICATION_PATH . '/../models'), realpath(APPLICATION_PATH . '/../models/generated'), get_include_path(), )));
application.ini 環境
;Doctrine autoloaderNamespaces[] = "Doctrine_" pluginPaths.ZendX_Doctrine_Application_Resource = "ZendX/Application/Resource" resources.doctrine.debug = 1 resources.doctrine.paths.models_path = APPLICATION_PATH "/../models" resources.doctrine.connections.orm1.dsn.adapter = mysql resources.doctrine.connections.orm1.dsn.user = "user" resources.doctrine.connections.orm1.dsn.pass = "password" resources.doctrine.connections.orm1.dsn.hostspec = "localhost" resources.doctrine.connections.orm1.dsn.database = "orm1_sd_idv_tw"
接下來試試基本運作,以下是顯示目前連線.
<?php class IndexController extends Zend_Controller_Action { public function init() { /* Initialize action controller here */ } public function indexAction() { // doctrine $manager = Doctrine_Manager::getInstance(); $conns = $manager->getConnections(); foreach ($conns as $conn) { echo $conn->getName() . "\n"; } } }
執行結果會顯示orm1
接下來介紹 Models
先增加 範例資料表
CREATE TABLE USER ( id BIGINT(20) NOT NULL AUTO_INCREMENT, first_name VARCHAR(255) DEFAULT NULL, last_name VARCHAR(255) DEFAULT NULL, username VARCHAR(255) DEFAULT NULL, password VARCHAR(255) DEFAULT NULL, TYPE VARCHAR(255) DEFAULT NULL, is_active tinyint(1) DEFAULT '1', is_super_admin tinyint(1) DEFAULT '0', created_at TIMESTAMP, updated_at TIMESTAMP, PRIMARY KEY (id) ) ENGINE=InnoDB
使用 generateModelsFromDb 從db產生models檔案
$manager = Doctrine_Manager::getInstance(); $conns = $manager->getCurrentConnection(); //取的目前連線 Doctrine_Core::generateModelsFromDb(APPLICATION_PATH . '/../models', array('orm1'), array('generateTableClasses' => true));
執行一次,你會發現檔案自動產生.

來看看檔案
User.php
<?php /** * User * * This class has been auto-generated by the Doctrine ORM Framework * * @package ##PACKAGE## * @subpackage ##SUBPACKAGE## * @author ##NAME## <##EMAIL##> * @version SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $ */ class User extends BaseUser { }
UserTable.php
<?php /** * UserTable * * This class has been auto-generated by the Doctrine ORM Framework */ class UserTable extends Doctrine_Table { /** * Returns an instance of this class. * * @return object UserTable */ public static function getInstance() { return Doctrine_Core::getTable('User'); } }
BaseUser.php
<?php // Connection Component Binding Doctrine_Manager::getInstance()->bindComponent('User', 'orm1'); /** * BaseUser * * This class has been auto-generated by the Doctrine ORM Framework * * @property integer $id * @property string $first_name * @property string $last_name * @property string $username * @property string $password * @property string $type * @property integer $is_active * @property integer $is_super_admin * @property timestamp $created_at * @property timestamp $updated_at * * @package ##PACKAGE## * @subpackage ##SUBPACKAGE## * @author ##NAME## <##EMAIL##> * @version SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $ */ abstract class BaseUser extends Doctrine_Record { public function setTableDefinition() { $this->setTableName('user'); $this->hasColumn('id', 'integer', 8, array( 'type' => 'integer', 'length' => 8, 'fixed' => false, 'unsigned' => false, 'primary' => true, 'autoincrement' => true, )); $this->hasColumn('first_name', 'string', 255, array( 'type' => 'string', 'length' => 255, 'fixed' => false, 'unsigned' => false, 'primary' => false, 'notnull' => false, 'autoincrement' => false, )); $this->hasColumn('last_name', 'string', 255, array( 'type' => 'string', 'length' => 255, 'fixed' => false, 'unsigned' => false, 'primary' => false, 'notnull' => false, 'autoincrement' => false, )); $this->hasColumn('username', 'string', 255, array( 'type' => 'string', 'length' => 255, 'fixed' => false, 'unsigned' => false, 'primary' => false, 'notnull' => false, 'autoincrement' => false, )); $this->hasColumn('password', 'string', 255, array( 'type' => 'string', 'length' => 255, 'fixed' => false, 'unsigned' => false, 'primary' => false, 'notnull' => false, 'autoincrement' => false, )); $this->hasColumn('type', 'string', 255, array( 'type' => 'string', 'length' => 255, 'fixed' => false, 'unsigned' => false, 'primary' => false, 'notnull' => false, 'autoincrement' => false, )); $this->hasColumn('is_active', 'integer', 1, array( 'type' => 'integer', 'length' => 1, 'fixed' => false, 'unsigned' => false, 'primary' => false, 'default' => '1', 'notnull' => false, 'autoincrement' => false, )); $this->hasColumn('is_super_admin', 'integer', 1, array( 'type' => 'integer', 'length' => 1, 'fixed' => false, 'unsigned' => false, 'primary' => false, 'default' => '0', 'notnull' => false, 'autoincrement' => false, )); $this->hasColumn('created_at', 'timestamp', null, array( 'type' => 'timestamp', 'fixed' => false, 'unsigned' => false, 'primary' => false, 'notnull' => true, 'autoincrement' => false, )); $this->hasColumn('updated_at', 'timestamp', null, array( 'type' => 'timestamp', 'fixed' => false, 'unsigned' => false, 'primary' => false, 'default' => '0000-00-00 00:00:00', 'notnull' => true, 'autoincrement' => false, )); } public function setUp() { parent::setUp(); } }
可以看到由generateModelsFromDb所產生的Models檔案已經被定義好了.
例:加上密碼為md5.
class User extends BaseUser { public function setPassword($password) { return $this->_set('password', md5($password)); } }
測試:
$user = new User(); $user->username = 'jwage'; $user->password = 'changeme'; echo $user->password;
例:取的時間資料自動轉換時間格式.
class UserTable extends Doctrine_Table { public function getCreatedToday() { $today = date('Y-m-d h:i:s', strtotime(date('Y-m-d'))); return $this->createQuery('u') ->where('u.created_at > ?', $today) ->execute(); } }
測試:
$usersCreatedToday = Doctrine_Core::getTable('User')->getCreatedToday(); echo $usersCreatedToday . "\n";
Doctrine 支援YAML 格式來定義Models資料表這邊就不另外測試了.


六月 8th, 2011 on 22:34:36
Hello,
I’m not writing in chinese because i’m french
Thanks for sharing this nice informations!
I have some problems with magic accessors which aren’t called in models/Users.php
I must do : $user->setPassword($password)
And with the test getCreatedToday(), i get "An error occurred"
Have you an idea
Again Thank you
六月 9th, 2011 on 14:12:15
idon’t know how to help you .
Would you open debug message to looking.