public function saveAttribute(JosAttribute $attribute)
 {
     $data = array('attribute_code' => $attribute->getAttributeCode(), 'year_id' => $attribute->getYearId(), 'frontend_input' => $attribute->getFrontendInput(), 'frontend_label' => $attribute->getFrontendLabel(), 'value_table' => $attribute->getValueTable());
     $attribute_id = (int) $attribute->getAttributeId();
     if ($attribute_id == 0) {
         $this->tableGateway->insert($data);
     } else {
         if ($this->getAttributeByArrayConditionAndArrayColumn(array('attribute_id' => $attribute_id), array('attribute_code'))) {
             $this->tableGateway->update($data, array('attribute_id' => $attribute_id));
         } else {
             return false;
         }
     }
     return true;
 }
 public function editAction()
 {
     // edit form
     $edit_form = new EditAttributeForm($this->getServiceLocator());
     $request = $this->getRequest();
     if ($request->isPost()) {
         $post = $request->getPost();
         $edit_form->setData($post);
         if ($edit_form->isValid()) {
             // khai báo model bảng jos_year
             $jos_year_table = $this->getServiceLocator()->get('NamHoc\\Model\\JosYearTable');
             // lấy year_id default
             $years = $jos_year_table->getYearByArrayConditionAndArrayColumn(array('is_active'));
             // không tồn tại year default
             if (!$years) {
                 $this->flashMessenger()->addErrorMessage('Lỗi. Bạn muốn thêm attribute vào năm học nào? Vui lòng kiểm tra lại!');
                 return $this->redirect()->toRoute('thuoc_tinh/crud', array('action' => 'index'));
             }
             // tạo điểm truy cập csdl jos_attribute
             $jos_attribute_table = $this->getServiceLocator()->get('Attribute\\Model\\JosAttributeTable');
             // kiểm tra id vừa post có tồn tại không
             $attribute = $jos_attribute_table->getAttributeByArrayConditionAndArrayColumn(array('attribute_id' => $post['attribute_id']), array('value_table'));
             if (!$attribute) {
                 $this->flashMessenger()->addErrorMessage('Lỗi, thuộc tính cần sửa không tồn tại. Vui lòng kiểm tra lại!');
                 return $this->redirect()->toRoute('thuoc_tinh/crud', array('action' => 'index'));
             }
             // kiểm tra với năm hiện tại có tồn tại dòng nào có tên như trên chưa
             // nếu có kiểm tra có khác id không
             $jos_attribute_exist = $jos_attribute_table->getAttributeByArrayConditionAndArrayColumn(array('attribute_code' => $post['attribute_code'], 'year_id' => $years[0]['year_id']), array('attribute_id'));
             if ($jos_attribute_exist and $post['attribute_id'] != $jos_attribute_exist[0]['attribute_id']) {
                 $this->flashMessenger()->addErrorMessage('Lỗi. Tên thuộc tính đã tồn tại. Vui lòng kiểm tra lại!');
                 return $this->redirect()->toRoute('thuoc_tinh/crud', array('action' => 'index'));
             }
             // luu lại
             $new_attribute = new JosAttribute();
             $new_attribute->exchangeArray($post);
             $new_attribute->setYearId($years[0]['year_id']);
             $new_attribute->setValueTable($attribute[0]['value_table']);
             $jos_attribute_table->saveAttribute($new_attribute);
             if ($post['frontend_input'] != 'Select') {
                 // tạo điểm truy cập csdl bảng attribute_option
                 $jos_attribute_option_table = $this->getServiceLocator()->get('Attribute\\Model\\JosAttributeOptionTable');
                 // xóa option cũ
                 $jos_attribute_option_table->deleteAttributeOptionByAttributeId($post['attribute_id']);
             }
             // thông báo thành công
             $this->flashMessenger()->addSuccessMessage('Chúc mừng, cập nhật thuộc tính thành công!');
             return $this->redirect()->toRoute('thuoc_tinh/crud', array('action' => 'index'));
         }
     }
     $this->flashMessenger()->addErrorMessage('Lỗi, thực thi không thành công. Vui lòng kiểm tra lại!');
     return $this->redirect()->toRoute('thuoc_tinh/crud', array('action' => 'index'));
 }