首頁 > PHP, Zend Framework > Zend Framework QuickStart MySQL version

Zend Framework QuickStart MySQL version

2011年1月20日 發表評論 閱讀評論

由於官網的 QuickStart 是用 sqlite 來做示範,是檔案式的 DB,但我想直接以 MySQL 來做,因此找到了一篇 「zend – 建model與database 連結」的文章,參考的是 db-adapter 這段

先在 phpMyAdmin 增加 database 以及 user

database : rdlab_guestbook (成品用)
database : rdlab_guestbook_testing (測試用)
database : rdlab_guestbook_development (開發用)
user : username
password : 11111111

設定權限讓 username 都可以存取上面三個 database

設定成品用的database參數:
andy@nb3vm:~/www/rdlab/application$ zf configure db-adapter ‘adapter=PDO_MYSQL&dbname="rdlab_guestbook"&host="localhost"&username="username"&password="11111111″‘ production

設定測試用的database:
andy@nb3vm:~/www/rdlab/application$ zf configure db-adapter ‘adapter=PDO_MYSQL&dbname="rdlab_guestbook_testing"‘ testing

設定開發用的database:
andy@nb3vm:~/www/rdlab/application$ zf configure db-adapter ‘adapter=PDO_MYSQL&dbname="rdlab_guestbook_development"‘ development

設定好後,會將參數設定在 「rdlab/application/configs/application.ini」裡
之後要做修改就直接來這裡修改,或是有其他專案要建立,來這裡 copy過去就好了!

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH “/../library"
bootstrap.path = APPLICATION_PATH “/Bootstrap.php"
bootstrap.class = “Bootstrap"
appnamespace = “Application"
resources.frontController.controllerDirectory = APPLICATION_PATH “/controllers"
resources.frontController.params.displayExceptions = 0

resources.layout.layoutPath = APPLICATION_PATH “/layouts/scripts"
resources.view[] =
resources.db.adapter = “PDO_MYSQL"
resources.db.params.dbname = “rdlab_guestbook"
resources.db.params.host = “localhost"
resources.db.params.username = “username"
resources.db.params.password = “11111111″

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.db.adapter = “PDO_MYSQL"
resources.db.params.dbname = “rdlab_guestbook_testing"

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
resources.db.adapter = “PDO_MYSQL"
resources.db.params.dbname = “rdlab_guestbook_development"

再來就是要建立資料表了
依照官網的步驟所建立的是 sqlite 的檔案式資料表
而我要直接在mysql裡建立,就可以先忽略建立 schema 的步驟,但新增好 table 後,是可以把 schema 匯出,在移植到別的主機時就可以不用重 key !

底下是我的 schema 語法:
存在「rdlab/scripts/schema.mysql.sql」

