Unable to use Propel 2 (Class not found) - php

I'm tryng from several days to setup and use Propel now 2.0. PHP version is 5.4.4-14+deb7u5
What I have done:
0) Fresh LAMP with a folder "test" in /var/www
1) Composer.json with
{
"require": {
"propel/propel": "2.0.*#dev"
}
}
(also tried with the alpha indicated in home page, no success, download but i cannot use)
2) It download all necessary files.
3) I can launch "vendor/bin/propel" and it exit after some green text.
4) I create the schema.xml with foreign keys indicated in http://propelorm.org/documentation/02-buildtime.html
5) I set up buildtime.cconfiguration
6) I can create the sql:build and the model:build (I find the bookstore.sql in generated-sql and the classes in generated-classes)
7) I CANNOT insert the sql. I launch sql:insert, no error at screen but no insert in database (connection/password is okay, double checked).
8) I load myself SQL in database.
9) I create an index.php with this:
<?php
// setup the autoloading
require_once 'vendor/autoload.php';
use Propel\Runtime\Propel;
use Propel\Runtime\Connection\ConnectionManagerSingle;
$serviceContainer = Propel::getServiceContainer();
$serviceContainer->setAdapterClass('bookstore', 'mysql');
$manager = new ConnectionManagerSingle();
$manager->setConfiguration(array (
'dsn' => 'mysql:host=localhost;dbname=my_db_name',
'user' => 'my_db_user',
'password' => 's3cr3t',
));
$serviceContainer->setConnectionManager('bookstore', $manager);
echo 'All ok, for now...';
$author = new Author();
$author->setFirstName('Jane');
$author->setLastName('Austen');
$author->save();
/* /end of php file */
The echo is printed normally but next row script exit with error 500 and in Apache log I read "Class author not found".
Is there some other config to adjust other than indicate in the guide?

I resolved a similar situation by adding this to my composer.json and then running install again.
"autoload": {
"classmap": ["generated-classes/"]
}

I had this error too. Apparently the problem was with the autoload configuration and running a php composer.phar dump-autoload command fixed it.
php composer.phar dump-autoload

If you want to solve this problem you should combine jerrygarcuih's and Abaobi Orajiaku's awnsers.
Thank you guys.
Add the models folder to the composer.json
"autoload": {
"classmap": ["generated-classes/"]
}
Then run composer 'dump-autoload'.
All generated classes should be in the same namespace.

I had a similiar issue. And i solved it by including the exact path to the classmaps.
"autoload": {
"classmap": [
"path/to/generated-classes/",
"path/to/generated-classes/Base/",
"path/to/generated-classes/Map/"
]
}

Related

Generating bundle with SensioGeneratorBundle gives error symfony 3.4

