So basically I am trying to download a file.
I Have the action:
public function getfile() {
$this->autoRender = false;
$accesskey = 'mrPQVeJF8VFXpSq';
$data = $this->File->find('first', array('conditions' => array('File.accesskey =' => $accesskey)));
$filepath = substr($data['File']['path'], 17);
$this->response->file($filepath, array('download' => true, 'name' => $data['File']['name']));
return $this->response;
}
this throws an error : (i guess the line with $this->response->file())
Fatal Error Error: Class 'File' not found File:
C:\wamp\www\project\lib\Cake\Network\CakeResponse.php Line: 1347
The class File is an Utility of cake. So, using it as a model will probably give you trouble. I recommend you change it. Read more about reserve cake and php words here. That may be the cause of your error.
And the other cause of that error is that you are not in FilesController, and are trying to call the Find model there. for that read how to load models from other controllers (look for ClassRegistry::init or $this->loadModel().
I'm just guessing here, because your are "guessing the line giving trouble is $this->response->file()". You may get a more accurate response if you do not guess and debug exactly what line is giving you that error.
Related
I have a problem printing a PDF using CakePHP and DomPDF as soon I want to fetch some data from the Database before printing the pdf. Without fetching the Data, it works like a charme. My function is the following:
public function tourpdf($tourid = null){
$contain = ['Deliveries','Deliveries.Articletransactions','Deliveries.Orders','Deliveries.Orders.Customers',
'Deliveries.Articletransactions.Orderarticles','Deliveries.Articletransactions.Orderarticles.Articles'];
$tour = $this->Tours->get($tourid)
->contain($contain);
$this->viewBuilder()
->className('Dompdf.Pdf')
->layout('Dompdf.default')
->options(['config' => [
'filename' => $filename,
'render' => 'browser',
'size' => 'A4',
'orientation' => 'landscape'
]]);
$this->set('Test', 'Hallo');
$this->set('Tour',$tour);
}
As soon as I set the Data from the last line, that I fetched above, I get the error Message "Fatal error: Cannot redeclare class Dompdf\View\PdfView in .../src/View/PdfView.php on line 66".
However commenting the last line out, the first set with Test is working and the PDF is generated. How can I set data retrieving from a query to the pdf file?
I got a similar error, but with Zend Framework 2. All of a sudden DOMPDF would give an error "Cannot redeclare (previously declared in ...)".
I had PHP functions in some templates and when I removed them I was getting another error "No block-level parent found. Not good.". I noticed this started happening when I upgraded libxml2 from 2.9.4 to 2.9.5 or later.
The solution to both of the problems was to instantiate DOMPDF class like this:
use Dompdf\Options;
$options = new Options();
$options->set('enable_html5_parser', true);
$dompdf = new Dompdf($options);
With enable_html5_parser all the problems went away. Here is some more information on this issue.
Amazing!
Had this issue on Mac 10.12.6
PHP Version 5.6.30
libxml Version 2.9.4
Using the regex to remove whitespace between the tags fixed the issue.
$html = preg_replace('/>\s+</', '><', $html);
I have done a code, in which i create events. When i create event there was a field named "detailed_address" which i have removed now, from database, from model,from the edit page, from every where.
Creating an event works fine. but when i edit that event and save it, there is error as:
The detailed address field is required.
I have checked my code for at-least 5 times there is no word detailed address now used.
controller methods:
public function update(EventRequest $request, $id)
{
$event = Event::findOrFail($id);
$input = $request->all();
$input['days_of_week'] = serialize(Input::get('days_of_week'));
$query = $event->update($input);
return redirect('event');
}
public function store(Request $request)
{
$checkbox = Input::get('days_of_week');
$checkbox_selection = Input::get('agree');
$input = $request->all();
$input['days_of_week'] = serialize($checkbox);
$query = Event::create($input);
return view('event.create');
}
Can any one tell what will be my problem?
As #manix suggested, try running php artisan clear-compiled, then i'd suggest running php artisan cache:clear as well just to make doubly sure it's not a cache issue.
Something that could also be worth looking into is your requests folder (app\http\requests), if you weren't validating the input on the controller it was likely being done via requests which might still be checking for input that isn't coming through.
Is the field also still registered as mass assignable on the model?
Can't see why it'd throw a validation error but it's worth making doubly sure it's gone from there too
I am trying to debug my sql but I am having a hard time. I know I can use this:
<?php echo $this->element('sql_dump'); ?>
to dump the sql but this doesnt (or at least I dont know how to use it) work if I am doing an ajax call. Because the page is not reloaded, the dump does not get refreshed. How can I run my command and debug the sql? Here is the code I have in my controller:
public function saveNewPolicy(){
$this->autoRender = false;
$policyData = $this->request->data["policyData"];
$numRows=0;
$data = array(
'employee_id' => trim($policyData[0]["employeeId"]),
'insurancetype_id'=> $policyData[0]["insuranceTypeId"],
'company' => $policyData[0]["companyName"],
'policynumber' => $policyData[0]["policyNumber"],
'companyphone' => $policyData[0]["companyPhone"],
'startdate'=> $policyData[0]["startDate"],
'enddate'=> $policyData[0]["endDate"],
'note' => $policyData[0]["notes"]
);
try{
$this->Policy->save($data);
$numRows =$this->Policy->getAffectedRows();
if($numRows>0){
$dataId = $this->Policy->getInsertID();
$response =json_encode(array(
'success' => array(
'msg' =>"Successfully Added New Policy.",
'newId' => $dataId
),
));
return $response;
}else{
throw new Exception("Unspecified Error. Data Not Save! ");
}
}catch (Exception $e){
return $this->EncodeError($e);
}
}
The problem is that if the company field in my array is empty, empty, the insert will fail without any error. I know it has failed, though, because of the numrows variable I use. I know the field accepts nulls in the database. It seems like the only way for me to debug this is to look at what SQL is being sent to MySql. Anyone know how to debug it? I am using CakePhp 2.4
I use this approach. I added this method in my AppModel class:
public function getLastQuery() {
$dbo = $this->getDatasource();
$logs = $dbo->getLog();
$lastLog = end($logs['log']);
return $lastLog['query'];
}
and then in any controller you call this like:
debug($this->{your model here}->getLastQuery());
Rather than trying to hack around in CakePHP, perhaps it would be easier to just log the queries with MySQL and do a tail -f on the log file? Here's how you can turn that on:
In MySQL, run SHOW VARIABLES; and look for general_log and general_log_file entries
If general_log is OFF, run SET GLOBAL general_log = 'ON'; to turn it on
In a terminal, run a tail -f logfile.log (log file location is in the general_log_file entry) to get a streaming view of the log as it's written to
This is very helpful for these circumstances to see what's going on behind the scenes, or if you have debug off for some reason.
I have some code in cakephp which produces an error.
Here is the PHP Controller:
$this->loadModel( 'Vote' ); //Newly added by amit start
$vote=$this->Vote->getVote($id,$uid);
$this->set('vote',$vote);
$voteCount = count($vote);
$this->set('voteCount',$voteCount);
$voteShow = $this->Vote->find('all', array(
'fields' => array('SUM(Vote.score) AS score','count(id) as countId'),
'conditions'=>array('Vote.type_id'=>$id),
));
$this->set('voteShow',$voteShow);
model:
public function getVote($id,$uid) {
if (empty($conditions))
$conditions = array('Vote.type' => 'blog',
'Vote.type_id' => $id,
'Vote.user_id' => $uid);
$users = $this->find('all', array('conditions' => $conditions,
'order' => 'Vote.id desc'
));
return $users;
}
That code produces this error:
Error : An internal error has occurred
What does this error mean?
I enabled debug mode: Configure::write('debug', 2); in core.php and it solved my problem.
In my case debug mode was
Configure::write('debug', 0); on live server in core.php.
I set it to Configure::write('debug', 1); and no error was shown this time.
So I again changed my debug mode to Configure::write('debug', 0); because I don't want to show debug error on my live site.
This time no error message shows up.
So just changing the debug mode once in core.php get rid of this error in my case.
If you change your debug mode to 1 and then run those functions which you have changed on local, CakePHP will refresh the cache for all those models which include in those functions. Now you can change back to Configure::write('debug', 0).
Do NOT set Configure::write('debug',2) in production, or you could end up writing sensitive data (such as Query) to user when there will be an error, the reason why debug 2 works is because items in the CACHE are not taken in consideration anymore, so it works. Just delete the cache and leave DEBUG to 0 in production VERY IMPORTANT
Cache can be found here: tmp/cache/
Then again, do NOT delete the 3 folder you'll find there, just delete their content.
I have a method in my controller that handles generic page requests, meant for my public pages. I want to throw a 404 when I can't match what is in the URL and what I know are the static content pages.
So I do this:
$opts = array(
'name' => 'Some message',
'code' => 404,
'message' => 'Your message here',
'base' => $this->base
);
$this->layout = 'blank';
$this->cakeError('error', array($opts));
The problem is I get this error on a 404 request:
Notice (8): Undefined variable: javascript [APP\views\layouts\default.ctp, line 10]
The "$this->layout = 'blank';" is my attempt to redirect to a blank layout file that doens't include any CSS, JS, etc. But it's ignoring this completely and loading the default template, which has JS includes. I'm guessing that the error routines that load 404 pages don't have access to these methods, which is the source of the error.
But I can't figure out how deal with this error.
First, there's a small bug in your code. Your second parameter to cakeError() is already an array. It should be:
$this->cakeError('error', $opts);
Second, you can set
$this->layout = false;
if you want a blank layout.