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
Related
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?
I'm trying to use a method from inside my controller.
This works inside my "view"
use aweber\aweber\aweber_api;
$consumerKey = "XXXX";
$consumerSecret = "XXXXX";
$aweber = new AWeberAPI($consumerKey, $consumerSecret);
but it doesn't work in my controller.
I get the error
Class 'app\controllers\AWeberAPI' not found
Any hints?
I think you extracted AWeber-API-PHP-Library to the path aweber\aweber\aweber_api. The directory structure could be
$ ls aweber\aweber\aweber_api
aweber.php
aweber_api.php
....
If you autoloading classes with composer, AWeberAPI is in root namespace
use \AWeberAPI;
$aweber = new AWeberAPI($consumerKey, $consumerSecret);
If you are not autoloading with composer
require_once('aweber_api/aweber_api.php');
$aweber = new AWeberAPI($consumerKey, $consumerSecret);
Normally the extensione related are in vendor namespace ..
try with
use vendor\aweber\aweber_api;
You have a typo. The name of the php file usually is the same name of the class. So your code should be:
use aweber\aweber\AWeberAPI;
If it isn't, you can try:
use aweber\aweber\aweber_api as AWeberAPI;
Thanks for all the comments here. To fix it I actually moved this functionality into my "view" instead of having it in my controller and it worked.
I wasn't able to test the responses here, so if anyone else if facing the same issues, I'd recommend trying some of these answers.
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
I have configured Propel to work with Laravel with the help of http://picqer.com/blog/propel-with-laravel. All model queries are working fine and all data is being fetched properly.
However, the toJSON and exportTo('JSON') methods are not working neither on the Objects or nor on the PropelObjectCollection returned by certain queries.
The exceptions thrown by Laravel are:
For exportTo('JSON') and toJSON() all caps method
Unknown parser class "PropelArrayParser"
For toJson() method:
Unknown parser class "PropelJsonParser"
I think the problem lies somewhere with autoload of Laravel. I am perhaps missing an entry that'd help it locate PropelJsonParser file.
P.S. I have no idea what effects 'php artisan dump-autoload' causes. But I did run it as per tutorial.
The build.properties file looks like:
propel.project = QuranApp
propel.database = mysql
propel.mysql.tableType = InnoDB
propel.database.url = mysql:host=localhost;dbname=quransociety
propel.database.user = user
propel.database.password = password
propel.disableIdentifierQuoting = false
propel.php.dir = ${propel.project.dir}/../../models
propel.output.dir = ${propel.project.dir}/../../database/propel
propel.phpconf.dir = ${propel.project.dir}/conf
propel.schema.dir = ${propel.project.dir}
I've added those two lines in laravel start.php (at the beginning):
set_include_path(dirname(__DIR__) . '/vendor/propel/propel1/runtime/lib/parser' . PATH_SEPARATOR . get_include_path());
require dirname(__DIR__) . '/vendor/propel/propel1/runtime/lib/parser/PropelJSONParser.php';
so basically I'm including them by hand, and this error is gone. There should be other way, that plays nice along with larvel autoload mechanism, but so far I didn't solve it differently
I am new to doctrine(using version 1.2) and following the steps given in the documentation manual. I've installed and configured it perfectly. But i have a problem while working with models. I have followed each and every step and have successfully generated the models in the models folder.. but after that when i m using the demo code
$user = new User();
$user->username = 'jwage';
$user->password = 'changeme';
it says..
Fatal error: Class 'User' not found in C:\wamp\www\test_doctrine\test.php on line 25
whilst if i check the output of
Doctrine_Core::loadModels('models');
i get
Array
(
[BaseUser] => BaseUser
[User] => User
[UserTable] => UserTable
)
how do i get access to the User class??
Doctrine is not loading the base classes. I had faced a similar problem and I solved it by modifying the autoload function wherein I am getting the base class and I require them then and there itself.
You then in the bootstrap.php file, after spl_autoload_register(array('Doctrine', 'autoload'));, you need to Doctrine_Core::loadModels('models');.
This way the base class gets included through the autoload function and the child class (in this case the User Class) can extend it and then you can access it.