7

I'm struggling to get propel to connect to a database. I've created mapped the classes and table maps using propel reverse "...", and created the following structure:

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>

Propel.php

<?php namespace propel;

use Propel\Runtime\Propel;

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

I've got the following unit test which falls over:

// 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);

Output;

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

Any ideas? I'm a little stumped... My config seems good, but it obviously isn't.

Update: 2015/07/06 13:01: After debugging this it looks like it bombs out because no connection managers could be found

Runtime propel variables

wonea
  • 4,783
  • 17
  • 86
  • 139

2 Answers2

6

I am just learning Propel and I was having the exact same issue.

I tried doing what you did here:

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

All that did was include the file and print the contents to the output just as you have in your output.

I finally got it working by effectively replacing your Propel.php with:

<?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');

Hope this helps

sullimt
  • 391
  • 1
  • 4
1

Your missing the port number in the below line in propel.xml:

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

Also check the database name in the schema.xml if you have reverse engineered from an existing schema. Make sure it's not blank.

Khush
  • 853
  • 2
  • 8
  • 21
  • I adjusted both propel.php and propel.xml with the port number, but this didn't make any difference. – wonea Jul 15 '15 at 09:04
  • Change the class name to ....../DebugPDO instead of ConnectionWrapper. Then run the test and see what you get. – Khush Jul 15 '15 at 12:52
  • Also make sure there is a database called "test" – Khush Jul 15 '15 at 12:55
  • Another suggestion is check if you are using a socket connection. The configure is slightly different for socket connection. This comes into play if your using MAMP to setup php mysql – Khush Jul 15 '15 at 12:57
  • Oh hang on...... Check your schema.xml and make sure the database name over there is not blank. – Khush Jul 15 '15 at 13:11
  • Please send me an update after trying my suggestions. – Khush Jul 15 '15 at 13:13