PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 26, 2022

[FIXED] Propel and Composer in PHP: no connection

 January 26, 2022     autoload, composer-php, configuration, php, propel     No comments   

Issue

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.


Solution

  • 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/"]
    }
    


Answered By - Jens A. Koch
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing