Output caching in codeigniter displays blank page - codeigniter

I've read a lot of discussions about this but have no luck.
I have a controller:
function get_list() {
$data['promoname'] = $this->Default_model->get_promo($this->input->post('year'));
$this->output->cache(10);
// Render view
$this->layout->render(get_platform().'/promo/promo_view', $data);
}
And in my Default_model:
function get_promo($year = NULL) {
$this->db->cache_on();
//query here
}
When the page is loaded for the first time, the data is displayed perfectly. But when I tried to load it again to check if it is cached it displays nothing, a blank white page. Error reporting is enabled.
A file has been created under application/cache, meaning there is no permission issue. But the file created contains '1357184142TS--->'.
Tried to remove the $this->db->cache_on(), but the behavior is still the same. What seems to be the problem?

Check your view file to see if any tags are missed or any values getting interrupted in-between.

As documentation https://ellislab.com/codeigniter/user-guide/general/caching.html says:
Because of the way CodeIgniter stores content for output, caching will only work if you are generating display for your controller with a view.
so you have to call:
$this->load->view(get_platform().'/promo/promo_view', $data)
instead of
$this->layout->render(get_platform().'/promo/promo_view', $data);

I am having a same problem. I haven't properly tested it though. I think the blank page is caused by using ($this->db->last_query()) in the Code with Nested Queries.
Try changing the $db['default']['db_debug'] = TRUE; to false. It may resolve the problem if other system queries are not running.

Related

Best way share data across all view in Laravel

I have some data that I want to share across all views. I am using AppServiceProvider's boot method to share data.it is working fine with MySQL however with pgsql when I run composer or PHP artisan commands I am getting the following error,(i do fresh and seed database very often)
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "news" does not exist
it took me a while to understand where is the real issue. I m still unable to understand if it is pgsql related error or something else, bellow is code in AppServiceProvider. if I comment this code everything works fine except where I m using this.
public function boot()
{
$activeClubs = (new TournamentService())->getAllActiveClubs();
$activeNews = (new TournamentService())->getActiveNews();
$activeTournaments = (new TournamentService())->getActiveTournament();
View::share(['activeClubs' => $activeClubs, 'activeTournaments' => $activeTournaments, 'activeNews' => $activeNews]);
}
can you please help me that how can i share data across all views that i don't get this error in future.
you can determine if the app is running from console or not and prevent loading data in console
if ( !app()->runningInConsole() ){
// it's not console.
}
you mentioned you want to load this data in all views but sometimes, its one view that loaded in all of your views e.g. the layout.app view.
if its the case I recommend using view composers instead of View::share() as it will pass the data to that view before render and it will only run if that view is included in the page (so basically it will solve your problem even without app()->runningInConsole() condition required )
view composers doc
simple example:
View::composer('view-name', function ($view) {
$view->with('key', 'value');
});

Laravel cache null error

I'm using File Cache in Laravel 5.3. Everything works fine. After few days in production I have many errors in my laravel.log that WHERE was called on NULL value (from cache). Why is this happening? Every time I open page everything works fine without errors. Can this be caused because of bots accessing my page? I have not idea what I'm doing wrong...
CacheServiceProvider.php
// insert settings into cache
Cache::add('settings', Settings::with('langs')->get(), env('CACHE_TIMEOUT'));
Helpers.php
$value = Cache::get('settings')->where('name', $name)->first()->value;
I tried to use conditions if cache value exists then use it but it's really anoying to put it everywhere. And what if value doesn't exists? Should I call database query?
in Helpers.php I would suggest you using:
$settings = Cache::remember('settings', env('CACHE_TIMEOUT'), function() {
return Settings::with('langs')->get();
});
$value = !empty($settings) ? $settings->where('name', $name)->first()->value : null;
Remove what you have in CacheServiceProvider.php because this code will fire once when it is needed to get data and cache it for next time.
Hope that helps!

Why are images uploaded through the admin panel of my custom module, having the file name of Array?

