Friday, November 4, 2011

Re: How to get related username in view?

On Nov 4, 6:35 pm, Jeremy Burns | Class Outfit
<jeremybu...@classoutfit.com> wrote:
> You're doing something wrong - this is very standard stuff. Have you got the Containable behaviour attached to the Category model? Or app_model?
>

Here is my category model:

<?php
class Category extends AppModel {
var $name = 'Category';
var $displayField = 'title';
//var $recursive = 2;
var $actsAs = array('Containable');
var $recursive = -1;
var $validate = array(
'title' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update'
operations
),
'unique' => array(
'rule' => array('isUnique'),
'message' => 'Title already in use.'
)
),
);
//The Associations below have been created with all possible keys,
those that are not needed can be removed

var $hasMany = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'category_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
?>

And here is the start of my controller:
<?php
class CategoriesController extends AppController {

var $name = 'Categories';

function index() {
$this->Category->recursive = 0;
$this->set('categories', $this->paginate());
}

function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid category', true));
$this->redirect(array('action' => 'index'));
}
$this->Session->write("category_id", $this->_categoryId());
$this->set('category', $this->Category->read(null, $id));
$categories = $this->Category->find(
'all',
array(
'conditions' => array('Category.id' => $id),
'Contain' => array(
'Post' => array(
'User' => array(
'User.id',
'User.name'
)
)
)
)
);
}

I have no idea if your suggested code is in the right place or not.

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: