Archive for the ‘CakePHP’ Category

Converting CakePHP array to xml

Posted on December 11th, 2008 in CakePHP, Php, Programming | No Comments »

Here is a small function to convert cakephp’s array from find statements to xml.

function cakeArrayToXML($arr,$keyParent = "rows"){
$arrayRow = "<$keyParent>";
foreach($arr as $tkey => $trow){
if(is_numeric($tkey)){
$arrayRow .= $this->cakeArrayToXML($trow,"row");
}elseif(is_array($trow)){
$arrayRow .= $this->cakeArrayToXML($trow,$tkey);
}else{
$arrayRow .= "<$tkey><![CDATA[$trow]]></$tkey>”;
}
}
$arrayRow .= “</$keyParent>”;
return $arrayRow;
}

To use it you would do something like
header("Content-type: text/xml;charset:UTF-8");
echo '<?xml version="1.0" ?>';
echo $this->EcOutput->cakeArrayToXML($this->Lyric->find("all",array("limit"=>2)));

Output would be an xml with a parent node of rows and a node name of row on each row, i guess you just have to test it to understand what i mean, haha.
Hope it helps.

How to change frequency on CakePHP’s describe query

Posted on November 15th, 2008 in CakePHP, Php, Programming | 1 Comment »

By default if you set the debug to 0 in your core.php the frequency of executing a describe query is 999 days, but if your developing your application the duration is 10 sections, meaning every 10 sections it would ask your database to describe the tables that you need in your controller. To change the 10 sections to a higher value you could do this.

open configure.php in cake/libs/
locate the function __loadBootstrap
inside this function locate the following
if (Configure::read() >= 1) {
$duration = '+10 seconds';
} else {
$duration = '+999 days';
}

Change the ‘+10 seconds’ according to your requirement, say you want to describe a query every 1 minute, you would change it to
if (Configure::read() >= 1) {
$duration = '+1 minute';
} else {
$duration = '+999 days';
}

Zend Framework inside CakePHP 1.2

Posted on October 12th, 2008 in CakePHP, Php, Programming | 1 Comment »

Zend Framework’s beauty is it’s range of services or web API, like Delicious, Flickr, GData and a lot more. build into it by default. CakePHP’s beauty is that you can extend it, extend it way up to the point where you can import other framework and make use of that framework’s feature. Having said that, Zend inside CakePHP would be totally awesome. The inspiration behind was this blog post http://britg.com/2008/07/07/using-the-zend-framework-in-cakephp/ but i will show you how to use the flickr service.

First download the minimal version of Zend Framework (It will ask you 2-3 times on what you want to download and will ask for you to login/register and i think this is insane). Anyway after downloading extract the files to your folder so that you will have a folder structure like
vendors
---Zend
------Everything that is inside the folder library/Zend

After that, as what britg.com said about the config file, import the following lines in core.php
ini_set('include_path',ini_get('include_path') . PATH_SEPARATOR . '/path/to/vendors/');

Alibris

Then you’re ready to use Zend’s feature in cake.
function getArtistImage($id){
$this->Artist->id = $artistId;
$artistData = $this->Artist->read();
if($artistData){
App::import("vendor",'Zend_Service_Flickr',array("file"=>'Zend/Service/Flickr.php'));
$flickr = new Zend_Service_Flickr('your flickr api key');
$artistName = $artistData['Artist']['name'];
$results = $flickr->tagSearch($artistName);
$this->set(compact(”results”,”artistName”));
}
}

Then in your view
<? foreach ($results as $result): ?>
<img src="<?=$result->Small->uri;?>"
height="<?=$result->Small->height;?>" width="<?=$result->Small->width;?>"
>
<? endforeach; ?>

To further explain a bit
App::import(”vendor”,’Zend_Service_Flickr’,array(”file”=>’Zend/Service/Flickr.php’));
– this imported the flickr api

$flickr = new Zend_Service_Flickr(’your flickr api key’);
– this creates a new instance of the flickr service

$artistName = $artistData['Artist']['name'];
$results = $flickr->tagSearch($artistName);
– this searches the artist’s name on the tags on flickr

Here is an example of this post
http://monmonja.com/music/lyrics/flickr/87

How to save/cache stuff on CakePHP1.2

Posted on October 12th, 2008 in CakePHP, Php, Programming | No Comments »

