Ngu ngơ học làm web (x49) - CakePHP2 - Lưu trên nhiều bảng

Tiếp theo của: Ngu ngơ học làm web (x48) - CakePHP2 – Hàm end(), upload và xóa tập tin
-----

Phần x49. CakePHP2 – Lưu trên nhiều bảng


Xem (clip số 47 – chickenrainshop):

Trong view ‘edit một cuốn sách’, thực hiện chuyển  ô Tác giả từ multiselect box thành một input kiểu text.

[View\Books\admin_edit.ctp]

<?php
                        echo $this->Form->input('id');
                        echo $this->Form->input('category_id');
                        echo $this->Form->input('title');
                        echo $this->Form->input('slug');
                        echo $this->Form->input('Writer', ['label' => 'Tác giả', 'type' => 'text', 'value' => $writers]);
                        echo $this->Html->image($this->request->data['Book']['image'], ['width'=>140, 'height' => 180]);
                        echo $this->Form->input('image',['label' => '', 'type' => 'file']);
                        echo $this->Form->input('info', ['label' => 'Nội dung', 'class' => 'ckeditor']);
                        echo $this->Form->input('price');
                        echo $this->Form->input('sale_price');
                        echo $this->Form->input('publisher');
                        echo $this->Form->input('publish_date');
                        echo $this->Form->input('link_download');
                        echo $this->Form->input('published');
            ?>

[BooksController.php]

            // xử lý thông tin tác giả trước khi thêm/cập nhật
            private function checkWriter($writerList = null) {
                        $writers = explode(',', $writerList);
                        $this->loadModel('Writer');
                        foreach ($writers as $writer) {
                                    $slug = $this->Tool->slug($writer);
                                    $writerInfo = $this->Writer->findBySlug($slug);
                                    if (empty($writerInfo)) {
                                                $data = [
                                                            'name' => ucwords(trim($writer)),
                                                            'slug' => $slug,
                                                            'biography' => 'Đang cập nhật'
                                                ];
                                                // cập nhật lại id để lưu mẩu tin mới
                                                $this->Writer->create();
                                                $this->Writer->save($data);
                                                $saveInfo[] = $this->Writer->id;
                                    } else {
                                                $saveInfo[] = $writerInfo['Writer']['id'];
                                    }
                        }
                        $this->request->data['Writer']['Writer'] = $saveInfo;
            }

            public function admin_edit($id = null) {
                        if (!$this->Book->exists($id)) {
                                    throw new NotFoundException(__('Invalid book'));
                        }
                        if ($this->request->is(array('post', 'put'))) {
                                    $this->checkSlug($this->request->data, 'Book', 'title');
                                    $this->loadModel('Category');
                                    $category = $this->Category->findById($this->request->data['Book']['category_id']);
                                    $save = true;
                                    if (!empty($this->request->data['Book']['image']['name'])) {
                                                $result = $this->uploadFile($category['Category']['slug']);
                                                if ($result['status']) {
                                                            $location = '/files/'.$category['Category']['slug'].'/'.$result['fileName'];
                                                            $this->request->data['Book']['image'] = $location;
                                                } else {
                                                            $this->Flash->error(__('Không upload được ảnh, vui lòng thử lại!'));
                                                            $save = false;
                                                }          
                                    } else {
                                                unset($this->request->data['Book']['image']);
                                    }
                                    if ($save) {
                                                $this->checkWriter($this->request->data['Writer']['Writer']);
                                                if ($this->Book->save($this->request->data)) {
                                                            $this->Flash->success(__('The book has been saved.'));
                                                            return $this->redirect(array('action' => 'index'));
                                                } else {
                                                            $this->Flash->error(__('The book could not be saved. Please, try again.'));
                                                }
                                    }                                  
                        } else {
                                    $options = array('conditions' => array('Book.' . $this->Book->primaryKey => $id));
                                    $this->request->data = $this->Book->find('first', $options);
                                    if (!empty($this->request->data['Writer'])) {
                                                foreach ($this->request->data['Writer'] as $writer) {
                                                            $writerList[] = $writer['name'];
                                                }
                                                $writers = implode(', ', $writerList);
                                    } else {
                                                $writers = null;
                                    }
                        }
                        $categories = $this->Book->Category->find('list');
                        $this->set(compact('categories', 'writers'));

            }
-----------
Cập nhật 30/7/2017
-----------
Xem thêm:
Tổng hợp các bài viết về Ngu ngơ học làm web