Doctrine Cache Symfony2 deleteByPrefix - php

I would like to delete all my cache entries prefixed by a string, but my code returns :
Fatal error: Call to undefined method Doctrine\Common\Cache\FilesystemCache::deleteByPrefix()
and my code is
$deleted = $cachemanager->deleteByPrefix('catalog_');
like where I checked : into the documentation of doctrine http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/caching.html#deleting.
I need to clear the cache since controller, not from CLI...
Does someone as an alternative to this method 'deleteByPrefix()' ?

You can use the namespace. This will require reorganizing your caches. Or you can write your own implementation (extending Doctrine\Common\Cache\FilesystemCache) which will implement deleteByPrefix(), but you'll use the ability to simply switch cache provider.

Finally, I come to use the Zend Framework cache bundle, who permit to delete by prefix...

Related

Symfony cache dont work with class/object

I use some php library, and generate element of class
$elnew = new LibClass();
I want to save this variable to cache.
If I make like this
$elem = $cache->getItem($ig_name);
if (!$elem->isHit()) {
$elem->set($elnew);
$cache->save($ig);
}
$elem->isHit() is always false. I checked how cache works with string - all is ok.
Also I'm not able to serialyze/unserialyze this object because it says
Serialization of 'Closure' is not allowed
and no way to modify LibClass
How can I save $elnew to cache? Any variants for with symfony components? Or maybe other libs can help me?
Serialization of 'Closure' is not allowed
You can use the PHP SuperClosure library to get rid of this.
Also you can try other memory storages like Redis or Memcache to cache your objects. See this resolved stackoverflow question.

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

atk4 schema generator error

I get the following error when running:
class page_generator extends Page_SchemaGenerator {
}
Application Error: requires jQuery or jUI support
BaseException, code: 0
C:\projects\wamp\atk4\atk4\lib\Form\Submit.php:33
Where did you get such class? What version of ATK4 are you using?
Anyway, you have to add jUI class iun your Frontend class.
Sorry, but I couldn't remain silent and will say that - schema generators are evil !!! :)
There are only very rare case when they are OK. One of such cases is if you actually require dynamically changeable DB structure. If that's the case, then better use this add-on: https://github.com/atk4/atk4-addons/tree/master/dynamic_model
Add the following to your lib/Frontend.php
$this->add('jUI');
And as #DarkSide said - schema generators are evil.
There is also on-the-fly generator controller for model:
https://github.com/atk4/atk4-addons/tree/master/dynamic_model

How to enable ORM annotation prefix outside of Symfony2?

I'm converting an old PHP project to the Symfony2 framework. Some of the pages are now handled by my Symfony2 front controller (index.php), but many pages have not yet been converted.
The problem is that, within Symfony, all of my Doctrine entity annotations must begin with the ORM\ prefix, but outside of Symfony, that prefix does not appear to be enabled, and so I get the following error:
Class MyProject\MyBundle\Entity\MyClass is not a valid entity or mapped super class.
I've tried to duplicate whatever magic Symfony does to set this up, including following these instructions [doctrine-project.org], and actually including app/autoload.php entirely into my legacy bootstrap process. But nothing works.
Does anyone know how I can manually replicate whatever it is that Symfony does to enable the ORM\ prefix for my Doctrine annotations?
I got the answer from the Symfony2 Google group. The problem is that the Doctrine configuration shown in the documentation uses SimpleAnnotationReader behind the scenes, but you need regular AnnotationReader to use the ORM\ namespace prefix. I got it to work by replacing this:
$config = new Doctrine\ORM\Configuration();
$driver = $config->newDefaultAnnotationDriver('/path/to/my/entities');
with this:
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
// ...
$config = new Doctrine\ORM\Configuration();
$reader = new AnnotationReader();
$driver = new AnnotationDriver($reader, '/path/to/my/entities');
I ended up with:
Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration($paths, $devMode, null, null, false);`
The 3rd and 4th null arguments are default. The 5th false argument tells it to make a standard AnnotationReader rather than a basic one.
I'm using Doctrine 2.5.6.
Explanation
I found I couldn't get Ian's solution working without calling Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration before making my own config. I was getting this error:
'[Semantical Error] The annotation "#Doctrine\ORM\Mapping\Entity" in class My\Class does not exist, or could not be auto-loaded.'
I was really confused so I took a look at the source code.
It turns out createAnnotationMetadataConfiguration calls Doctrine\ORM\Configuration::newDefaultAnnotationDriver rather than creating the annotation driver directly. This calls AnnotationRegistry::registerFile(__DIR__ . '/Mapping/Driver/DoctrineAnnotations.php'); which seems to be critical. After that, newDefaultAnnotationDriver just creates a new AnnotationDriver().

How do you define Exception?

I'm used to Zend Framework, when you write your own component, you make it's own Exception file, but on per file basis, then you have such structure:
Zend/View/Exception.php
Zend/View/Helper/Exception.php
Zend/View/Renderer/Exception.php
etc.
I'm ok with, I also use Doctrine2 and Exception are "stored" in a different way
something like (in a Zend way)
and in Zend/View/Exception.php
class Exception {
public static function invalidArguement() {
return new self('Invalid arguement was given, etc..');
}
I understand that the second approach is less flexible but more accurate because it throws exception according the error.
The first approach is just a way to be able to throw a Zend_View_Exception with a custom messagE.
Also, what about one Exception file per, Exception.
Like the following structure :
Exception/InvalidArguement.php
Exception/AuthentificationFailed.php
Exception/QuantityLimit.php
Is there any best practices? Any pros/cons?
For me the best practice is to group exceptions related to their issue.
For example if you have a number of Auth exceptions, like InvalidDetails, UserNotFound put them here
Library/Auth/Exceptions/InvalidDetails.php
Library/Auth/Exceptions/UserNotFound.php
Each exception should be an extension of Zend_Exception ( unless you've extended it yourself )
this way you can do:
throw new Library_Auth_Exception_InvalidDetails("Invalid details when trying to login");
the benefit of using this method is you DONT need to have a message, the Exception name can cover it enough.
My assumptions here is you setup a namespace for Library called Library and everything is within there.
I tend to group everything, so a typical Auth library could be:
Auth/Forms/Login.php
Auth/Exception/InvalidUser.php
Auth/Orm/Abstract.php
Auth/Orm/Doctrine.php
HTH
I've never worked with Zend framework but if this at all helps, I would at least make a common Exception class and all those other ones extend that rather than just make one for each.

Categories