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.
Related
On my Laravel 8 application, I have a scenario where I generate the class name a call it. Here is the piece of code for it that I'm calling inside a trait:
$crm_class = "App\Lib\CRM\\" . Str::studly($lead->crm->name);
if (!class_exists($crm_class)) {
throw new \Exception('CRM CLass Not Found: ' . $crm_class);
}
return (new $crm_class)->generateLead($lead, $request);
Basically, depending on the name of the lead's CRM, I call a class that deals with that particular CRM. This could for example resolve to something like App\Lib\CRM\Acme
This works perfectly in my localhost setup, but when I run it on my AWS EC2 instance, the exception is thrown that "CRM CLass Not Found: \App\Lib\CRM\Acme".
I'm not using any use statements on the top for the CRM classes. I've also tried adding a \ before App with no success. I'm expecting to have about 5 different unique CRM classes.
Any idea why the code works on localhost and not elsewhere?
Turned out that Str::studly was producing a capital L in the class name, but my class name was using a lower case l. I changed the case, and the issue is resolved.
I have upgraded Laravel from 4.0 to 4.1 recently in a few instances. Today I did the upgrade in yet another instance and found out that there is something wrong with the User model. The models/User.php file is still there, but I don't think it is used by Laravel anymore. My question is: why?
To demonstrate the issue, I have created the following entries in my routes.php:
Route::get('test1', function()
{
$course = Course::find(4);
return ($course->users()->first());
});
Route::get('test2', function()
{
$user = User::find(22);
return ($user->courses()->first());
});
Both these entries are correct regarding syntax and the database object (course with id 4 exists and user with id 22 exists). My Course.php model has the following:
public function users()
{
return $this->belongsToMany('User')->withPivot('participant_role')->withTimestamps();
}
And my User.php has a corresponding entry:
public function courses()
{
return $this->belongsToMany('Course')->withPivot('participant_role')->withTimestamps();
}
Now if I launch the first URL, /test1, I get a working JSON entry as a result. As expected.
With the second URL, /test2 however I get an error message:
BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::courses()
open: /home/simoa/laravelapps/clientname/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php `
I think there is something wrong here. Why is my Laravel instance trying to call the courses() method from the Illuminate\Database\Query\Builder class? That isn't normal, right?
As I said earlier, everything else works perfectly, except things to do with the User model.
The issue was caused by an invalid entry in the file vendor/composer/autoload_classmap.php.
For some reason during the 4.1 upgrade (probably running the command: composer update) the entry for 'User' => $baseDir . '/app/models/User.php' has turned into 'User' => $baseDir . '/app/models/old_User.php' in that file.
I did have an old backup/dev file called old_User.phpin my models directory and for some reason, it seems, composer has mapped User class to that file.
Solution: delete old_User.php and re-run composer update.
try running php artisan clear-compiled and then php artisan optimize
I am using Zend Framework 2.2.2 and Doctrine2 Module 0.7.0.
My goal is to have my functions related to a task in a standalone php-class. My current workflow is between two different programms: get data -> modify and store data -> send data.
This workflow needs functions from 3 ZF2 modules:
1. source software module
2. internal storage mechanism module
3. destination software module
The first task is successfull but when I move my data to the second module like this (shrinked to the main code):
use MTNewsletterEngine\Controller\NewsletterEngineController;
/** #var \MTNewsletterEngine\Controller\NewsletterEngineController */
private $_newsletterEngine;
$this->_newsletterEngine = new NewsletterEngineController();
[...]
$this->_newsletterEngine->addNewNewsletterRecipient($emailAddresses,1);
The second Controller has problems getting the service locator:
Fatal error: Call to a member function get() on a non-object in C:\xampp\htdocs\app\trunk\module\MTNewsletterEngine\src\MTNewsletterEngine\Controller\NewsletterEngineController.php on line 51
Line 51:
$em_mtnewsletterengine = $this->getServiceLocator()->get('doctrine.entitymanager.orm_mtnewsletterengine');
NewsletterEngineController is the Main Controller from Module MTNewsletterEngine.
I am confused as I don't know how to get this solved. Thanks.
Do not create a new instance of NewsletterEngineController by using the new keyword. The ServiceLocator will not be injected to the created object this way. Use Zend\ServiceManager to retrieve an instance of Zend\Mvc\Controller\ControllerManager (alias: "ControllerLoader" (ci)) and use the get method, to load the target controller. Zend\Mvc\Controller\ControllerManager extends the ServiceManager itself (because it is a plugin manager).
Check your module.config.php. The controller should be listed as an invokable controller.
Example:
'controllers' => array(
'invokables' => array(
'MTNewsletterEngine\Controller\NewsletterEngine' => 'MTNewsletterEngine\Controller\NewsletterEngineController'
),
),
$this->_newsletterEngine = $this->getServiceLocator()
->get('ControllerLoader')
->get('MTNewsletterEngine\Controller\NewsletterEngine');
For more information read the manual and try to understand the way the ServiceManager / ServiceLocator (which is part of Zend\Di) works.
Maybe you should also think about the structure of your application. I am not sure what you are trying to do there but it seems like you are mixing up different application layers.
Docs
http://framework.zend.com/manual/2.2/en/index.html#zend-di
http://framework.zend.com/manual/2.2/en/index.html#zend-servicemanager
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
i just started using PHP namespaces. I have two models classes in separate files
In both files first i declare namespace
namespace eu\ed\sixImport\importViewer\models;
first class:
class Log extends \Doctrine_Record
$this->hasMany('eu\ed\sixImport\importViewer\models\DataSource as DataSource', array(
'local' => 'id',
'foreign' => 'logId'));//setup relationship in setUp method
second class:
class DataSource extends \Doctrine_Record
$this->hasOne('eu\ed\sixImport\importViewer\models\Log as Log', array(
'local' => 'logId',
'foreign' => 'id'));//setup relationship in setUp method
Everything works fine untill i make something like this
$query = \Doctrine_Query::create()
->select('log.*')
->from('eu\ed\sixImport\importViewer\models\Log log')
->leftJoin("log.DataSource")
->orderBy("log.id DESC");
$requiredPage = (($startingRow - ($startingRow%$rowsRequired))/$rowsRequired) + 1;
$pager = new \Doctrine_Pager($query, $requiredPage, $rowsRequired);
$res = $pager->execute();
$this->logsPageCount = $pager->getNumResults();
print_r($res[0]["DataSource"]->toArray());//it fails on access to relationship
Than Doctrine throw Exception
Uncaught exception 'Doctrine_Exception' with message 'Couldn't find class eu\ed' in C:\wamp\www\importViewer\resources\doctrine\Doctrine-1.1.5\lib\Doctrine\Table.php:293...
From Exception, you can see, it looks for class 'eu\ed'. Backslash[s] cut the rest of the class name, and than class is not obviously found. Can you give me some suggestion, how to solve this problem?
Thanks
I am not sure if Doctrine 1 supports namespaces. I really recommend using Doctrine 2 - it is built on top of mature architecture (with API from Java Hibernate) and does not involve that much magic like Doctrine 1.