Phalcon MVC model Exception - php

I'm trying to create an API with Phalcon for the first time.
I have been followed the tutorial "http://docs.phalconphp.com/en/latest/reference/tutorial-rest.html", but encountered a problem.
I've been created a new project with all the settings, and inside I got:
a "models" folder with "photos.php" file
a "index.php" with connection to my DB and function to retrieve information from "photos" table
The problem is that when I'm trying to activate the function through the browser i get an Error:
"Phalcon\Mvc\Model\Exception: Model 'photos' could not be loaded inC:\wamp\www\Test\index.php on line 77".
$photos = $app->modelsManager->executeQuery($phql); // line 77
What can cause this problem?

It's one of three problems:
1 - Your class name in photos.php is not photos.
2 - You have mis-referenced the photos model in your PHQL query.
3 - You have not registered the directory where your models are stored. To do this, add
$loader = new \Phalcon\Loader();
$loader->registerDirs(array(
'/path/to/models'
))->register();
after
$di = new \Phalcon\DI\FactoryDefault();
but before
$app = new \Phalcon\Mvc\Micro();

If you create project structure using Phalcon developer tool, the config.ini might need an update like this:
from:
modelsDir = /models/
to:
modelsDir = **..**/models/

i just faced same issue, got it solved by add require with model path right after $di = new \Phalcon\DI\FactoryDefault(); and
before $app = new \Phalcon\Mvc\Micro($di);
like suggested by brian

Related

Doctrine ORM not working with the Migrations

Good Day, my friends.
I want to use the doctrine ORM with the Migrations.
The issue is next: I want to place the migration configuration file in the specific folder. For example: 'config/doctrine-migrations.php'.
Everything working fine when I follow the official documentation and place the migrations.php file in the root folder, but when I try to place it in the specific folder system is not working.
My cli-config.php content is:
<?php
require_once "app/bootstrap.php";
return \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($container->get(\Doctrine\ORM\EntityManager::class));
Well, I can change this file in a next way:
<?php
require_once "app/bootstrap.php";
return \Doctrine\Migrations\DependencyFactory::fromEntityManager(
new \Doctrine\Migrations\Configuration\Migration\PhpFile(BP . '/config/doctrine-migrations.php'),
new \Doctrine\Migrations\Configuration\EntityManager\ExistingEntityManager($container->get(\Doctrine\ORM\EntityManager::class))
);
After this, Doctrine Migration working fine, but Doctrine ORM stop working with the next error:
Argument #1 ($helperSet) must be of type Symfony\Component\Console\Helper\HelperSet, Doctrine\Migrations\DependencyFactory given
If someone knows how to solve my issue and use a specific config file please clarify a possible solution.
Best Regards, Mavis.
I may be a bit late, but i ran into the same problem as you.
You can use the same cli-config for migrations and orm. For that, you need add
the orm commands manually to the cli-config.php.
My cli config looks like this:
$em = getEntityManager();
$config = new PhpFile('migrations.php');
$dependencyFactory = DependencyFactory::fromEntityManager($config, new ExistingEntityManager($em));
$migrationCommands = [
new Command\DumpSchemaCommand($dependencyFactory),
new Command\ExecuteCommand($dependencyFactory),
new Command\GenerateCommand($dependencyFactory),
new Command\LatestCommand($dependencyFactory),
new Command\ListCommand($dependencyFactory),
new Command\MigrateCommand($dependencyFactory),
new Command\RollupCommand($dependencyFactory),
new Command\StatusCommand($dependencyFactory),
new Command\SyncMetadataCommand($dependencyFactory),
new Command\VersionCommand($dependencyFactory),
];
$customCommands = [];
$commands = array_merge($migrationCommands, $customCommands);
ConsoleRunner::run(new SingleManagerProvider($em), $commands);
All my import statements, are not included in this example, but you can
get them from the docrtine documentation
Looks like I found an answer.
I can add custom integration in my application by this guide: https://www.doctrine-project.org/projects/doctrine-migrations/en/3.0/reference/custom-integration.html
In a custom file, I can configure whatever I want.
Hope that this solution will help somebody else.

Phalcon 2 vs Phalcon 3 instantiate model

