2015-07-06 23 views
6

我很努力地推動連接到數據庫。我創建使用propel reverse "..."映射類和表圖,並創建了以下結構:推進 - 沒有爲數據庫「默認」定義連接

Solution Structure

propel.ini

[propel] 

# A better Pluralizer 
propel.builder.pluralizer.class = builder.util.StandardEnglishPluralizer 

propel.xml

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?> 
<config> 
    <propel> 
     <general> 
      <project>PHP-PlayArea</project> 
      <version>2.0.0-dev</version> 
     </general> 
     <database> 
      <connections> 
       <connection id="default"> 
        <adapter>mysql</adapter> 
        <classname>Propel\Runtime\Connection\ConnectionWrapper</classname> 
        <dsn>mysql:host=localhost;dbname=test</dsn> 
        <user>me</user> 
        <password>blahblahblah</password> 
        <settings> 
         <charset>utf8</charset> 
        </settings> 
       </connection> 
      </connections> 
     </database> 
     <runtime> 
      <defaultConnection>default</defaultConnection> 
      <connection>default</connection> 
     </runtime> 
     <generator> 
      <defaultConnection>default</defaultConnection> 
      <connection>default</connection> 
     </generator> 
    </propel> 
</config> 

行走。 php

<?php namespace propel; 

use Propel\Runtime\Propel; 

Propel::init("../propel/propel.xml"); 

我已經得到了以下的單元測試落在了:

// Include the main Propel script 
require_once '../propel/Propel.php'; 

require_once '../propel/Base/Users.php'; 
require_once '../propel/Map/UsersTableMap.php'; 
require_once '../propel/Users.php'; 

use propel\Users; 

const name = 'gareth'; 

class PropelTests extends \PHPUnit_Framework_TestCase { 
    public function testAddUser() 
    { 
     // create a user ? 
     $user = new Users(); 
     // brings back an empty config 
     $manager = new ConfigurationManager(); 
     //Get the array of runtime configured connections 
     $connections = $manager->get(); 

     // *** fails here *** 
     // test connections 
     $con = Propel::getWriteConnection(UsersTableMap::DATABASE_NAME); 
     $con = Propel::getReadConnection(UsersTableMap::DATABASE_NAME); 

輸出;

C:\wamp\bin\php\php5.5.12\php.exe -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1 C:\Users\gareth\AppData\Local\Temp\ide-phpunit.php --no-configuration Tests\PropelTests C:\Development\PHP-PlayArea\Tests\Propel.Tests.php 
Testing started at 11:40 ... 
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?> 
<config> 
    <propel> 
     <!-- full config as above --> 
    </propel> 
</config> 

No connection defined for database "default". Did you forget to define a connection or is it wrong written? 
C:\Development\PHP-PlayArea\vendor\propel\propel\src\Propel\Runtime\ServiceContainer\StandardServiceContainer.php:279 
C:\Development\PHP-PlayArea\vendor\propel\propel\src\Propel\Runtime\ServiceContainer\StandardServiceContainer.php:355 
C:\Development\PHP-PlayArea\propel\Base\Users.php:655 
C:\Development\PHP-PlayArea\Tests\Propel.Tests.php:29 

任何想法?我有點難倒...我的配置看起來不錯,但它顯然不是。

更新:2015年7月6日13:01:調試這之後,它看起來像它的炸彈,因爲沒有連接管理器,可以發現

Runtime propel variables

+0

從來沒有使用過,但推進你檢查路徑與'行走::初始化(在初始化'/路徑/到/我/文件');'? –

回答

5

我只是在學習Propel和我有完全相同的問題。

我試圖做你在這裏做什麼:

Propel::init("../propel/propel.xml"); 

所有這一切做的是包括文件和打印內容,就像你在你的輸出的輸出。

我終於得到它通過有效地取代你的Propel.php工作:

<?php 

use Propel\Common\Config\ConfigurationManager; 
use Propel\Runtime\Connection\ConnectionManagerSingle; 
use Propel\Runtime\Propel; 

// Load the configuration file 
$configManager = new ConfigurationManager('../propel/propel.xml'); 

// Set up the connection manager 
$manager = new ConnectionManagerSingle(); 
$manager->setConfiguration($configManager->getConnectionParametersArray()[ 'default' ]); 
$manager->setName('default'); 

// Add the connection manager to the service container 
$serviceContainer = Propel::getServiceContainer(); 
$serviceContainer->setAdapterClass('default', 'mysql'); 
$serviceContainer->setConnectionManager('default', $manager); 
$serviceContainer->setDefaultDatasource('default'); 

希望這有助於

+0

太棒了!非常感謝你。我現在有一個ConnectionManager來玩。 – wonea

1

你缺少在下面的端口號line in propel.xml:

<dsn>mysql:host=localhost:<<port number>>;dbname=test</dsn> 

如果您已從現有模式進行反向工程,請檢查schema.xml中的數據庫名稱。確保它不是空白的。

+0

我用端口號調整了propel.php和propel.xml,但這沒有什麼區別。 – wonea

+0

將類名更改爲....../DebugPDO而不是ConnectionWrapper。然後運行測試,看看你得到了什麼。 – Khush

+0

還要確保有一個名爲「測試」的數據庫 – Khush