0
votes

I am a pre-newbie with cakephp. I have a "users" table and a "category" table

user belongsTo category (fields: users.id, users.name, users.category) category hasMany users (fields: category.id, category.name, users.category)

I am addressing associating data like this.

in (users) edit.ctp I put

// view/Users/edit.ctp

    echo $this->Form->input('name');
    echo $this->Form->input('categories', array( 'value' => $this->Form->value('User.category'), 
'name'=>'data[User][category]') );
</pre>

in users controller I have
<pre>
    public function edit($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->User->read(null, $id);
        }
        $sexes = $this->User->Sex->find('list');
        $categories = $this->User->Category->find('list');
        $this->set(compact('categories'));

    }

Everything is working, but i suspect there's a much easier way to do that form. Is this realy needed?
, array( 'value' => $this->Form->value('User.category'), 'name'=>'data[User][category]')
Without theese params the select box does not highlight the selected option, and nothing is saved.

Could be something like

echo $this->Form->input('Category.name');

for instance? But code like that does not show a select box.
And it doesn't save the users.category field.

I was not able to find any tutorial or code with samples about this. Links would be appreciated.

1

1 Answers

0
votes

Try to use Cake's nameing conventions for database tables and fields. If you follow the conventions Cake will do a lot of the heavy lifting for you:

users table:

users.id, users.name, users.category_id

categories table

categories.id, categories.name

User Model

class User extends AppModel {

    public $belongsTo = array(
        'Category'
    );

}

Category Model

class Category extends AppModel {

    public $hasMany = array(
        'User'
    );

}

Users Controller:

class UsersController extends AppController {

    public function edit($id) {

        if (!empty($this->request->data) {

            // form submitted - try to save
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash('User updated');
            }
            else {
                $this->Session->setFlash('Please correct the errors');
            }
        }
        else {
           // prepopulate the form
           $this->request->data = $this->User->findById($id);
        }

        // populate the categories dropdown
        $this->set('categories', $this->User->Category->find('list');

    }

}

/app/views/Users/edit.ctp

<?php

echo $this->Form->create();
echo $this->Form->inputs();
echo $this->Form->end('Update');