I've migrated my Phalcon project from 2 to 3 and get some unexpected behavior. In my loader I do this:
<?php
use Phalcon\Loader;
$loader = new Loader();
$loader->registerDirs(
array(
$config->application->libraryDir,
$config->application->controllersDir,
$config->application->modelsDir
)
);
$loader->register();
Now, when I try to instantiate one of my models in my controller-action the code halts:
$present = new \Present();
Results in a "recv() failed (104: Connection reset by peer) while reading response header from upstream" error in /var/log/nginx/error.log.
However, when I instantiate the class directly in my loader.php file the model is auto-loaded and I can instantiate it anywhere. The workaround for the controller action is:
if (class_exists('Present')) { // THIS triggers the autoloader
$present = new \Present();
$present->save();
}
So the issue is solved for now. My question is: why doesn't
new \Present()
trigger the auto-loader in the controller-action as I expected? Why did it work under Phalcon 2? Why does it work when I do it directly in the loader.php or in public/index.php?

Laravel 5 add nusoap

I am unable to install nusoap to my existing laravel 5 application. Ive done the following:
Created a new Folder in App/Http/Controllers/ - namend "Soap" - and copied the libary into it.
use
composer dump-autoload
So i am able to use
use nusoap_client;
But i am always getting an error:
Class 'App\Http\Controllers\nusoap_client' not found
I thought that laravel automatically load all classes from the "app" directory, but how can i use it here?
Tried with:
$wsdl = "test.xx/_vti_bin/lists.asmx?wsdl";
$client = new nusoap_client($wsdl, true);
Thanks for any help!
Just add these lines to your controller:
include 'nusoap.php';
use nusoap_client;
As a simple way, you can add a backslash in front of nusoap_client, like this:
$client = new \nusoap_client($wsdl, true);

Doctrine lazy loading

I'm just getting to grips with Doctrine, and using the suggested lazy loading of models. As per the tutorials, I've created a doctrine bootstrap file:
<?php
require_once(dirname(__FILE__) . '/libs/doctrine/lib/Doctrine.php');
spl_autoload_register(array('Doctrine', 'autoload'));
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
$manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_CONSERVATIVE);
Doctrine_Core::loadModels(array(dirname(__FILE__) . '/models/generated', dirname(__FILE__) . '/models')); //this line should apparently cause the Base classes to be loaded beforehand
My models and base classes have all been created by Doctrine.
I've also created a simple test file as follows:
<?php
require_once('doctrine_bootstrap.php');
$user = new User();
$user->email = 'test#test.com';
echo $user->email;
However, this generates the following error:
Fatal error: Class 'User' not found in E:\xampp\htdocs\apnew\services\doctrine_test.php on line 4
However, if I explicitly require the BaseUser.php and User.php files, then it works fine without any errors
<?php
require_once('doctrine_bootstrap.php');
require_once('models/generated/BaseUser.php');
require_once('models/User.php');
$user = new User();
$user->email = 'test#test.com';
echo $user->email;
So, it seems that Doctine is not auto loading the models correctly. What am I missing?
OK, so you need the following line in the bootstrap file:
spl_autoload_register(array('Doctrine_Core', 'modelsAutoload'));
And then auto loading works as expected
Your approach is correct since Doctrine has it's own loading functionallity:
Doctrine::loadModels('models');
Doctrine::loadModels('models/generated');
Doctrine::loadModels('models/tables');
...
This is not recursive so you need to add folders that contain your mapped/managed models.
In the User.php model there needs to be a require to the BaseUser.php class at the top. As the user class extends the BaseUser.php
I have had this issue and that has solved it. I would be interested if there is something I am missing to not have to do that include manually. Give that a shot and see if it fixes the issue without having to require the User.php

Need help setting up Doctrine 2

