Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Propel and Composer in PHP: no connection

I've problem in configuring Propel with Composer in my php project.

this is how appears my tree directory:

project/
    |--/public_html/index.php
    |--/app/
    |    |--data/
    |    |     |--propel.json
    |    |     |--schema.xml
    |    |--vendor/
    |    |--composer.json

In /data/ folder I would store all my propel files, that is generated-classes/ , generated-conf/ and generated-sql/ .

To realize this purpose, with a terminal in /data/ folder, I put the commands in the following sequence:

$ propel sql:build
$ propel model:build
$ propel config:convert

and all go right.

To make more suitable work, in composer.json I've added this extra feature:

"autoload": {
    "classmap": ["./data/generated-classes/"]
}

so that, almost in theory, putting

require '../app/vendor/autoload.php';

inside index.php should be enough. Unfortunately, when I try to use one propel classes inside this page, returns the error

Type: Propel\Runtime\Exception\RuntimeException

Message: No connection defined for database "my_api". Did you forget to define a connection or is it wrong written?

File: 'C:\pathToMyProject'\project\app\vendor\propel\propel\src\Propel\Runtime\ServiceContainer\StandardServiceContainer.php

Line: 279

I thought that propel doesn't find the propel.json file stored in /data/folder.

As extra, if in index.php I simply add

require_once  '../app/data/generated-conf/config.php';

all goes right.

There's a trick to autoload propel without use this last require_once? (obviously keep the tree as is).

Thanks for reading.

like image 974
Sim Sca Avatar asked Mar 15 '15 00:03

Sim Sca


People also ask

How to install propel using composer?

If you want to install Propel via Composer, just create a new composer.json file at the root of your project’s directory with the following content: Then you have to download Composer itself so in a terminal just type the following:

How do I add project dependencies using composer in PHP?

The following example uses Composer, a PHP tool for handling project dependencies. Create the file composer.json which describes your project dependencies. You will use at least Propel (package propel/propel) and VertabeloPropel (vertabelo/vertabelo-propel) as your project dependencies.

Why do we need PHP composer?

In fact, before Composer, there was a popular tool called PEAR which was used to manage PHP extensions and libraries. But it had its own limitations, which Composer was created to address. In a nutshell, we need a tool which can be used to install libraries and manage application dependencies.

How to install PHPMailer in composer?

In the composer.json file, you just need to declare your project dependencies, as shown in the following snippet. Next, when you run the composer install command from that folder, Composer installs the phpmailer package and its dependencies in the vendor directory.


1 Answers

  • The order of CLI commands is important:

    • composer install or update to fetch propel
    • then the commands to generate the models with propel
    • then re-scan / re-generate the autoloading files with composer dump-autoload --optimize
  • You could include the configuration file in the bootstrap process of your application - like you already have.

  • Or you could use the files directive in Composers autoload section to define file(s), which should be included on every request.

    Referencing: https://getcomposer.org/doc/04-schema.md#files

    "autoload": {
        "files": ["./data/generated-conf/config.php"],
        "classmap": ["./data/generated-classes/"]
    }
    
like image 100
Jens A. Koch Avatar answered Oct 08 '22 21:10

Jens A. Koch