I'm getting trouble generating a new bundle siwth sensio/generator-bundle, symfony ^3.4, what i do is:
> symfony new calendar --version=^3.4
> cd calendar
> composer require sensio/generator-bundle
> php bin/console generate:bundle:
> planning on sharing this bundle: yes
> Bundle namespace: Nmateo/CalBundle
> Bundle name"NmateoCalBundle": [enter]
> Target Directory"src/": [enter]
> Configuration format"xml": [enter]
> symfony new calendar --version=^3.4
> cd calendar
> composer require sensio/generator-bundle
> php bin/console generate:bundle:
> planning on sharing this bundle: yes
> Bundle namespace: Nmateo/CalBundle
> Bundle name"NmateoCalBundle": [enter]
> Target Directory"src/": [enter]
> Configuration format"xml": [enter]
Then i get this message:
The command was not able to configure everything automatically.
You'll need to make the following changes manually.
-Edit the composer.json file and register the bundle
namespace in the "autoload" section:
-Edit /home/nmateo/Documents/calendar/src/Kernel.php
and add the following bundle in the AppKernel::registerBundles() method:
new Nmateo\CalBundle\NmateoCalBundle(),
So i registered it in autoloader:
"autoload": {
"psr-4": {
"App\\": "src/",
"Nmateo\\CalBundle\\": "src/Nmateo/CalBundle/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/",
"Nmateo\\CalBundle\\": "src/Nmateo/CalBundle/"
}
},
and added new Nmateo CalBundle NmateoCalBundle to the end of registerBundles in Kernel.php:
public function registerBundles()
{
$contents = require $this->getProjectDir().'/config/bundles.php';
foreach ($contents as $class => $envs) {
if ($envs[$this->environment] ?? $envs['all'] ?? false) {
yield new $class();
}
}
new Nmateo\CalBundle\NmateoCalBundle();
}
and do the composer dump-autoload:
Generated autoload files containing 0 classes
And then i start my server and got this error, i tried many times..:
EDIT: Sorry for the text in codes quote but the stackoverflow is giving me headaches since it dont want to publish my question it says: please place your code in code quotes buy all my code was already in codes quotes..
Read GitHub repository before use any bundle. This bundle can`t works well with Symfony 4.0
Symfony 3.4 is equal to LTS Symfony 4.0
Sensiolabs uses *.4th versions as feature-freeze version.
https://github.com/sensiolabs/SensioGeneratorBundle
"WARNING: This bundle does not support Symfony 4. It also does not support the new bundle-less directory structure as created by Symfony Flex. Use the Maker bundle instead."
Symfony 3.4 has an issue with generating a new bundle its a known issue,
https://github.com/symfony/symfony-standard/issues/1098
to fix that change your code as below
"autoload": {
"psr-4": {
"": "src/"
}
What we did is that we specified in the composer that every new generated bundle will be imported from /src
To finish the procedure you'll need to run
composer dump-autoload
make sure you have the file composer.phar or download it from here https://getcomposer.org/download/
hope it helps

Propel ORM and PHP

I am making a php application using propel ORM. It gives me the following message when I try to run it:
Fatal error: Uncaught Error: Class 'Propel\Runtime\Propel' not found in C:\MAMP\htdocs\Conference\vendor\bin\generated-conf\config.php:2 Stack trace: #0 C:\MAMP\htdocs\Conference\vendor\bin\list.php(6): require_once() #1 {main} thrown in C:\MAMP\htdocs\Conference\vendor\bin\generated-conf\config.php on line 2.
In my config.php generated file I have this written:
'classname' => '\\Propel\\Runtime\\Connection\\ConnectionWrapper'
What does it all mean? Am I missing some file or what?
I think you are missing a step in the building.
I assume you have your schema.xml file complete and you also have a propel.yaml (or with allowed extension file) properly configured. Also I assume you got Propel with Composer.
If you have all that the next steps are:
1) Open a terminal and go to your project directory, where the schema.xml and propel.yaml files are.
2) Execute the following command to get yout generated-sql (I have to do it this way on Windows):
c:\MAMP\htdocs\Conference\vendor\bin\propel sql:build
3) Get your model classes with the following command:
c:\MAMP\htdocs\Conference\vendor\bin\propel model:build
4) After generating the classes, you have to autoload them. Open your composer.json file with your text editor and add the following:
"autoload": {
"classmap": ["generated-classes/"]
}
It should look like this, for example:
{
"require": {
"twig/twig": "~1.0",
"propel/propel": "~2.0#dev"
},
"autoload": {
"classmap": ["generated-classes/"]
}
}
5) To finish the classes autoloading, you need to execute on your console:
composer dump-autoload
6) And for the runtime connection settings run this for comunicate classes at runtime:
c:\MAMP\htdocs\Conference\vendor\bin\propel config:convert
7) Assuming you have created your database, the last thing you need to do is create the tables, this is with the following command:
c:\MAMP\htdocs\Conference\vendor\bin\propel sql:insert
And there you go! That works for me every time I build a project.

Laravel: Created a service but class is not found

Created the following file:
File: App\Services\Custom\Auth\AuthService.php
Name space: App\Services\Custom\Auth
Class name: AuthCustom
Method inside: foo()
In my controller I'm trying to call the foo method from the Service I created.
App\Services\Custom\Auth\AuthService\AuthCustom::foo()
Why does it keep returning Class 'App\Services\Custom\Auth\Authservice\AuthCustom' not found
What am I doing wrong?
Thanks!!
EDIT:
I added this in the composer.json and run composer dump-autoload without errors.
And it works!
"autoload": {
"classmap": [
"database",
"app/Services/Custom/Auth/AuthService.php"
],
"psr-4": {
"App\\": "app/"
}
},
Your namespace does not match your directory structure. If your class is in App\Services\Custom\Auth\AuthService.php, then your namespace needs to be App\Services\Custom\Auth. If you really want your namespace to be App\Custom\Auth, then your file needs to be App\Custom\Auth\AuthService.php.
Once you fix this, make sure you do a composer dump-autoload on the command line.
It seems that you didn't run composer dump-autoload or php composer.phar dump-autoload.
The composer.json is very important for autoloading!
Laravel needs an big file with all your php files required, usually generated via calling either artisan or composer with : php artisan dump-autoload / composer dump-autoload
It just regenerates the list of all classes that need to be included in the project (autoload_classmap.php). Ideal for when you have a new class inside your project.
More details: http://developed.be/2014/08/29/composer-dump-autoload-laravel/

Making Legacy Application PSR-4 Compatible

I have a question with regards to refactoring a legacy PHP application to be compatible with PHP's PSR-4 standards. I have some classes located at app/classes/ folder which are not yet namespaced properly and I want them to be autoloaded directly when I call composer's vendor/autoload.php. I added a name space according to \<NamespaceName>(\<SubNamespaceNames>)*\<ClassName> and I have tried creating a vendor/myapp/classes/src/ directory under the vendor folder of composer, and executed a dump-autoload command but to no avail. The class doesn't get loaded up and composer can't figure out where to find it. Any pointers on how to do this?
Thanks,
Jan
Thing/s to Note:
-> I don't want to upload the source code to any repository that can be publicly searchable like Packagist as the code is for internal use only.
EDIT:
Here is my composer.json:
...
"autoload":{
"psr-4": {
"Myapp\\" : "Myapp"
}
},
...
But now the structure looks like:
->Myapp
-->Classes
--->Module1
---->submodule.php
--->Module2
---->submodule2.php
--->Module3
---->submodule3.php
--->Config
---->config.db.php
->vendor
-->autoload.php
Here are my issues/questions:
When I try to load submodule.php, which in turn would load submodule2.php and submodule3.php, it would tell me that submodule3.php is not found. The namespace of submodule3.php is Myapp\Classes\Module3 but it says its not found.
I want to include forcibly, config.db.php, on every call of autoload.php
I now figured it out. But the source files will not reside in the /vendor folder, which is okay for my case. If you want to make your files be autoloaded automatically no matter which folder, just add it to the psr-4 block in the composer.json, or create it if it's not yet there. In my composer.json, I have this block on the top level object, which adds the folder I want to get autoloaded and includes also the specific file I want to include, like a config file of some sorts:
...
"autoload":{
"files": [
"somefolder/somefile.php"
],
"psr-4": {
"MyApp\\" : "MyApp"
}
},
...
Which simply means that composer should autoload files residing in the MyApp directory which will also have a namespace of MyApp.
So my folder structure looks like this now:
->MyApp
-->Classes
--->MyClass1.php --> namespace MyApp\Classes classname: MyClass1
-->Components
--->MyClass2.php --> namespace MyApp\Classes classname: MyClass2
->somefolder
-->somefile.php
->vendor
-->autoload.php
->index.php
So if I want to include the file MyClass1.php in index.php, I will just add it like:
include_once( __DIR__ . "/vendor/autoload.php");
use \MyApp\Classes\MyClass1;
$foo = new MyClass1();
$foo->bar();
Hope it helps.
You could approach this by creating your own composer package, adding it to your private git repository, and adding the package to your project's composer.json file:
"require": {
"{your-vendor}/{package-name}": "dev-master"
},
"repositories": [
{
"type": "vcs",
"url": "git#bitbucket.org:{your-vendor}/{package-name}.git"
}
],
Once this is done, run composer update.

Load git repo with composer - autoloading issue

I have a github repository https://github.com/KoulSlou/UPS and I would like to add it to my project.
In project root I created composer.json file and defined the following autoloading properties:
{
"autoload": {
"files": [
"libraries/Ups/Ups.php",
"libraries/Ups/Ups_Base.php",
"libraries/Ups/Ups_Base_Response.php",
"libraries/Ups/Ups_Live_Rates.php"
]
}
}
When I run
php composer.phar install
repository is being downloaded, but it looks like autoloader is not working. When I try to initialize one of the classes
$test = new Ups()
I got the following error:
Fatal error: Class 'Ups' not found in application/....
Did I define "autoload" property incorrectly?
I'd suggest not using the "files" autoloader, because that isn't very automatic - the files mentioned here are ALWAYS included. Replacing it with "classmap" would be better. And then you'd not be required to mention ALL files, but you can simply state the directory you want to have scanned for classes.
Now what I don't see anywhere: Did you initialize Composer's autoloader anywhere? This usually is something like
require "vendor/autoload.php";
Finaly, I have found out what was the problem. composer.json file in the project I was trying to load - UPS library -was invalid. I was able to download files when I ran:
composer.phar install
but it looks like composer.json file was ignored. I found it out when I ran
composer.phar update
and got
No valid composer.json was found
With option -v I got error that "name" is undefined index. So, I simply added "name" field to the composer.json. Final version is:
{
"name":"KoulSlou/UPS",
"autoload": {
"files": [
"libraries/Ups/Ups.php",
"libraries/Ups/Ups_Base.php",
"libraries/Ups/Ups_Base_Response.php",
"libraries/Ups/Ups_Live_Rates.php"
]
}
}

Categories