[英]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 ). 我在Zend的文档中找到了此功能,更具体地在“创建模型和数据库表”部分( http://framework.zend.com/manual/1.12/en/learning.quickstart.create-model.html )中。
This is in the Application_Model_GuestbookMapper: 这在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...: 我创建了一个映射器的实例,并尝试将信息从解码后的json字符串传递给它,但仍然出现错误...:
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. 我知道我必须将$ data传递给save()函数,但是我不知道该怎么做...该模型不适合json-url,我只是想展示如何检索和解码json。
Can anybody help me? 有谁能够帮助我?
What you need to pass in to the $mapper->save(); 您需要传递给$ mapper-> save();的内容。 is an instance of Application_Model_Guestbook. 是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: 因此,希望您有一个Application_Model_Guestbook类,您可以在其中定义将数据数组设置为其属性的可能性,例如:
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. 我没有对此进行专门的测试,但是它应该使您了解如何实现自己的目标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.