DROP TABLE IF EXISTS guestbook;
CREATE TABLE IF NOT EXISTS guestbook (
    id int(11) NOT NULL AUTO_INCREMENT,
    email varchar(32) COLLATE utf8_unicode_ci NOT NULL,
    `comment` longtext COLLATE utf8_unicode_ci NOT NULL,
    created int(11) DEFAULT NULL,
    PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE INDEX "id" ON "guestbook" ("id");

另外再做個 data 匯入的 sql:
存在「rdlab/scripts/data.mysql.sql」

INSERT INTO guestbook (email, comment, created) VALUES
    ('ralph.schindler@zend.com',
    'Hello! Hope you enjoy this sample zf application!',
    NOW());

INSERT INTO guestbook (email, comment, created) VALUES
    ('foo@bar.com',
    'Baz baz baz, baz baz Baz baz baz - baz baz baz.',
    NOW());

有了上面的兩個scripts後
就取用官網範例的script,把schema和data的檔名改成上述的兩個檔名,再把裡面的 chmod 註記不要執行
其他不用修改,並且存成「rdlab/scripts/load.mysql.php」,如下:

<?php
// scripts/load.mysql.php
/**
* Script for creating and loading database
*/
// Initialize the application path and autoloading
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
set_include_path(implode(PATH_SEPARATOR, array(
    APPLICATION_PATH . '/../library',
    get_include_path(),
)));
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
// Define some CLI options
$getopt = new Zend_Console_Getopt(array(
    'withdata|w' => 'Load database with sample data',
    'env|e-s'    => 'Application environment for which to create database (defaults to development)',
    'help|h'     => 'Help -- usage message',
));
try {
    $getopt->parse();
} catch (Zend_Console_Getopt_Exception $e) {
    // Bad options passed: report usage
    echo $e->getUsageMessage();
    return false;
}
// If help requested, report usage message
if ($getopt->getOption('h')) {
    echo $getopt->getUsageMessage();
    return true;
}
// Initialize values based on presence or absence of CLI options
$withData = $getopt->getOption('w');
$env      = $getopt->getOption('e');
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (null === $env) ? 'development' : $env);
// Initialize Zend_Application
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);
// Initialize and retrieve DB resource
$bootstrap = $application->getBootstrap();
$bootstrap->bootstrap('db');
$dbAdapter = $bootstrap->getResource('db');
// let the user know whats going on (we are actually creating a
// database here)
if ('testing' != APPLICATION_ENV) {
    echo 'Writing Database Guestbook in (control-c to cancel): ' . PHP_EOL;
    for ($x = 5; $x > 0; $x--) {
        echo $x . "\r"; sleep(1);
    }
}
// Check to see if we have a database file already
$options = $bootstrap->getOption('resources');
$dbFile  = $options['db']['params']['dbname'];
if (file_exists($dbFile)) {
    unlink($dbFile);
}
// this block executes the actual statements that were loaded from
// the schema file.
try {
    $schemaSql = file_get_contents(dirname(__FILE__) . '/schema.mysql.sql');
    // use the connection directly to load sql in batches
    $dbAdapter->getConnection()->exec($schemaSql);
    //chmod($dbFile, 0666);
    if ('testing' != APPLICATION_ENV) {
        echo PHP_EOL;
        echo 'Database Created';
        echo PHP_EOL;
  }
  if ($withData) {
        $dataSql = file_get_contents(dirname(__FILE__) . '/data.mysql.sql');
        // use the connection directly to load sql in batches
        $dbAdapter->getConnection()->exec($dataSql);
        if ('testing' != APPLICATION_ENV) {
            echo 'Data Loaded.';
            echo PHP_EOL;
        }
  }
} catch (Exception $e) {
    echo 'AN ERROR HAS OCCURED:' . PHP_EOL;
    echo $e->getMessage() . PHP_EOL;
    return false;
}
// generally speaking, this script will be run from the command line
return true;
?>

存好後,執行一下 scripts 以驗證 table 並且匯入 data

andy@nb3vm:~/www/rdlab/scripts$ php load.mysql.php –withdata
Writing Database Guestbook in (control-c to cancel):
1
Database Created
Data Loaded.

要是有任何錯誤,就要參考錯誤訊息來修改嘍!

有了 table 和 data
接著要在專案裡建立相關的物件
再次使用 zf 指令!

andy@nb3vm:~/www/rdlab/application$ zf create db-table Guestbook guestbook

Creating a DbTable at application/models/DbTable/Guestbook.php
Updating project profile ‘zfproject.xml’

他會產生 rdlab/application/models/DbTable/Guestbook.php 這個檔

<?php

/**
* This is the DbTable class for the guestbook table.
*/
class Application_Model_DbTable_Guestbook extends Zend_Db_Table_Abstract
{
    /** Table name */
    protected $_name = 'guestbook';

}

然後為了要有 data mapper,得再執行

andy@nb3vm:~/www/rdlab/application$ zf create model GuestbookMapper

Creating a model at application/models/GuestbookMapper.php
Updating project profile ‘.zfproject.xml’

會存成 rdlab/application/models/GuestbookMapper.php
我們繼續編輯:

<?php

