2014-06-12 27 views
0

我希望表單域在表單頁面打開時包含數據庫中包含的先前數據。我在這裏經歷了大量的查詢,並且知道使用populate()或bind()方法是實現它的方法。但是當我嘗試使用它時,我得到一個未定義的方法錯誤。 有沒有其他方法可以做到這一點? 我也無法使用bind()。我在提交後獲得了一個帶有默認值的新表單。 對不起,這是一個愚蠢的問題。自從我開始學習Zend框架以來,這隻剩下4-5天。另外,我上網的大多數方法都是針對較老的框架。我正在使用Zend Framework2。無法讓bind()工作

這是控制器代碼

<?php 
class ChatController extends AbstractActionController 
{ 
    protected $chatTable; 

    public function indexAction() 
    { 

     $form = new ChatForm(); 
     $model= new Chat(); 
     $form->bind($model); 
     $form->get('submit')->setValue('Save'); 

     $request = $this->getRequest(); 
     if ($request->isPost()) { 
      $gen_set = new Chat(); 
      $form->setInputFilter($gen_set->getInputFilter()); 
      $form->setData($request->getPost()); 

      if ($form->isValid()) { 
       $gen_set->exchangeArray($form->getData()); 
       $this->getChatTable()->saveChat($gen_set); 

       // Redirect to list of albums 
       return $this->redirect()->toRoute('chat'); 
      } 
     } 
     return array('form' => $form); 
    } 

    public function getChatTable() 
    { 
     if (!$this->chatTable) { 
      $sm = $this->getServiceLocator(); 
      $this->chatTable = $sm->get('Chat\Model\ChatTable'); 
     } 
     return $this->chatTable; 
    } 

}

我的實體類,這裏API_KEY和anon_prefix是列「設置」行,還有一個柱值。

<?php 
class Chat implements InputFilterAwareInterface 
{ 

    protected $inputFilter;       

    public function exchangeArray($data) 
    { 
     $this->api_key=(isset($data['api_key'])) ? $data['api_key'] : null; 
     $this->anon_prefix = (isset($data['anon_prefix'])) ? $data['anon_prefix'] : null;  
    } 


    // Add content to these methods: 

    public function setInputFilter(InputFilterInterface $inputFilter) 
    { 
     throw new \Exception("Not used"); 
    } 

    public function getInputFilter() 
    { 
     if (!$this->inputFilter) { 
      $inputFilter = new InputFilter(); 

      $inputFilter->add(array(
       'name'  => 'iflychat_external_api_key', 
       'required' => true, 
       'filters' => array(
        array('name' => 'StripTags'), 
        array('name' => 'StringTrim'), 
       ), 
      )); 

      $inputFilter->add(array(
       'name'  => 'iflychat_show_admin_list', 
       'required' => true, 
       'validators' => array(
        array(
         'name' => 'InArray', 
         'options' => array(
          'haystack' => array(1,2), 
         ), 
        ), 
       ), 
      )); 


      $this->inputFilter = $inputFilter; 
     } 

     return $this->inputFilter; 
    } 

    public function getArrayCopy() 
    { 
     return get_object_vars($this); 
    } 
} 

這是用來值輸入到數據庫

<?php 
class ChatTable 
{ 
    protected $tableGateway; 

    public function __construct(TableGateway $tableGateway) 
    { 
     $this->tableGateway = $tableGateway; 
    } 

    public function fetchAll() 
    { 
     $resultSet = $this->tableGateway->select(); 
     return $resultSet; 
    } 


    public function saveChat(Chat $gen_set) 
    { 

     $data = array(          
      'value' => $gen_set->api_key, 
     ); 
     $id='iflychat_external_api_key'; 
     $this->tableGateway->update($data,array('settings' => $id)); 

     $data = array(
      'value' => $gen_set->anon_prefix, 
     ); 
     $id='anon_prefix'; 
     $this->tableGateway->update($data,array('settings' => $id)); 
    } 
} 

