cakephp 2 disabled cache after save - cakephp-2.0

after insert or delete i don't wish delete view cache, but cake do it automatically.
How disabled clear cache after save, delete functions?
My try, but still the same:
if($this->User->save($data, array('callbacks' => false)))
Thanks

If I understand you correctly, you want to add this to your model or AppModel.
public function afterSave($created) {
Cache::clear();
parent::afterSave($created);
}
Remove parent::afterSave($created); if in the model itself.
If you don't want to empty the cache completely, you can target via Cache::delete($key) or by group: Cache::clearGroup($groupname);

Related

Multiple classes in Ext JS to update and share the same config settings

I am fairly new to using Ext JS 4.0.7, and have recently discovered the Config options, so I have set up the following config class, which initialises with the following:
Ext.define('CurrentRowSelection', {
extend: 'Ext.app.Controller',
config: {
currentAccountID: {
$value: 'Empty',
cached: true,
evented: true
}
},
initialize: function(config) {
this.callParent(config);
}
});
I am able to update this config quite nicely from another class's listener event, which is right clicking on a row record and grabbing the currentAccountID value. However, every time I try to access the new, updated, config from a new class after initialising - it keeps reverting back to the 'Empty' value.
The following is used to set the config from the class containing the listener:
var CurrentRowSelection = new CurrentRowSelection();
CurrentRowSelection.setCurrentAccountID(1);
However, when I create a new constructor in a new class, with the hope of accessing this newly updated config from elseware, my console.log is telling me that it is continuously reverting back to the default values, as it seems to be initialising each time a new constructor is created - which makes sense, but I need to stop it.
How can I do thisand make sure that this value is saved all the time, every time? Thank you.
Don't make new controllers every time. Every controller should be instantiated only once, because if you instantiate multiple controllers of the same type, every event binding you prepare in the control method is executed multiple times (the control method usually is the most important method of a controller, although there may be exceptions.)
So every controller should exist as a single instance only, and that singleton would be accessed using getController wherever and whenever you need it:
MyAppName.app.getController("ControllerName").setCurrentAccountID(1)
That said, I would recommend to look into using stores for data storage, not controllers. Stores offer the possibility to synchronize the settings with a server (create, read, update, delete). If you want to create a singleton store, you would give it a storeId and use Ext.getStore() to access the store.
For example, the config settings in my application are in the first record of the store with storeid AllSettings:
Ext.getStore("AllSettings").getAt(0).get("groupDiscoveryCacheHours")

Appending new data to the Backbone Relational Relation

I'm trying to design a load more type of system where every time you press load more you add data to the existing collection. This is a rough sketch of how the UI looks.
Everything works pretty great except as you would except everytime I re-run the
items.fetch
What it does: It overrides the entire collection with the new data
What I want it to do: I want the new records returned to be added to the records collection not override old 'records'
How can I make this work so that the newly fetched records are appended to existing records and not overridden?
Add { remove: false } to your fetch call:
items.fetch({ remove: false, data: { nextId: this.info.get('nextId') } });
What's happening here is Collection#fetch will synchronize its models with the data the server returns. That includes adding new models, updating models already present, and removing models that are not included in the response.
The behavior of fetch can be customized by using the available set options. For example, to fetch a collection, getting an "add" event for every new model, and a "change" event for every changed existing model, without removing anything: collection.fetch({remove: false})
The available set options are add, remove, and merge. Setting one (or all) to false will disable that functionality in the fetch.
It sounds like you just want { remove: false }, leaving the add and merge functionality.
I'm not familiar with backbone-relational,
But with a normal collection, you can do the following in parse method:
Backbone.Collection.extend({
parse: function(response){
// you can update the info here like this.info = response.info or similar
return _.union(this.toJSON(), response.records);
}
});
Basically we combine the response with existing data and return it so that we maintain the previous data when the collection updates

how to do auto fetch in backbone

I have a collection of models I fetch from a REST API every 10 seconds. (collection.fetch() every 10 seconds with a timer).
The user can also edit the model in a dialog box and click Save going back to the table of models.
How do I prevent cases where the user saves a model in a dialog and the auto fetch exactly comes back with a stale model so the model stays with the stale data until the next auto fetch.
Two suggestions:
Use collection.fetch({ update: true }) - that way models will only be add/remove/change'd rather than recreated on each fetch.
When the model is edited via the your dialog box, only save() the specific attributes that the user changed, like model.save(changedData, { patch: true }); -- using this patch behavior will make sure you're only sending the attributes that were just changed. Then your server can respond with the other recently-changed attributes, and all should be fine.

saving data for another model, and problems with redirection, in cakephp

When I save data in one model, I'd like to create some data in another model and save that too. Because I can't do this using beforeSave(), I eventually decided to use afterSave() to create new data items in my second model. I'm not writing a blog application, but to use the blog analogy it's equivalent to automatically creating a series of comments for every blog post that is added and, when a post is edited, deleting all comments and re-adding new comments:
class Post extends AppModel {
function afterSave() {
ClassRegistry::init('Comments')->deleteAll(array('Post.id' => $this->id));
ClassRegistry::init('Comments')->saveAll($comments); // comments contains the comments to be added
}
}
This works fine, apart from the fact that the afterSave() function causes the redirection from my controller's add/edit actions (to /posts/index) to be overruled, and I get redirected back to the add/edit form instead (if I comment out the entire afterSave() method, the redirection works as intended).
If you're wondering why I didn't put the logic in the controller, I did originally, but I want it to work for both add and edit actions, and also for a "batch" add action I use to add multiple "posts" at once.
I guess I have two questions:
Is there a better way to achieve this
kind of result?
How can I make the redirection work?
Thanks for reading this far and if I haven't explained it clearly I hope you can use your imagination to see what I'm trying to do.
I think you need to include return true at the end of your afterSave() function. This seems like a decent approach if you don't want to put it in the controller. Although I would think about whether you will always want to add these comments (or whatever they are) after every save, even single-field updates.
In the (somewhat unlikely) event that someone else has the same problem, I eventually found that setting
'atomic' => false
in the saveAll() options solves the problem with the redirect. I have no idea why.
You can't do?:
<?php
function edit_post($id = null) {
if (!$id && empty($this->data)) {
// error...
}
if (!empty($this->data)) {
if ($this->Post->save($this->data)) {
ClassRegistry::init('Comments')->deleteAll(array('Post.id' => $this->Post->id));
ClassRegistry::init('Comments')->saveAll($comments); // comments contains the comments to be added
} else {
// error...
}
}
if (empty($this->data)) {
$this->data = $this->Post->read(null, $id);
}
}
?>

CakePHP error 'page not found' error - how to avoid

I need some background information about CakePHP, and how it works...
let's say that i have method (function) defined in CakePHP's controller, ie. deleteItem, like
function deleteItem( $id = null )
{
$this->PublicationNumeration->delete( $id, true );
}
The CMS I developed works ok, proper record is deleted ($id), and it works fine.
But if I try to call this method from browser, I am getting the error 'page not found'.
Is it possible to skip that error (no matter how)?
For development mode, set debug value to 2 in app/config/core.php:
Configure::write('debug', 2);
If you set debug value to 2, you can get detailed message of what happening. From your description, there are two possibilities:
record with given id has been deleted, or
view from current action is not exist. Usually in delete action, you
don't create view but redirect it to
somewhere else (which you not do in
code above).
If you want, you can create your own custom error pages in /views/errors
Make sure you're including the controller's name in the URL as well. If your controller's class name is CategoriesController and you want to delete an item with an ID of 4, for example, make sure you're going to this address in the browser:
http://www.example.com/categories/deleteItem/4
The page is not found because you haven't created it (deleteItem.ctp). After the delete statement, put a redirect to the page you want to return to, usually an index page after a delete operation.

Resources