i setup Doctrine 1 b4 but it seems like now when i try Doctrine 2 it fails
i have Doctrine installed at D:\ResourceLibrary\Frameworks\Doctrine
D:\ResourceLibrary\Frameworks\Doctrine\bin
D:\ResourceLibrary\Frameworks\Doctrine\Doctrine
D:\ResourceLibrary\Frameworks\Doctrine\Doctrine\Common
D:\ResourceLibrary\Frameworks\Doctrine\Doctrine\DBAL
D:\ResourceLibrary\Frameworks\Doctrine\Doctrine\ORM
i put D:\ResourceLibrary\Frameworks\Doctrine\bin in the PATH Windows Environment Variable
and added D:\ResourceLibrary\Frameworks\Doctrine to the php.ini include_path
i find that when i try to do
D:\>php doctrine.php
Could not open input file: doctrine.php
fails ... i thought that since i have D:\ResourceLibrary\Frameworks\Doctrine\bin in the PATH Windows Environment Variable, it shld be able to find doctrine.php?
D:\ResourceLibrary\Frameworks\Doctrine\bin>php doctrine.php
Warning: require(D:\ResourceLibrary\Frameworks\Doctrine\bin/../lib/vendor\Symfony\Components\Console\Helper\HelperSet.ph
p): failed to open stream: No such file or directory in D:\ResourceLibrary\Frameworks\Doctrine\Doctrine\Common\ClassLoad
er.php on line 143
Fatal error: require(): Failed opening required 'D:\ResourceLibrary\Frameworks\Doctrine\bin/../lib/vendor\Symfony\Compon
ents\Console\Helper\HelperSet.php' (include_path='D:\ResourceLibrary\Frameworks\ZendFramework\library;D:\ResourceLibrary
\Frameworks\Doctrine;.;c:\php\includes') in D:\ResourceLibrary\Frameworks\Doctrine\Doctrine\Common\ClassLoader.php on li
ne 143
then 2nd try, pass with errors ...
After spending several hours to configure Doctrine 2 on my machine last night I finally succedded to work with Doctrine 2.
I configured Doctrine 2 in this way.
There are several ways to install Doctrine 2 although I preferred to download from package manager.
First I downloaded package DoctrineORM-2.0.0BETA3.tgz from http://www.doctrine-project.org/projects/orm/download
I extracted the tar file inside my test folder where I tested my sample code. My folder looks like
./test
DoctrineORM/
DoctrineORM/bin
DoctrineORM/bin/Doctrine
Then I created 2 folders on root 'model' and 'proxies'.
Now we need to bootstrap Doctrine.
--- Bootsrap ---
<?php
// test.php
require 'DoctrineORM/Doctrine/Common/ClassLoader.php';
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine', 'DoctrineORM');
$classLoader->register(); // register on SPL autoload stack
$classloader = new \Doctrine\Common\ClassLoader('model', __DIR__);
$classloader->register();
$config = new \Doctrine\ORM\Configuration();
$cache = new \Doctrine\Common\Cache\ArrayCache();
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
$driverImpl = $config->newDefaultAnnotationDriver('model');
$config->setMetadataDriverImpl($driverImpl);
$config->setProxyDir('proxies');
$config->setProxyNamespace('proxies');
$config->setAutoGenerateProxyClasses(true);
$config->getAutoGenerateProxyClasses();
$connectionOptions = array(
'driver' => 'pdo_mysql',
'dbname' => 'test',
'user' => '[DB_User]',
'password' => '[DB_Pass]'
);
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
//echo 'Hello World!' . PHP_EOL;
// Get result from Table
$q = $em->createQuery('SELECT c FROM model\User c ORDER BY c.name');
$t = $q->getResult();
echo "<pre>"; print_r($t);
// Save a new User to DB
$uu = new model\User();
$uu->name = 'test name1';
$uu->age = 4;
$em->persist($uu);
$em->flush();
--- Bootsrap ---
Then I created a model file in model directory
--- Model - User.php ---
<?php
// Model File model/User.php
namespace Model;
/** #Entity #Table(name="users") */
class User
{
/**
* #Id #Column(type="integer")
* #GeneratedValue(strategy="AUTO")
*/
public $id;
/** #Column(type="string", length=50) */
public $name;
/** #Column(type="integer", length=50) */
public $age;
}
--- Model - User.php ---
My complete folder structure now looks like below.
./test
DoctrineORM/
DoctrineORM/bin
DoctrineORM/bin/Doctrine
Model/
Model/User.php
test.php
Hope this will help you. If you need any further help I can send you the complete source which works fine at my machine.
You don't need the entire Symfony framework. Just the bits that Doctrine relies on.
If you download the current version of the ORM component (right now at BETA2), it will include a folder called Symfony (probably in lib/vendor/Symfony, but it tends to move around with new releases). You need to make sure that the ClassLoader in doctrine.php or cli-config.php can find that Symfony folder.
$classLoader = new \Doctrine\Common\ClassLoader('Symfony', __DIR__ . '/path/to/Symfony');
$classLoader->register();
I hope this information is accurate. The Doctrine team keeps messing with the structure of the release they closer they get to a final version.
If you want to use the command line interface in the new Doctrine 2 beta you also have to install the Symfony 2 framework (because it includes the Console Helper) and put it into your include path.
As far as I know there is currently no PEAR installation available, so you will have to download the sources from the Symfony 2 website or via Git.
On a sidenote: It seems that you also tried to execute doctrine.php at a path where it doesn’t exist. That’s the reason why you got the
Could not open input file: doctrine.php
error message.
Doctrine v2.* is stored in bin dir.
Try like this :
$ vendor/bin/doctrine orm:conver-mapping xml src/ --from-database --force
Dont put PHP on start.

Categories