Tag Archives: CakePHP

More CakePHP notes

Get the authentication details in a view. Permission based so in a user with low rights you need to specifically identify the property you want

<?php debug($this->Session->read('Auth.User')); // works with power user ?>
<?php debug($this->Session->read('Auth.User.username')); // works everywhere ?>

If you want to make a banned view (under ACL) accessible, in the Controller use Auth->Allow:

public function beforeFilter() {
	parent::beforeFilter();
	// this makes the index visible to everyone
	$this->Auth->allow('index', 'view');
}

 

CakePHP Personal Notes

Just links and things as I learn my way around CakePHP

https://github.com/mcurry/cakephp_static_user – didn’t work

Realised that the acl plugin should be in app/Plugin not /plugins
http://book.cakephp.org/2.0/en/plugins.html

Get data from another controller.
http://stackoverflow.com/questions/9205540/cakephp-how-to-retrieve-data-from-another-model-in-controller

$this->Result->Quiz->Question->find('count')

This found all the questions – not, as I first thought, the questions for that quiz.

http://devblog.springest.com/how-to-find-related-data-in-habtm-models/
Helped me to

    public function number_of_questions()
    {
        $x = $this->Question->Quiz->find('all',array(
            'conditions' => array(
                'Quiz.id' => $this->id
            )));
        return(count($x[0]['Question']));        
    }

Not sure what the 0 should be.  Not very “CakePHP ish”. Then

$this->Quiz->number_of_questions()

Uploads:
https://github.com/blueimp/jQuery-File-Upload

CakePHP eureka moments

It is a very  steep learning curve, and I keep forgetting what I learn, so just going to keep a list.

  1. To make a link to ‘id’ on a .ctp page link to something more useful change the column name thus:
    echo $this->Html->link($powerpoint['User']['id'], array('controller' => 'users', 'action' => 'view', $powerpoint['User']['id'])); 
    
    //becomes
    
    echo $this->Html->link($powerpoint['User']['username'], array('controller' => 'users', 'action' => 'view', $powerpoint['User']['id']));

    (The query is doing a join automatically in the background)

  2. To make a droplist show a useful thing like the name rather than the key, you need to go in to the XxxxxxController and change the function thus:
    $users = $this->Powerpoint->User->find('list');
    
    //becomes
    
    $users = $this->Powerpoint->User->find('list',array('fields' => array('User.id', 'User.username')));

    I guess it then used the two fields as given.  If you add a third field it is used as a label in the option.  Not sure how to concatenate the columns if you want “Firstname Lastname” in the droplist.

  3. The Model reflects the database. I knew that but recording it here, as I actually used it.
  4. If you have a table that is joined through another table (belongsto > belongsto) then you can make it appear thus in the Controller:
    		$users = $this->Question->Powerpoint->find('list', array(
    				"fields" => array('Powerpoint.user_id', 'Users.username'),
    				"joins" => array(
    					array(
    						"table" => "Users",
    						"type" => "LEFT",
    						"conditions" => array(
    							"Powerpoint.user_id = users.id"
    						)
    					)
    				)
    			));