class Application_Model_GuestbookMapper
{
    protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_Guestbook');
        }
        return $this->_dbTable;
    }

    public function save(Application_Model_Guestbook $guestbook)
    {
        $data = array(
            'email'   => $guestbook->getEmail(),
            'comment' => $guestbook->getComment(),
            'created' => date('Y-m-d H:i:s'),
        );

        if (null === ($id = $guestbook->getId())) {
            unset($data['id']);
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('id = ?' => $id));
        }
    }

    public function find($id, Application_Model_Guestbook $guestbook)
    {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return;
        }
        $row = $result->current();
        $guestbook->setId($row->id)
                  ->setEmail($row->email)
                  ->setComment($row->comment)
                  ->setCreated($row->created);
    }

    public function fetchAll()
    {
        $resultSet = $this->getDbTable()->fetchAll();
        $entries   = array();
        foreach ($resultSet as $row) {
            $entry = new Application_Model_Guestbook();
            $entry->setId($row->id)
                  ->setEmail($row->email)
                  ->setComment($row->comment)
                  ->setCreated($row->created);
            $entries[] = $entry;
        }
        return $entries;
    }
}

接著再建立 model

andy@nb3vm:~/www/rdlab/application$ zf create model Guestbook

Creating a model at application/models/Guestbook.php

Updating project profile ‘.zfproject.xml’

就會產生 rdlab/application/models/Guestbook.php
我們再編輯成如下:

<?php
// application/models/Guestbook.php

class Application_Model_Guestbook

{
    protected $_comment;
    protected $_created;
    protected $_email;
    protected $_id;

    public function __construct(array $options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
        }
    }

    public function __set($name, $value)
    {
        $method = 'set' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid guestbook property');
        }
        $this->$method($value);
    }

    public function __get($name)
    {
        $method = 'get' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid guestbook property');
        }
        return $this->$method();
    }

    public function setOptions(array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }

    public function setComment($text)
    {
        $this->_comment = (string) $text;
        return $this;
    }

    public function getComment()
    {
        return $this->_comment;
    }

    public function setEmail($email)
    {
        $this->_email = (string) $email;
        return $this;
    }

    public function getEmail()
    {
        return $this->_email;
    }

    public function setCreated($ts)
    {
        $this->_created = $ts;
        return $this;
    }

    public function getCreated()
    {
        return $this->_created;
    }

    public function setId($id)
    {
        $this->_id = (int) $id;
        return $this;
    }

    public function getId()
    {
        return $this->_id;
    }
}

接著就是做個 controller,使用 zf create controller 這個指令

andy@nb3vm:~/www/rdlab/application$ zf create controller Guestbook

Creating a controller at
application/controllers/GuestbookController.php
Creating an index action method in controller Guestbook
Creating a view script for the index action method at
application/views/scripts/guestbook/index.phtml
Creating a controller test file at
tests/application/controllers/GuestbookControllerTest.php
Updating project profile ‘.zfproject.xml’

會產生 rdlab/application/controllers/GuestbookController.php 這個 controller
我們修改如下:

<?php

class GuestbookController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
        $guestbook = new Application_Model_GuestbookMapper();
        $this->view->entries = $guestbook->fetchAll();
    }
}

最後再把 view 產生出來以顯示畫面,參考官網如下:
rdlab/application/views/scripts/guestbook/index.phtml

<!-- application/views/scripts/guestbook/index.phtml -->

<p><a href="<?php echo $this->url(
    array(
        'controller' => 'guestbook',
        'action'     => 'sign'
    ),
    'default',
    true) ?>">Sign Our Guestbook</a></p>

Guestbook Entries: <br />
<dl>
    <?php foreach ($this->entries as $entry): ?>
    <dt><?php echo $this->escape($entry->email) ?></dt>
    <dd><?php echo $this->escape($entry->comment) ?></dd>
    <?php endforeach ?>
</dl>

原則上就大工告成嘍!

Categories: PHP, Zend Framework Tags:
  1. 目前尚無任何的評論。