CSV not to be exported in Magento - magento

I have set the following Code within WebController.php
/**
* Export order grid to CSV format
*/
public function exportCsvAction()
{
$fileName = sprintf('stores-%s.csv', date('Ymd'));
$grid = $this->getLayout()->createBlock('adminhtml/web_web_grid');
$this->_prepareDownloadResponse($fileName, $grid->getCsvFile());
}
I don't know what is the problem that causing CSV not to be exported.
Following error is showing -
Fatal error: Call to a member function getCsvFile() on a non-object in
/var/www/projects/tmp/storelocator/app/code/local/Company/Web/controllers/Adminhtml/WebController.php
on line 0
Please help!
Thanks in Advance.

This line :
$grid = $this->getLayout()->createBlock('adminhtml/web_web_grid');
returns null. The error tells you it clearly :
Fatal error: Call to a member function getCsvFile() on a non-object
Are you sure this 'adminhtml/web_web_grid' relates to a block ?
Do you have a folder/file Web/Web/Grid.php in the Block folder of the adminhtml module ?
For example, if you didn't overload the Mage_Adminhtml module, you should have a class named Mage_Adminhtml_Block_Web_Web_Grid in the folder app/code/local/Mage/Adminhtml/Block/Web/Web/Grid.php that's what the createBlock('adminhtml/web_web_grid') is searching for

Please make sure that you calling right block(Block must be extended by Mage_Adminhtml_Block_Widget_Grid or it might be grid like)
then use your code
You might test by following code by putting in action (its magento core code)..........
$fileName = 'customer.csv';
$content = $this->getLayout()->createBlock('adminhtml/customer_grid')->getCsvFile();
$this->_prepareDownloadResponse($fileName, $content);

Related

How to get file as a resource with Storage?

I am trying to lock a file with the flock function but I am not able to do it. I work with Laravel 8 and Storage Class.
The code is as follows:
$disk = Storage::disk('communication');
$file_name = 'received.json';
$file_exists = $disk->exists($file_name);
if($file_exists){
flock($disk->get($file_name), LOCK_EX);
...
}
The problem I'm having is that when I invoke the get() function on the file path, it returns the contents of the file (a string), which causes the following error:
flock() expects parameter 1 to be resource, string given
I need to know how to get file as a resource and not the content of the file.
Could someone help me and tell me how to do it?
Thank you very much in advance.
You can use Storage::readStream() method
if($file_exists){
$stream=Storage::disk('communication')->readStream($file_name);
flock($stream, LOCK_EX);
}
As per php doc
flock(resource $stream, int $operation, int &$would_block = null): bool
First param needed stream.flock() allows you to perform a simple
reader/writer model which can be used on virtually every platform
(including most Unix derivatives and even Windows).
Ref:https://www.php.net/manual/en/function.flock.php

Use of undefined constant Laravel

Controller Code:
$tokenAmount = json_decode($token->getBody())->result;
return view('account')->with(compact('tokenAmount'));
View Code:
<label>Wallet Balance {{tokenAmount}}</label>
When I run the code. I'm getting following error.
Use of undefined constant tokenAmount - assumed 'tokenAmount'
PS: I tried following method also.
$tokenAmount = json_decode($token->getBody())->result;
return view('account', compact('tokenAmount'));
If I do echo $tokenAmount, it is printing the value without any error.
Please update your view code and check like:
<label>Wallet Balance {{tokenAmount}}</label>
to
<label>Wallet Balance {{$tokenAmount}}</label>

Firing an Event in Laravel 5

In an attempt to learn using events I have followed this tutorial
In my App\Events\ThingWasDone.php i have this
Session::put('testevent', 'it works!');
$author= Entity::find(261);
$author->still_active_URL = 99;
$author->save;
The Session is properly defined and I can get it in my View files.
Any attempt to write to DB produces this error:
LogicException in Model.php line 2632:
Relationship method must return an object of type
Illuminate\Database\Eloquent\Relations\Relation
I tried options like passin or not passing any variable:
\Event::fire(new ThingWasDone($object->id));
\Event::fire(new ThingWasDone());
Didn't help.
Any hint?
change
$author->save;
to:
$author->save();
This might solve the error.

No Error for Undefined Variable Used in Class Methods in PHP

I spent more than 10 hours to find out the typo for debugging my PHP program. I expected PHP would produce an error when using an undefined variable. But when it is used as an object in a method, it doesn't. Is there a reason for it?
<?php
$oClass = new MyClass;
// $oCless->tihs('key', 'taht'); //<-- triggers the error: Notice: Undefined variable
$oClass->tihs('key', 'taht');
echo $oClass->arr['key'];
class MyClass {
public $arr = array('key' => 'foo');
function tihs($key, $value) {
$tihs->arr[$key] = $value; //<-- this does not cause an error message.
}
}
?>
Normally if the error reporting level is set to E_ALL | E_STRICT (or E_ALL as of PHP 5.4.0) it should spit out an E_STRICT error. For instance, this code:
error_reporting(E_ALL | E_STRICT);
$tihs->adrr = 453;
Produces:
Strict Standards: Creating default object from empty value in [...]
Interestingly enough, if you specifically create an array instead of an ordinary variable as a property of an object that doesn't exist, e.g.:
error_reporting(E_ALL | E_STRICT);
$tihs->adrr[25] = 453;
No strict standards error is shown! It looks like this could potentially be something PHP folks might want to fix, because I'm not aware this is documented nor I think there's a legitimate reason for this behaviour.
For the record, in both cases regardless of the error a new stdClass is being created on the fly instead, like sberry mentions in his answer.
It is because of PHP trickery...
Under the covers, PHP is actually creating an object called tihs, adding an array to the object called arr and setting key to value.
Here is a print_r($tihs); after the assignment:
stdClass Object
(
[arr] => Array
(
[key] => taht
)
)
You seemed to have misspelt $oClass as $oCless
Agreed, $oCless instead of $oClass would give you an undefined variable error.
Also, "this" is a keyword in most languages and may be in php as well. You should refrain from using it so that it doesn't come out in other languages as a habit. You'll get way more errors if you're using "this" as function and variable names. You wouldn't even get things to compile.

Flex HSlider Data tip function

Hi I'm trying to pass in a function to change the tool tip for a HSlider in flex 4 but I keep getting the following error no matter how I try defining the function:
Error #1006: value is not a function
Here's the code
function positiveNumberTips(value:Number):Number
{
return calculations.roundToPrecision((value * -1),2);
}
metricSlider.dataTipFormatFunction(positiveNumberTips);
6 MetricSliders, which are HSliders, are created dynamically so that is why I cannot define the dataTipFormatFunction in a declaration as usual.
The positiveNumerTips function is being used by other static sliders and it is working correctly.
Thanks for any help.
Try this: Your Syntax is wrong.
metricSlider.dataTipFormatFunction=positiveNumberTips;

Resources