Here is a small code on how to save or cache stuff, meaning object, array, class, almost anything except view (you can refer to the CakePHP 1.1 document on how to cache views for it still work on cake 1.2, and retrive or get back the cached/saved stuff back.

The following tutorial will use File as Cache Engine and duration is set to 3600000 in the core.php
Cache::config('default', array('engine' => 'File','duration'=> 3600000));
Reason behind why we set it to 3600000 is that if you look at the codes on how it had been implemented, it checks the default duration added the current time and compared to the cache time you specify and if the cache time is greater then the first one then it will not return anything (it spend me hours to find this one out), you can refer to the codes on file.php, line 171 on the folder cake/libs/cache.

Back to the how to save stuff and retrieve it later
function saveArtistData($artistId){
$cacheName = "artist_datas_".$artistId;
$data = $this->Artist->findAll();
Cache::write($cacheName,$data,'+1 weeks');
}
function getArtistData($artistId){
$cacheName = "artist_datas_".$artistId;
return Cache::read($cacheName);
}

Pretty simple? Just use Cache::write to write data (The API here), and Cache::read to read data (API). The data by default will be serialize and by default will be place under app/tmp/cache

Hope this helps

Making CakePhp and Session Work

Posted on September 25th, 2008 in CakePHP, Php, Programming | No Comments »

If you have been developing website that uses cakephp, you might stumble upon an annoying time when your session is not working. Here are some tips on how to make it work.

1) Configure::write(’Security.level’, ‘medium’);
On your core.php search for the line Security.level, by default this is configured to be high. Most of the time changing this will make your session work.

2) Configure::write(’Session.checkAgent’, false);
Also on your core.php, checkAgent will check your HTTP_USER_AGENT, which retrieves your browser and computer information, most of the time and i don’t know why IE will have a problem with this. By default the setting is set to true, try setting it to false and see if your session works.

3) session.cookie_path
Some times when your session.cookie_path is different from the root folder, Session and CakePhp will not work. A simple solution is to set Session.start on core.php to false. And on your AppController (app_controller.php) or on every Controller that you have to use session, set the following on the beforeFilter function
function beforeFilter(){
$this->Session->activate('/');
...
}

GameDuell Inc. - Play Sudoku

4) Headers already out before CakePhp gets the Session
This took me hours before figuring out what’s the problem with my Session and this is the reason why i wrote this post. Anyway, to check if your headers are out before cake fire session_start, first create a dummy page what will definitely fire a session, create it under webroot folder. For example create a file named session.php under webroot with the following code:
<?php
session_start();
$_SESSION['test'] = “testing”;
?>

Access it by your browser (http:/xxxx/session.php).

Next step is to go to your cake folder, then libs folder, then open session.php. Locate for the word “function __startSession” and change the following lines
function __startSession() {
if (headers_sent()) {
if (!isset($_SESSION)) {
var_dump($_SESSION);
$_SESSION = array();
}
return false;
.....

Now definitely there should be something on the $_SESSION, if not or you have an empty array then you have a problem with spaces on your files, you can check the controller your calling from, the models, the components and see if you have a space on the top or the bottom. If your sure you have none, then check if the file was meant to run under linux, windows or unix. If you still have a problem then check the file encoding, if you are using UTF-8 select the one without BOM. If you still have problem then ask google to help you. :)

Mysql REGEXP on Cakephp

Posted on August 27th, 2008 in CakePHP, Programming | No Comments »

Here is a small tutorial on how to use mysql’s regular expression RegExp on cakephp 1.1 or 1.2.

The regular expression will find the records that start with the value of $letter followed by any number of Alphanumeric, space or quote.

Using Query
$condition = "1=1 AND Artist.name REGEXP
'^[$letter][A-Za-z0-9 \']*’”;
$this->paginate(”Artist”,$condition);

Using Array
$condition = array('Artist.name'=>"REGEXP
^[$letter][A-Za-z0-9 ']*”);
$this->paginate(”Artist”,$condition);

The Black Ghost helicopter at Firebox.com
Using Find Method
1.2 (Tested)
$data = $this->Artist->find("all",array("condition"=>
array('Artist.name REGEXP'=>"^[$letter][A-Za-z0-9 ']*”)));
1.1: Tested before don’t know if it still works
$data = $this->Artist->findAll(array(’Artist.name’=>
“REGEXP ^[$letter][A-Za-z0-9 ']*”));

Hope this helps :)