How to use CakePHP's database.php file for custom scripts? - database

Can you help me how to use class defined in CakePHP's /config/database.php file, for my custom scripts? I need to use defined array for db connection.
Tnx in adv!

There is nothing magic about this class. It works the same as any other PHP class.
<?php
include("path/to/cake/config/database.php");
$db = new DATABASE_CONFIG;
echo $db->default['login'];
echo $db->default['password'];
echo $db->default['database'];
?>
Now you can reference the variables like you would with any other class.

It sounds like you're looking for something similar to Rails' migrations, i.e. scripts that run within the context of the framework. There have been efforts to create such a thing (see Joel Moss' article in the Bakery).
What I tend to do is to keep a set of DDL scripts in a _meta/ directory of my project. I execute these directly on the database so they have no need to read info from database.php. If you're set on using Cake's runtime context, my way won't help you, but maybe Joel's will.

Related

How can I download a PDF file from a form using UI designer?? Bonita

The thing is I have found how upload a document and after that downolad it. But I just want to download it. I want to do it using the UI designer but I dont know how to do it.
Thanks :)
I dont know which tool are you using to design your UI, anyway this is concerning functionality, not design. In that point, i need to know wich language do you want (or can) use. For example, in PHP, it's very simple, you can make something like:
(create php file) downloadpdf.php
1st: (if you want to generate pdf "on the fly":
<?php
function download($foo){
content headers (type, force-download, etc)
database select to get data or harcode it.
echo data
}
?>
and call this function with some id to select from database or something (ignore if you want to hardcode it)
Other option to download a file, if it's stored on server is making a link to this file (statically or dyamically). If you wanna take control to file downloads, check this post:
http://www.media-division.com/the-right-way-to-handle-file-downloads-in-php/
I don't mean that it can be done with UI designer tools, and it's not concerned if it's from a form or not.
Cheers!
You should create link and variable which type is javascript expression. On Variable value write
return "/bonita/portal/" + $data.context.mainDoc_ref.url;
On link URL write your variable and to text
Download: {{context.mainDoc_ref.fileName}}
Here you can find excellent example for this case

CakePHP doens't support load models?

I use CakePHP 1.3.9 but I can't use other Models in a Controller.
I use $this->loadModel('ModelName); and $this->ModelName->find('all') - always empty.
The variable $uses also doesn't work.
Why is it not working for me?
I used i18n and must set $locale...
Do you mean the data set is empty? If the model is not loaded, you shouldn't be able to call $this->ModelName->find() as $this->ModelName would be null. Does it throw an error? Your usage is correct, as stated in the manual : http://book.cakephp.org/view/992/loadModel
You can also do
App::import('Model', 'ModelName');
$model = new Model();
But I'm guessing that your current resultset is returning empty rather than the model itself not being set.
Have you tried looking at what $this->ModelName actually contains? Do the following and post it here
pr($this->ModelName)
It's considered bad practice to put (un-associated) models in your $uses array.
Depending what you are trying to do, you may be able to make use of containable behaviour.
$this->User->Post->find('all');
If not, you should be able to use loadModel:
$this->loadModel('Article');
$recentArticles = $this->Article->find('all', array('limit' => 5));
To quote Cake:
The loadModel function comes handy when you need to use a model which is not the controller's default model or its associated model.
JohnP and Ross are correct. Controller::loadModel() is clearly working and not your problem if pr($this->ModelName) is working for you.
As they mentioned, you're probably having trouble because the data simply isn't in the database. Or maybe there's something wrong with your query. Have you tried checking the query that's produced by CakePHP and trying to query the database directly through the MySQL command line (assuming you're using MySQL)?
Or is there any chance you've overloaded the Model::find() method?

$session->flash()

I am using cakePHP v1.26.
In the default.ctp file,
I got a single of this code in it:
$session->flash();
I came a corss a web site in which the author suggested using this instead:
if($session->check('Message.flash')){
$session->flash();
}
I do not understand what this line of code is doing:
if($session->check('Message.flash')){...}
what is "Message.flash" in this case?
Is "Message.flash" a custom variable or
a built-in varibale which has been predefined in cakePHP?
Message.flash is the session variable name. It will be defined by cakephp, when you use $this->Session->setFlash('Your message'); from your controller.
if($session->check('Message.flash')){...} checks, if session Message.flash, which contains the flash message, exists.
Note also that contrary to the current manual description, $session->flash() does not echo the result, it just returns it, so you will need to have
echo $session->flash();
in your view.
For latest cakephp version
if(!($this->Session->check('Message.flash')));
// your code
In view section for show messages.
$this->Session->flash();

How to do form-based file uploads in CakePHP?

I have been looking into this for a while and can't figure it out. Basically I have an add page for my model which you can add a map from a URL or from a file upload. I have got all the fields and validation in but how and where do I manage the uploaded file?? There must be some easy way to do this. Thanks!
Firstly your form needs to be set up to allow file uploads.
<?php echo $form->create(Model, array('type' => 'file')); ?>
This will allow any file inputs to actually upload the file to your server with $form->file(field) or $form->input(field, array('type' => 'file')).
Once the file has been uploaded you should handle everything else from within the Model:
function beforeSave($created) {
extract($this->data[Model][field]);
if ($size && !$error) {
move_uploaded_file($tmp_name, destination);
$this->data[Model][field] = destination;
}
return true;
}
These are only the basics, so be sure to have a play around to find the solution that best fits your needs.
You can use Zend Components to handle the file upload. There is a good example here on my website:
CakePHP file upload using Zend Components
NOTE: MeioUploadBehavior has been deprecated. Instead jrbasso suggests the Upload Plugin.
In addition to the fine answers already given I want to hint about MeioUploadBehavior, currently maintained by jrbasso at github, which has been a great help for me in my own CakePHP project.
You simply add the behavior to your model using the $actsAs field and at the same time specifying any custom preferences. Then create necessary fields (described by supplied docs in detail) in your database, or configure the model to not use any database table. Finally setup the form in your add page, also described in the supplied documentation. The behavior will then take care of the rest for you.

Linking to a file (e.g. PDF) within a CakePHP view

I'd like to link to some PDFs in one of my controller views. What's the best practice for accomplishing this? The CakePHP webroot folder contains a ./files/ subfolder, I am confounded by trying to link to it without using "magic" pathnames in my href (e.g. "/path/to/my/webroot/files/myfile.pdf").
What are my options?
EDIT: I didn't adequately describe my question. I was attempting to link to files in /app/webroot/files/ in a platform-agnostic (ie. no mod_rewrite) way.
I've since worked around this issue by storing such files outside the CakePHP directory structure.
$html->link('Pdf', '/files/myfile.pdf');
This is somewhat tangential, but for access to such a location in Models and other places you can simply do this:
$file = WWW_ROOT . DS . 'files' . DS;
This tactic might be helpful to someone accessing files for static data loading, such as XML or JSON.
This is not recommended for public consumption or public linking.
I can confirm that this is an issue when mod_rewrite is not being used.
<?php echo $html->link('pdf', '/files/test.pdf'); ?>
outputs
pdf
it should output
pdf
This should work
<?php echo $html->link('pdf', $this->webroot('files'.DS.'test.pdf'); ?>
I'm not sure I understand the question correctly, but here goes. Basically any file you put in the webroot folder will be accessible on the webserver, so if you put the file in webroot/files/file.pdf you would simply link to /files/file.pdf.
If that doesn't work, please clarify your question...
or..
Link Text
:)
or...
Link Text
Descargar Archivo

Resources