In case the index controller is useful, here it is.
function index($categoryID = null) {
if (!empty($categoryID)) {
App::import('Model','Category');
$cat = new Category();
$category = $cat->field('title', array('id' =>
$categoryID));
$this->set('category', $category);
$this->set('categoryID', $categoryID);
$conditions = array(
'conditions' => array(
'Category.id' =>
$categoryID),
'order' => 'Subcategory.order
ASC'
);
$subcategories = $this->Subcategory->find('all',
$conditions);
} else {
$this->set('category', 'All');
$conditions = array(
'order' => array(
'Category.title' =>
'ASC',
'Subcategory.order' =>
'ASC'
)
);
$subcategories = $this->Subcategory->find('all',
$conditions);
}
$this->Subcategory->recursive = 0;
$this->set('subcategories', $subcategories);
}
On Feb 14, 11:48 am, adam_g2000 <[email protected]> wrote:
> Thank you Jeremy, you've saved me a great deal of time going forward.
> I now really have got to grips with the base routing and have
> redeveloped the work done so far using your suggested method - much
> quicker, easier to read through the code and tighter. I can't thank
> you enough.
>
> Before I continue, I switched from passing the title to the ID, some
> of the users titles featured : in them, and the obvious started to
> happen, passed in the URL Cake was interpreting them as key, value...
>
> However I still have the problem with redirect. It doesn't seem to be
> sending the variable, or my controller is not reading it. When I
> reference the variable in the view, I get the error "Notice 8:
> Undefined variable: categoryID [APP/views/subcategories/index.ctp,
> line 44]" Here is my revised code:
>
> function edit($id = null, $categoryID = null) {
> App::import('Model','Category');
> $cat = new Category();
> $category = $cat->field('title', array('id' => $categoryID));
> $this->set('category', $category);
> if (!$id && empty($this->data)) {
> $this->Session->setFlash(__('Invalid subcategory',
> true));
> $this->redirect(array('action' => 'index'));
> }
> if (!empty($this->data)) {
> if ($this->Subcategory->save($this->data)) {
> $this->Session->setFlash(__('The subcategory
> has been saved',
> true));
> $this->redirect(array(
> 'controller' => 'subcategories',
> 'action' => 'index',
> $categoryID
> ));
> } else {
> $this->Session->setFlash(__('The subcategory
> could not be saved.
> Please, try again.', true));
> }
> }
> if (empty($this->data)) {
> $this->data = $this->Subcategory->read(null, $id);
> }
> $categories = $this->Subcategory->Category->find('list');
> $resources = $this->Subcategory->Resource->find('list');
> $this->set(compact('categories', 'resources'));
> }
>
> In the view...
>
> <div class="subcategories form">
> <?php echo $this->Form->create('Subcategory');?>
> <fieldset>
> <legend><?php __('Edit Subcategory'.$category); ?></legend>
> <?php
> echo $this->Form->input('id');
> echo $this->Form->input('title');
> echo $this->Form->input('order');
> echo $this->Form->input('status');
> echo $this->Form->input('category_id');
> echo $this->Form->input('Resource');
> ?>
> </fieldset>
> <?php echo $this->Form->end(__('Submit', true));?>
> </div>
> <div class="actions">
> <h3><?php __('Actions'); ?></h3>
> <ul>
>
> <li><?php echo $this->Html->link(__('Delete', true),
> array('action'
> => 'delete', $this->Form->value('Subcategory.id')), null,
> sprintf(__('Are you sure you want to delete # %s?', true),
> $this->Form->value('Subcategory.id'))); ?></li>
>
> <li><?php echo $this->Html->link(__('List Subcategories',
> true),
> array('action' => 'index'));?></li>
> <li><?php echo $this->Html->link(__('List Categories', true),
> array('controller' => 'categories', 'action' => 'index')); ?> </li>
> <li><?php echo $this->Html->link(__('New Category', true),
> array('controller' => 'categories', 'action' => 'add')); ?> </li>
> <li><?php echo $this->Html->link(__('List Resources', true),
> array('controller' => 'resources', 'action' => 'index')); ?> </li>
> <li><?php echo $this->Html->link(__('New Resource', true),
> array('controller' => 'resources', 'action' => 'add')); ?> </li>
> </ul>
> </div>
>
> ... I know it's working to this point because this line is acting as a
> test <legend><?php __('Edit Subcategory'.$category); ?></legend> and
> it displays (for example) *Edit SubcategoryFashion Design*, which must
> mean that the edit method received the variables for category and is
> working with them, so they should be available to 'redirect', yet when
> redirect bounces back to the subcategories/index page, it defaults to
> the ELSE part of the IF statement.
>
> Once again, thanks for looking Jeremy, if you (or any others) can
> offer any further advice I'd be hugely grateful.
>
> Adam.
>
> On Feb 12, 5:51 pm, Jeremy Burns | Class Outfit
>
>
>
> <[email protected]> wrote:
> > Take out your customer route - you don't need it. Just use links in the
> > format:
>
> > echo $this->Html->link(
> > 'Text',
> > array(
> > 'controller' => 'controller_name',
> > 'action' => 'action_name',
> > $variable
> > )
> > );
>
> > Cake will sort the rest out for you. $variable can be the id if you want,
> > or the category.
>
> > The reason you are getting the funny url is because you are specifying
> > names for the third and forth parameters (id and category), so they appear
> > with a colon before them. If you really insist on sending over two
> > parameters, just do this:
>
> > echo $this->Html->link(
> > 'Text',
> > array(
> > 'controller' => 'controller_name',
> > 'action' => 'action_name',
> > $variable,
> > $category
> > )
> > );
>
> > Your receiving action will just match the variables with the parameters in
> > expects in order:
>
> > function action($variable = null, $category = null) {}
>
> > The same holds true for a redirect, where the syntax would be:
>
> > $this->redirect(
> > array(
> > 'controller' => 'controller_name',
> > 'action' => 'action_name',
> > $variable,
> > $category
> > )
> > );
>
> > Note that the 'Text' has gone but the array is the same.
>
> > Jeremy Burns
> > Class Outfit
>
> > [email protected]http://www.classoutfit.com
>
> > On 10 Feb 2011, at 21:25, adam_g2000 wrote:
>
> > > Hi Guys,
>
> > > Firstly, this is my first post, so please be gentle and my apologies
> > > for any inevitable faux pas should I make any. Secondly, I am aware
> > > there seems to be much confusion around redirect, I have been studying
> > > both the Cookbook, CakePHP Application Development (Book) and a series
> > > of tutorials in Linux Format. I have been learning for a couple of
> > > months now - though fully admit to being a newbie.
>
> > > I'm expecting my problem to be simple, and down to a basic lack of
> > > understanding of redirect, so again, I apologise if this is answered
> > > elsewhere - if it is and I've read it, I haven't understood it.
>
> > > I have a series of resources, each belonging (HABTM) to a subcategory,
> > > each subcategory belongs to one category. At the start of the App you
> > > click on the title of the category, which then filters by passing,
> > > using custom routing, the name of the category to the subcategory
> > > controller eg:
>
> > > // Provides the category title to the subcategory index/list page
> > > Router::connect(
> > > '/subcategories/:category',
> > > array('controller' => 'subcategories', 'action' => 'index'),
> > > array(
> > > 'pass' => array('category')
> > > )
> > > );
>
> > > function index($category = null) {
> > > if (!empty($category)) {
> > > $this->set('category', $category);
> > > $conditions = array(
> > > 'conditions' => array('Category.title'
> > > => $category),
> > > 'order' => 'Subcategory.order
> > > ASC'
> > > );
> > > $subcategories = $this->Subcategory->find('all', $conditions);
> > > } else {
> > > $this->set('category', 'All');
> > > $conditions = array(
> > > 'order' => array(
> > > 'Category.title' => 'ASC',
> > > 'Subcategory.order' => 'ASC'
> > > )
> > > );
> > > $subcategories = $this->Subcategory->find('all', $conditions);
> > > }
> > > $this->Subcategory->recursive = 0;
> > > $this->set('subcategories', $subcategories);
> > > }
>
> > > *This code is only relevant for background info, not the source of the
> > > fault.*
>
> > > This works well. Each link provides the data in the correct url format
> > > to each subsequent 'thing', subcategory index or resource index.
>
> > > The problem I have is with editing. When I need to Edit a Subcategory
> > > (for example, this will need to work for Resources also) I pass
> > > parameters (category being the one used to filter, so the one I'm most
> > > interested in) to the edit controller, and in turn to the view like
> > > so:
>
> > > From subcategory index.ctp:
>
> > > <?php echo $html->link('Edit', array(
> > > 'controller' => 'subcategories',
> > > 'action' => 'edit',
> > > 'id' => $subcategory['Subcategory']['id'],
> > > 'category' => $category
> > > )); ?>
>
> > > Which is received well enough by the edit controller (I display my
> > > category in the edit view as a test).
>
> > > So I know my controller has the variable at it's disposal.
>
> > > function edit($id = null, $category = null) {
> > > $this->set('category', $category);
> > > if (!$id && empty($this->data)) {
> > > $this->Session->setFlash(__('Invalid subcategory', true));
> > > $this->redirect(array('action' => 'index'));
> > > }
> > > if (!empty($this->data)) {
> > > if ($this->Subcategory->save($this->data)) {
> > > $this->Session->setFlash(__('The subcategory has been
> > > saved',
> > > true));
> > > $this->redirect(array(
> > > 'controller' => 'subcategories',
> > > 'action' => 'index',
> > > 'id' => $id,
> > >
>
> ...
>
> read more »
--
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