Sunteți pe pagina 1din 2

from me to you.

– Abdulhakim Khalib Haliru

My assumption about you: you are not exactly a newbie, you know your way round the windows file
system.Buzz words like OOP, ORM,Controllers,Models,Views don’t really give you the creep

//this is targeted at cakephp 1.2.x

/*

So you are a CakePhp baker (lol) and you need to write an action that affects your whole app. Just
some little action that does no harm to your app performance.

*/

Quickly make use of the beforeRender method and place it in you app_controller.php file

e.g.

//I made this when suddenly I realize I could have suspended users, logged in and then verify if they
//have been suspended and Bop! Tell the user, sorry you have been suspended.

function beforeRender()

parent::beforeRender();
if ($this->Auth->user('suspend')==1)
{
$this->redirect('/undpusers/logout/1');
}
}

//Yeah yeah, you wanna see my logout method , huh ? ok

function logout($suspended=null)
{
if($suspended==1)
{
$this->Session->setFlash('This account has been suspended Please contact administrator via email <br/>
johndoe@example.com);
}
else
{
$this->Session->setFlash('Logout Successful');
}
$this->redirect($this->Auth->logout());
}
/*
Good thing cakephp has got features to spare you lotta SQL debugging time and helps you
ensure healthy relationship in models, but sometimes you just wish you could exhibit your
old inefficient php manners and find yourself getting a bit frustrated. Hey relax think through
and you may come up with something like this or similar.

Here, I needed to concatenate fields like username,lastname to make FULLNAME, loop


through my resulting models and make another array out of it, hence when I pass the final
array to my Pear ExcelWriter , I don’t have unexpected results.
*/

If ($this->Session->read('selReports'))
{
foreach($this->Session->read('selReports') as $v)
{
$this->$model->recursive=2;
$result[] = $this->$model->
find('all',array('conditions'=>array($model.'.'.$field=>$value,$model.'.'.strtolower($assocMode
l).'_'.$assocField=>$v,$assocModel.'.'.$assocField=>$v)));
}

for($i=0;$i<count($result);$i++)
{

//Concatenation
//observe $conEdDataset is a new array to hold my records whilst I manipulate through
//Observe the 3 models, ‘User’,’Conrpt’,’Consultant’

$conEdDataset[$i][]=$result[$i][0]['Consultant']['User']['firstname'].'
'.$result[$i][0]['Consultant']['othername'].' '.$result[$i][0]['Consultant']['User']['lastname'];
$conEdDataset[$i][]=$result[$i][0]['Conrpt']['experience_in_full'];
$conEdDataset[$i][]=$result[$i][0]['Conrpt']['age'];
$conEdDataset[$i][]=$result[$i][0]['Consultant']['gender'];
$conEdDataset[$i][]=$result[$i][0]['Consultant']['Consultantsq'][0]['insqualtype'];
$conEdDataset[$i][]=$result[$i][0]['Consultant']['maritalstatus'];
$conEdDataset[$i][]=$result[$i][0]['Consultant']['User']['email'];
$conEdDataset[$i][]=$result[$i][0]['Consultant']['telmobile'];
}
$this->Session->write('dataset',$conEdDataset);

$conEdHeaders=array('Name','Experience','Age','Gender','Education','Marital
Status','Email','Mobile Phone');

$this->Session->write('datasetHeaders',$conEdHeaders);

//Sorry mate, this is an excerpt from a long Action in one of my controllers. From here I instantiate my
Excel class and continue my ride. But I hope this is clear enough for you to get by.

S-ar putea să vă placă și