我收到此錯誤,「不能使用類型爲聊天\型號\聊天作爲陣列的對象

+0

檢查ZF2中的$ form-> bind($ model);從數據庫加載數據(通常用於編輯操作)。爲了使用form-submit之後發佈的數據來填充表單,執行此操作 - '$ request = $ this-> getRequest(); if($ request-> isPost()){$ form-> setData($ request-> getPost()); }' –

+0

''model'有很多屬性時'bind()'非常有用。如果你想手動設置數據,那麼試試這個 - $ form-> get('id') - > setValue($ model-> id);''其中'id'是表單中的一個字段。 –

+0

顯示您的代碼。 –

回答

0

你的行動沒有按」儘管它很有意義,但您將Chat實例實例化爲$model,後來實例化爲$gen_set。你應該做的是綁定第一個,然後使用表單類getData方法稍後返回你綁定的實例,以及你在setData方法中給出的值。不需要從對象到陣列再進行任何轉換。

下面是它應該如何看...

public function indexAction() 
{ 

    $form = new ChatForm(); 

    // bind the model 
    $model= new Chat(); 
    $form->bind($model); 

    $form->get('submit')->setValue('Save'); 

    $request = $this->getRequest(); 
    if ($request->isPost()) { 
     $form->setInputFilter($gen_set->getInputFilter()); 

     // set data from POST as properties of the bound model ... 
     $form->setData($request->getPost()); 

     if ($form->isValid()) { 

      // get the bound model instance with the POSTed values 
      // ($gen_set is now the original $model object instance bound above) 
      $gen_set = $form->getData(); 

      // and save it 
      $this->getChatTable()->saveChat($gen_set); 

      // Redirect to list of albums 
      return $this->redirect()->toRoute('chat'); 
     } 
    } 

    return array('form' => $form); 
} 
+0

我確實意識到我在發佈後創建了2個聊天實例。但是這仍然沒有幫助。由於我的表單ID和它的值存儲在2列的行中,我想我們在這裏做的是提取列值。 :/ – user2740957

0

控制器代碼 -

<?php 

class ChatController extends AbstractActionController { 

    protected $chatTable; 

    public function indexAction() { 

     $model = $this->getChatTable()->fetchLastChat(); 
     if($model === null || $model->count() == 0) 
      $model = new Chat(); 

     //Now if no record exists in the database then $model will be empty 
     //Else $model will contain data of last record. 

     $form = new ChatForm(); 
     $form->bind($model); 
     $form->get('submit')->setValue('Save'); 

     $request = $this->getRequest(); 
     if ($request->isPost()) { 
      $gen_set = new Chat(); 
      $form->setInputFilter($gen_set->getInputFilter()); 
      $form->setData($request->getPost()); 

      if ($form->isValid()) { 
       $gen_set->exchangeArray($form->getData()); 
       $this->getChatTable()->saveChat($gen_set); 
      } 
     } 
     return array('form' => $form); 
    } 

    public function getChatTable() { 
     if (!$this->chatTable) { 
      $sm = $this->getServiceLocator(); 
      $this->chatTable = $sm->get('Chat\Model\ChatTable'); 
     } 
     return $this->chatTable; 
    } 

} 

ChatTable類代碼 -

<?php 

//Other use statements 

use Zend\Db\Sql\Select; 

class ChatTable { 

    protected $tableGateway; 

    public function __construct(TableGateway $tableGateway) { 
     $this->tableGateway = $tableGateway; 
    } 

    public function fetchAll() { 
     $resultSet = $this->tableGateway->select(); 
     return $resultSet; 
    } 

    public function fetchLastChat() { 
     $select = new Select('TABLE_NAME'); //Change the tablename accordingly 
     $select->order('PRIMARY_KEY DESC'); //Set the Primary Key of the table 
     $select->limit(1); 

     $resultSet = $this->tableGateway->selectWith($select); 
     return $resultSet->current(); 
    } 

    //Rest of the Code .... 

請把這個想法從上面的代碼。