简体   繁体   中英

save() function Zend Framework

I have found this function in the documentation from Zend, more specific in the Create model and Database Table section ( http://framework.zend.com/manual/1.12/en/learning.quickstart.create-model.html ).

This is in the Application_Model_GuestbookMapper:

public function save(Application_Model_Guestbook $guestbook)
{
    $data = array(
        'email'   => $guestbook->getEmail(),
        'comment' => $guestbook->getComment(),
        'created' => date('Y-m-d H:i:s'),
    );

    if (null === ($id = $guestbook->getId())) {
        unset($data['id']);
        $this->getDbTable()->insert($data);
    } else {
        $this->getDbTable()->update($data, array('id = ?' => $id));
    }
}

and now i would like to integrate this into my controller, but i have no idea how?

I created an instance of the mapper and tried to pass the info from my decoded json string to it, but I still get errors...:

public function indexAction()
{
   $mapper = new Application_Model_GuestbookMapper();
   $db = Zend_Db_Table_Abstract::getDefaultAdapter();

   $json = file_get_contents('http://data.appsforghent.be/poi/apotheken.json');
   $data = Zend_Json::decode($json);

   foreach($data['apotheken'] as $row)
   {
       $mapper->save();
   }
}

I know i have to pass the $data to the save() function but I have no idea how... The model won't fit the json-url, I just wanted to show how I retrieve and decode the json.

Can anybody help me?

What you need to pass in to the $mapper->save(); is an instance of Application_Model_Guestbook. So hopefully you have a class Application_Model_Guestbook in which you define the possibility to set a data array as its attributes, for example like this:

class Application_Model_Guestbook {
  private $email,$comment,$created;
  public function __construct($data) {
   $this->email = $data['email'];
   // etc add other variables
  }
  public function getEmail() {
   return $this->email;
  }
}

Then to call that, use:

   foreach($data['apotheken'] as $row)
   {
       $guestbook = new Application_Model_Guestbook($row);
       $mapper->save($guestbook);
   }

I have not tested this specifically, but it should give you an idea of how to achieve what you want to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM