Tag Archives: cakephp

CakePhp Tip #6: Displaying items that belong to a user

cake-logo

Displaying items that belong to a user:

One of the most common things youll do in cakephp is list items that the user should only see. For example a user should just see their “posts” listed or their “articles” etc.

CakePHP has “Magic Find Types” that can help. For example if i wanted to list all the posts of the user_id =12

In my controller i can create an action as follows;

 $myposts=$this->Posts->findAllByUserId(12);

if i wanted to list all posts of the current logged in user;

public function my(){

	$userid=$this->Auth->user('id');
	$this->Post->recursive = 1;
		
	$this->set('posts', $this->paginate('Post',array('Post.user_id =' => $userid)));
		
	//use the index view to render
	$this->render('index');	
 }

To call view this action;

http:://localhost/mycakeapp/posts/my

Source and more info;
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html

CakePhp tip #5: Joining two fields to create one

Joining two fields to create one (eg to get somebodys name by combining firstname and surname)

cake-logo

You need to create a “VirtualField” in the model;

 public $virtualFields = array('fullname' => 'concat(Patient.firstname, "-", patient.surname)');

Then in your controller, to get a drop downlist;

 $patients = $this->Exercise->Patient->find('list',array('fields'=>array('fullname')));

Credit to:
http://stackoverflow.com/questions/11822942/cakephp-display-multiple-fields-in-a-single-drop-down

Cakephp Tip #3 – Requesting JSON or XML

The newest version of Cakephp makes it even easier to make a JSON or XML request.

Add the request handler component to your controller;

public $components = array('Paginator','RequestHandler' );

create your action in your controller;
eg

public function testjson() {
$this->set('posts', $this->paginate());
$this->set('_serialize', array('posts'));
}

To access either the JSON or XML version simply append .json or .xml to the action name;

http://localhost/myapp/posts/testjson.json

http://localhost/myapp/posts/testjson.xml

Manual:

CakePHP 2 – Tip#1: Display Field instead of ID

Display Field instead of ID:

Whenever CakePHP automagically fetches lists from your tables, it uses the id key for the value and the $displayField for the text.

If your table has a name or title field, CakePHP automatically displays it as the display field. So, either rename the field that you want as your display field (say, candidate_name to just name) or set the $displayField variable in your model:

class Candidate extends AppModel {
    var $displayField = 'candidate_name';
}

Source:
http://stackoverflow.com/questions/4558505/cakephp-related-tables-show-ids-instead-of-values

Manual:

Notepad++ Enable CakePhp .ctp Files

If you are writing code in Notepad++ and using Cakephp youll notice it doesnt highlight .ctp files (CakePhp View Files).

You can fix this quite easily by;

Opening;

C:\Users\[YOURNAME]\AppData\Roaming\Notepad++\

Open teh file called; langs.xml

Scroll down till you see;

  <Language name=”html” ext=”html htm shtml shtm xhtml hta”

Add ctp to the end of it;

<Language name=”html” ext=”html htm shtml shtm xhtml hta ctp”

Save it and reopn Notepad++ your .ctp files should now render correctly.