POSTS
Doctrine 入門(一)
準備
依照之前整合的 Zend Framework 1.11.0 and Doctrine 1.2.3 and Smarty 3.0.4 環境,來做學習與測試.
更改index.php
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"
接下來試試基本運作,以下是顯示目前連線.
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
* @version SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $
*/
class User extends BaseUser
{
}
UserTable.php
BaseUser.php
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資料表這邊就不另外測試了.