It's in the right place but you have amended it.
You are populating a variable called $categories - you normally do that for a
list. You are using find 'all' and then restricting it with a condition. Use
find first instead (that was my original mistake). Your $category variable is
being populated with a read. On the assumption that you want the variable
$category to be populated with 'this' category, do this (then check the
variable $category in the view - die(debug($category)); is good for that):
<?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()); //
why are you doing this?
// this->set('category', $this->Category->read(null, $id)); -
redundant
$this->set(
'category',
$this->Category->find(
'first',
array(
'conditions' => array('Category.id' =>
$id),
'Contain' => array(
'Post' => array(
'User' => array(
'User.id',
'User.name'
)
)
)
)
)
);
}
Jeremy Burns
Class Outfit
http://www.classoutfit.com
On 4 Nov 2011, at 18:43, Daniel wrote:
> On Nov 4, 6:35 pm, Jeremy Burns | Class Outfit
> <[email protected]> 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
> [email protected] For more options, visit this group at
> http://groups.google.com/group/cake-php
--
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
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php