Here is my BrandController.php
https://gist.github.com/a958926883b9e7cc68f7#file-brandcontroller-php-L53
I've gone through all my files of my custom module, and compared them to the one given from the custom module maker, and I couldn't find much differences.
Are you attempting to upload multiple files? If you're using multiple fileupload elements with the same name you'll get an array of items.
So when the following line is called,
//this way the name is saved in DB
$data['filename'] = $_FILES['filename']['name'];
It will have the value
["name"]=>array(2) {
[0]=>string(9)"file0.txt"
[1]=>string(9)"file1.txt"
}
you'll need to update the code to loop through each $_FILES['filename']['name'] and upload and save the files separately.
You may unknowingly uploaded multiple files. If you that is not your intention, you may check your in your HTML and check the name attribute of the tag. It must not be an array (like this).
<input type="file" name="my_files[]" />
If you only see Array() in your database, it means you are indeed uploading a multiple files. You can process them by using loops.
If you are really sure that you are uploading 1 image, you may follow #Palanikumar's suggestion. Use a print_r() and display the $_FILES and paste it here. IF you don't want to use that, You can use
json_encode($the-data-you-are-going-to-insert-to-the-database);
If you don't know where to put the print_r() function, you may put it after line 56 of this file.
https://gist.github.com/desbest/a958926883b9e7cc68f7#file-brandcontroller-php-L53
if(isset($_FILES['filename']['name']) && $_FILES['filename']['name'] != '') {
print_r($_FILES);
die;
If saveAction() is being called inside an ajax function you need to log the ajax response. Assuming you are using jquery..
$ajaxResponse = $.POST({...});
console.log($ajaxResponse.responseText);
Then, you you can view it inside a browser's console. If nothing appears, you may use a non-async request
$ajaxResponse = $.POST({
// your options,
// your another option,
async: FALSE
});
Usually file upload will return in array format. So that each uploaded file will have the information like name, type, size, temporary name, error. You can get the file information using print function (print_r($_FILES)). So if you want to display name of the file you have to use something like this $_FILES['filename']['name']
Use print function and debugging tool then save file information using loops.
For more info please check here.
You aren't setting the enctype of the form so the image will never be sent. updated the code to
$form = new Varien_Data_Form(array( 'enctype' => 'multipart/form-data'));

Load single post using Ajax for WPML based site

I want to load single posts into an index page using Ajax for a WPML based site that I'm working on. Not sure what I'm missing but there seems to be a catch when using multiple languages and fetching the correct posts.
Here's the code I wrote -
function loadProject(reference) {
console.log(window.location.origin + '/references/' + reference);
// Displays "http://mysite.com/references/example-post/", which exists
$('#content').load(window.location.origin + '/references/' + reference, function() {
console.log('Load was performed.');
});
}
I don't reach the JS log after load() but I don't receive any error message either (e.g. "File not found").
"References" is a custom post type that I've created using the Types plugin, and has a file of it's own called single-references.php. I'm not entirely sure how CPTs work with WPML, and that's probably where the problem lies. I have two languages, Swedish and English, and the CPT only has one slug for its kind ("references").
[Solved]
Used the .get() method instead to retrieve the post and now it works.
Well, no console error, no network query :
it is not a WPML issue
did you check #content really exists ?
In chrome dev tool, go to Network, and trigger your function. You'll see what you receive.

Magento 1.7: Non-system product attribute not saving in PHP script

I have a script that has loaded a product using
$product = Mage::getModel("catalog/product")->loadByAttribute('sku', $data['id']);
If I modify or retrieve a system attribute, such as weight, everything works fine:
$product->getWeight(); // returns the correct weight
$product->setWeight(5.0); // correctly sets weight
I have two custom attributes: google_product_category and mpn. If I try to modify them using the same technique, like so:
$product->setMpn('123ABC');$product->setGoogleProductCategory('Electronics');the call does not throw an error but the data does not save (yes, I am calling $product->save() afterwards).
What am I doing wrong?
Can you post your scripts? Are you on developer mode?
It looks like the problem is because of store area
Take a look at Mage_Catalog_Model_Product -> setOrigData
public function setOrigData($key=null, $data=null)
{
if (Mage::app()->getStore()->isAdmin()) {
return parent::setOrigData($key, $data);
}
return $this;
}
When saving a product, it needs to compare the data with origdata.
It can retrieve origdata if it is run from 'admin' area. If your scripts run from 'default' area, it won't be able to get the origdata.
So that if you're on developer mode, in some point of code there gonna be a warning thrown that will broke your script (I may be wrong because you said no error was thrown)
You can do it by changing Mage::app('default') into Mage::app('admin')
or using Mage::app()->setCurrentStore('admin');

Resources