Propel Data Load - "default" context does not exist - php

I am currently stuck on the error The "default" context does not exist. when trying to build my data model with the command symfony propel:build --application=frontend --all --and-load --no-confirmation
After lots of Googling it appears this error is caused by using sfContext inside a model or a form so I have found these and commented them out (see below), the error still occurs, does anyone else know a fix?
>> file- /var/www/html/dev/meeting/config/generated-sfGuardPlugin-schema.xml
>> file- /var/www/html/dev/meeting/config/generated-schema.xml
>> propel load data from "/var/www/html/dev/meeting/data/fixtures"
>> propel load data from "/var/www/html/dev/meeting/plugins/sfGuardPlugin/data/fixtures"
The "default" context does not exist.
grep -R sfContext lib/model/*
lib/model/MeetingMeetings.php: return "";//sfContext::getInstance()->getController()->genUrl('meeting/show?id='.$this->getId(), $full);
lib/model/sfGuardUserProfile.php: //if(!is_null(sfContext::getInstance())&&($useYou||$useYourself)&&$this->getUserId()==sfContext::getInstance()->getUser()->getId()) {
grep -R sfContext lib/form/*
lib/form/MeetingMeetingsForm.class.php: //sfContext::getInstance()->getUser()->setFlash("info",
Many thanks for your time,
Not sure what information I can provide, does anyone have any other questions?

I resolved this problem by doing the following.
First find all references to sfContext in your model files and find an alternative way to get what ever sfContext was needed for (for example passing it to the method).
Check all library files mentioned inside any models for use of sfContext, repeat above solution.
Use is_null checks on sfContexts where it has to exist so it only does if it required.
The problem in my case was my save method used another library which used sfContext to get the current user, which obviously doesn't exist when inserting data to the model

Just ran into this today with data-load. Instead of modifying the function signature, here's what I did.
if(!sfContext::hasInstance())
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'cache', true);
else
$configuration = sfContext::getInstance()->getConfiguration();
And then I can run stuff like this in my model classes.
$configuration->loadHelpers('Texturizer');
I'm not sure what the 'cache' parameter does in getApplicationConfiguration, but I found that line off of the Jobeet book here http://www.symfony-project.org/jobeet/1_4/Propel/en/21

To resolve the problem quickly, comment the code /lib/form/baseForm.class.php , generate the module, and restores the code

Related

Laravel cannot find a Controller Method (does not exist)

I'm trying to implement a new method in a BoController called "deleteBooking", the method is defined:
public function deleteBooking($id){
$booking = Reservation::find($id);
if($booking && $booking->delete()){
try {
$email = Mail::to($booking->user_email)->send(new Cancel($booking));
} catch(\Exception $e){
Log::error($e->getMessage());
}
return redirect('admin/manager/home')->with('message','Réservation annulée!');
}
return redirect('admin/manager/home')->with('message','Réservation non annulée!');
}
But laravel at the endpoint says:
(1/1) BadMethodCallException
Method [deleteBooking] does not exist.
Other methods from the same class are linked to endpoints too, and work well.
Do you have any ideas please? Thank you.
I got it fixed, I've found another file called BoController, in another folder somehow and it was conflicting with the App\Http\Controllers one.
Thank you.
It's most likely that you have declared that function for some other request type other than the one you're trying to make. For example you put Route::post('some-method', 'BoController#deleteBooking'); but you need to put either Route::get(...) or Route::put(...) or Route::delete(...).
If it isn't that problem, then you probably misspelled it.
I have faced similar issue. Then I have figured out a issue pointed in composer install log, with following instance of log line:
Class App\Http\Controllers\BlogController located in ./app/Http/Controllers/BlogControllerOld.php does not comply with psr-4 autoloading standard. Skipping.
Based on that I have found that one of the file renamed with Old suffix was creating conflict with the main file. So here I have to chhoseone of the following solutions:
To delete the file created for backup.
Or just rename the class in duplicated file to BlogControllerOld.
So its a good idea to check for issues with composer install
It will highlight the conflicts that can be fixed using one of the method above.
Once fixed using specified methods above issue composer install to apply the fix and regenerate autoloader.

Call to undefined method ActiveRecord\Config::initialise() composer

Hey everyone I'm following a tutorial on Composer, I've installed ActiveRecord and I'm trying to create a database model. Whenever I load the page though I get this error: Call to undefined method ActiveRecord\Config::initialise()
Here's my setup file in index.php
require_once "vendor/php-activerecord/php-activerecord/ActiveRecord.php";
ActiveRecord\Config::initialise(function($cfg) {
//setting up a model (which is the representation of a table)
$cfg->set_model_directory('models');
$cfg->set_connections(array(
'development' => 'mysql://root:tutsplus#localhost/blog'
));
});
$posts = Post::all();
print_r($posts);
?>
And here's where I declare Post
class Post extends ActiveRecord\Model{}
I really cannot find the reason this isn't working, I actually did this instead to see if manually creating a new Post instance would fix the initialisation problem but it didn't, it had exactly the same error:
$post_class = new Post;
$posts = $post_class->all();
print_r($posts);
I'm really stumped on this one, I've usually managed to find something that fixes my problem on here but this is just nope. There is literally no difference to the tutorial code that I can see and I've checked it loads of times. Any help would be greatly appreciated.
(edit: the duplicate php-activerecord folders at the top isn't a code problem, the folder is actually duplicated and I haven't got round to moving the contents yet)
Point one: When using Composer, you are supposed to only include "vendor/autoload.php", and nothing else. Composer does the rest of autoloading for you.
Point two: It is called initialize with a Z, not S. You might simply have misspelled that method name.

CakePHP - how to simply reset cached database models

I have several apps based on CakePHP and this basically applies to all of them. When my debug mode is set to 0 (live mode), every time I update the database structure, like new tables and fields, then as soon as my app uses those, I always get the default "An Internal Error Has Occurred" message. It is solved if I set debug to 1 and then use those new fields. Is there a better way to do this? I don't want to enable debugging and doing a test write every time I have to update my database. Also /tmp/cache subfolders are empty, so I don't know where it is stored.
Here's a function I wrote to do exactly that.
function clear_cache() {
$cachePaths = array('js', 'css', 'menus', 'views', 'persistent','models');
foreach($cachePaths as $config) {
clearCache(null, $config);
}
}
It uses the clearCache function in Cake.
You need to clear all cache configs
bin/cake cache clear_all
Source
For cake 2.x, you can delete the cache directory like this:
rm -rf app/tmp/cache/
For CakePHP 2.x, place this line of code anywhere in your application to clear the model cache:
Cache::clear(false, '_cake_model_');
This is decoupled from the low-level cache engine (File, Memcache, Redis, etc), so it should work as-is.
CakePHP 2.x Docs: Caching

PHP-FFMpeg prerequisites

I'm attempting to implement https://github.com/PHP-FFMpeg/PHP-FFMpeg
I copied the src/FFMpeg folder to my includes folder and made sure my autoloader knows where to find everything.
as a test I made a script that simply does:
$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open('video.mpg');
I get:
Fatal error: Class 'Doctrine\Common\Cache\ArrayCache' not found in /var/www/php/include/FFMpeg/FFProbe.php on line 203
My question is: does PHP-FFMPeg require Doctrine, because that is not stated in the documentation. What version do I need? Are there other prerequisites?
I could create a new question for this, but I'm not sure if I should. I now have PHP-ffmpeg implemented. I'm using Laravel, however that should be irrelevant for this question. I'm trying to enable progress monitoring. It works, however I need to pass in an ID so I can update the correct key in memcache.
$id = 12345;
$format->on('progress', function ($audio, $format, $percentage) {
//this works perfect, but doesn't tell me which item is being updated
Cache::put("progress", $percentage, .25);
//this does not work as I am unable to pass in $id, if I add it as the 4th argument above it will display the number of threads or something
//Cache::put("{$id}_progress", $percentage, .25);
});
I need clarification on the "on" method. I looked through https://ffmpeg-php.readthedocs.org/en/latest/_static/API/ and was not able to figure out how this method works. Any help would be appreciated.
You should follow the recommended instructions in the README.
Composer is the easiest way to install PHP-FFMpeg dependencies
The "on" method called on the format is an implementation of EventEmitter.
As you can see here : https://ffmpeg-php.readthedocs.org/en/latest/_static/API/FFMpeg/Format/ProgressableInterface.html it extends the EventEmitterInterface of https://github.com/igorw/evenement.
If you're really interested about how it works under the hood, have a look at here :
The progress listener is created here : https://github.com/PHP-FFMpeg/PHP-FFMpeg/blob/master/src/FFMpeg/Format/Audio/DefaultAudio.php#L96 and added at execution here https://github.com/PHP-FFMpeg/PHP-FFMpeg/blob/master/src/FFMpeg/Media/Video.php#L151
This is actually possible because FFMpegDriver extends the Driver provided by https://github.com/alchemy-fr/BinaryDriver
Hope this helps :)

Reloading a Class

I have a PHP daemon script running on the command line that can be connected to via telnet etc and be fed commands.
What it does with the command is based on what modules are loaded, which is currently done at the start. (psuedocode below for brevity)
$modules = LoadModules();
StartConnection();
while(true){
ListenForCommands();
}
function LoadModules(){
$modules = Array();
$dir = scandir("modules");
foreach($dir as $folder){
include("modules/".$folder."/".$folder.".php");
$modules[$folder] = new $folder;
}
}
function ListenForCommands(){
if(($command = GetData())!==false){
if(isset($modules[$command])){
$modules[$command]->run();
}
}
}
So, an example module called "bustimes" would be a class called bustimes, living in /modules/bustimes/bustimes.php
This works fine. However, I'd like to make it so modules can be updated on the fly, so as part of ListenForCommands it looks at the filemtime of the module, works out if it's changed, and if so, effectively reloads the class.
This is where the problem comes in, obviously if I include the class file again, it'll error as the class already exists.
All of the ideas I have of how to get around this problem so far are pretty sick and I'd like to avoid doing.
I have a few potential solutions so far, but I'm happy with none of them.
when a module updates, make it in a new namespace and point the reference there
I don't like this option, nor am I sure it can be done (as if I'm right, namespaces have to be defined at the top of the file? That's definitely workaroundable with a file_get_contents(), but I'd prefer to avoid it)
Parsing the PHP file then using runkit-method-redefine to redefine all of the methods.
Anything that involves that kind of parsing is a bad plan.
Instead of including the file, make a copy of the file with everything the same but str_replacing the class name to something with a rand() on the end or similar to make it unique.
Does anyone have any better ideas about how to either a) get around this problem or b) restructure the module system so this problem doesn't occur?
Any advice/ideas/constructive criticism would be extremely welcome!
You should probably load the files on demand in a forked process.
You receive a request
=> fork the main process, include the module and run it.
This will also allow you to run several commands at once, instead of having to wait for each one to run before launching the next.
Fork in php :
http://php.net/manual/en/function.pcntl-fork.php
Tricks with namespaces will fail if module uses external classes (with relative paths in namespace).
Trick with parsing is very dangerous - what if module should keep state? What if not only methods changed, but, for example, name of implemented interface? How it will affect other objects if they have link to instance of reloaded class?
I think #Kethryweryn is something you can try.

Categories