-3

i have 2 table in database.there are categories and posts.

categories have id , name

posts have id , category

and i have 2 files.

  1. PostsController.php

  2. edit.ctp

i edit category by edit.ctp when i save its

image >> http://s704.photobucket.com/albums/ww41/018115496/edit.png

It is not save category's name but save a category's id into Posts.

image >> http://i704.photobucket.com/albums/ww41/018115496/index.png

This is my code

PostsController.php

class PostsController extends AppController {
            public $helpers = array('Html', 'Form');
            public $uses = array('Post','Category');
  public function index() {
                         $this->set('posts', $this->Post->find('all'));
                }


 public function edit($id = null) {
                    $this->loadModel('Category');
                    $categories = $this->Category->find("list",array('field'=>array('Category.name')));
                    $this->set("categories", $categories);
                    if (!$id) {
                        throw new NotFoundException(__('Invalid post'));
                    }
                    $post = $this->Post->findById($id);
                    if (!$post) {
                        throw new NotFoundException(__('Invalid post'));
                    }
                    if ($this->request->is(array('post', 'put'))) {
                        $this->Post->id = $id;
                        if ($this->Post->save($this->request->data)) {
                            $this->Session->setFlash(__('Your post has been updated.'));
                            return $this->redirect(array('action' => 'index'));
                        }
                        $this->Session->setFlash(__('Unable to update your post.'));
                    }
                    if (!$this->request->data) {
                        $this->request->data = $post;
                    }
            }
    } 

and edit.ctp

<?php
echo $this->Form->create('Post');
echo $this->Form->input('category', array('options' => $categories));
echo $this->Form->end('Save Post');
?> 

i think i because find("list") but i don't know how to solve. Thank you.

bbk
  • 1
  • You are editing posts via the posts controller. If you wand to add new Categories, then you will need to do it via the categories views and controller respectively. – Colonel Mustard Feb 15 '15 at 11:19

1 Answers1

0

I have fixed your problem, You need to replace this

//PostsController.php
public function edit($id = null) {
        $this->loadModel('Category');
        $categories = $this->Category->find("list",array('field'=>array('Category.name','Category.name')));
        $this->set("categories", $categories);
        if (!$id) {
            throw new NotFoundException(__('Invalid post'));
        }
        $post = $this->Post->findById($id);
        if (!$post) {
            throw new NotFoundException(__('Invalid post'));
        }
        if ($this->request->is(array('post', 'put'))) {
            $this->Post->id = $id;
            if ($this->Post->save($this->request->data)) {
                $this->Session->setFlash(__('Your post has been updated.'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(__('Unable to update your post.'));
        }
        if (!$this->request->data) {
            $this->request->data = $post;
        }
}

When calling find('list'), the fields passed are used to determine what should be used as the array key and value, and optionally what to group the results by. By default, the primary key for the model is used for the key, and the display field (which can be configured using the model attribute displayField) is used for the value. Some further examples to clarify:

$justCategories = $this->Category->find('list', array(
    'fields' => array('Category.name')
));
$Categories = $this->Category->find('list', array(
    'fields' => array('Category.name', 'Category.name')
));

With the above code example, the resultant vars would look something like this:

    $justCategories = Array
(
    //[id] => 'name',
    [1] => 'Books',
    [2] => 'Baby',
    [3] => 'Business & Industrial'
)

$Categories = Array
(
    //[name] => 'name',
    ['Books'] => 'Books',
    ['Baby'] => 'Baby',
    ['Business & Industrial'] => 'Business & Industrial'
)

Read find(‘list’) in CakePHP

Supravat Mondal
  • 2,574
  • 2
  • 22
  • 33