Zend Framework2使用Faq随记

69 篇文章 0 订阅
44 篇文章 0 订阅

1. 初始文件

   1.1 /init_autoload.php  -----------------------------------

  // Composer autoloading
if (file_exists('vendor/autoload.php')) {
    $loader = include 'vendor/autoload.php';
}

if (defined('LIB')) {
    include LIB . '/Zend/Loader/AutoloaderFactory.php';
    Zend\Loader\AutoloaderFactory::factory(array(
        'Zend\Loader\StandardAutoloader' => array(
        'autoregister_zf' => true
        )
    ));
}

if (!class_exists('Zend\Loader\AutoloaderFactory')) {
    throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.');
}


  1.2 /public/index.php  ----------------------------------

    date_default_timezone_set("UTC");

/**
 * This makes our life easier when dealing with paths. Everything is relative
 * to the application root now.
 */
chdir(dirname(__DIR__));
//chdir(__DIR__);

if (!defined('APP_PATH'))
    define ('APP_PATH', dirname(__DIR__).'/');
if (!defined('LIB'))
    define('LIB', APP_PATH . 'vendor/ZF2/library');

// Setup autoloading
require APP_PATH.'init_autoloader.php';

// Run the application!
Zend\Mvc\Application::init(require APP_PATH.'config/application.config.php')->run();


2. 全局变量:不要直接定义全局变量,而应在config/global.php 或 local.php 配置,如

return array(
    'service_manager' => array(
        'factories' => array(
            //'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
            'Zend\Db\Adapter\Adapter' => function ($serviceManager) {
                $adapterFactory = new Zend\Db\Adapter\AdapterServiceFactory();
                $adapter = $adapterFactory->createService($serviceManager);
                \Zend\Db\TableGateway\Feature\GlobalAdapterFeature::setStaticAdapter($adapter);
                return $adapter;
         }
        ),
    ),

    'db' => array(
        'driver'    => 'pdo',
        'dsn'       => 'mysql:dbname=testdb;host=localhost',
        'username'  => 'root',
        'password'  => '',
    ),
    'msg' => array(
        'add'    => 'Data Inserted Successfully',
        'edit'   => 'Data Updated Successfully',
        'delete' => 'Data Deleted Successfully',
    ),
);
?>


Controller File:DemoController.php

<?php
  namespace Demo\Controller;
  use Zend\Mvc\Controller\AbstractActionController;
    class DemoController extends AbstractActionController
    { 
       public function indexAction($cms_page_name='whyus')
        { 

            /*Call config file to fetch current cms page id-- fetch config file from database.local.php*/
            $config = $this->getServiceLocator()->get('Config');
            $all_msg = $config['msg'];
       }
    }
?>


3.使用数据表前缀

   3.1 方法一, 在操作表时设置

  public function __construct(Adapter $adapter, UserEntityInterface $entity)
    {
        $resultSet = new ResultSet();
        $resultSet->setArrayObjectPrototype($entity);
        $config = ....??
        
        parent::__construct($config['db']['prefix'].'users', $adapter, null, $resultSet);
    }



 3.2 方法二,全局配置前缀

==== 创建类 TableNamePrefixFeature ======

class TableNamePrefixFeature extends Zend\Db\TableGateway\Feature\AbstractFeature
{
    protected 
$prefix null;
    
    public function 
__construct($prefix null)
    {
        if (
null !== $prefix) {
            
$this->setPrefix($prefix);
        }
    }
    
    public function 
setPrefix($prefix)
    {
        
$this->prefix $prefix;
    }
    
    public function 
postInitialize()
    {
        if (
null !== $this->prefix) {
            
$this->tableGateway->getSql()->setTable($this->prefix $this->tableGateway->table);
        }
    }
}

==== 使用类TableNamePrefixFeature=========

$adapter = new Zend\Db\Adapter\Adapter(array(
    
'driver'   => 'Mysqli',
    
'database' => 'example',
    
'username' => '',
    
'password' => ''
));

$feature = new TableNamePrefixFeature('foo_');

$tableGateway = new Zend\Db\TableGateway\TableGateway('bar'$adapter$feature);
var_dump($tableGateway->getSql()->getTable()); // string 'foo_bar' (length=7) 


 

$dbParams = array(
    'database'  => 'wlm',
    ...
    'prefix'    => 'wlm_'
);

return array(
    'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter' => function ($sm) use ($dbParams) {
                return new Zend\Db\Adapter\Adapter(array(
                          ...
                ));
            },
            'Zend\Db\TableGateway' => function ($sm) use ($dbParams) {                
                $dbFeature = new TableNamePrefixFeature($dbParams['prefix']);
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                return new Zend\Db\TableGateway\TableGateway('bar', $dbAdapter, $dbFeature);
            },
        ),
    ),
);







评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值