Zend framework2 Zend框架2-通用类
当我在zend framework 2的五月表上更改id_art中的id时,我会收到此消息,我可以添加和删除,但无法更新: 例外情况Zend framework2 Zend框架2-通用类,zend-framework2,Zend Framework2,当我在zend framework 2的五月表上更改id_art中的id时,我会收到此消息,我可以添加和删除,但无法更新: 例外情况 Fichier: C:\wamp\www\zf2\module\Annonces\src\Annonces\Model\AnnoncesTable.php:29 Message: Could not find row 0 Pile d'exécution: #0 C:\wamp\www\zf2\module\Annonces\sr
Fichier:
C:\wamp\www\zf2\module\Annonces\src\Annonces\Model\AnnoncesTable.php:29
Message:
Could not find row 0
Pile d'exécution:
#0 C:\wamp\www\zf2\module\Annonces\src\Annonces\Model\AnnoncesTable.php(48): Annonces\Model\AnnoncesTable->getAnnonces(0)
这是我的脚本模块表:
<?php
namespace Annonces\Model;
use Zend\Db\TableGateway\TableGateway;
class AnnoncesTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
//fonction qui permet d'afficher tous la table "Article"
public function fetchAll()
{
$resultSet = $this->tableGateway->select();
return $resultSet;
}
//fonction qui permet de recuperer tout les id de la table "Article"
public function getAnnonces($id)
{
$id = (int) $id;
$rowset = $this->tableGateway->select(array('id_art' => $id));
$row = $rowset->current();
if (!$row) {
throw new \Exception("Could not find row $id");
}
return $row;
}
/*fonction qui permet de ajouet ou modiier un objet de type "Annonce" dans la table "Article"
en fonction de l'id*/
public function saveAnnonces(Annonces $annonces)
{
$data = array(
'titre_art' => $annonces->titre_art,
);
$id = (int) $annonces->id;
if ($id == 0) {
$this->tableGateway->insert($data);
} else {
if ($this->getAnnonces($id)) {
$this->tableGateway->update($data, array('id_art' => $id));
} else {
throw new \Exception('Annonce id does not exist');
}
}
}
//fonction qui permet de supprimé un article en fonction de l'id
public function deleteAnnonces($id)
{
$this->tableGateway->delete(array('id_art' => $id));
}
}
?>
///////////////////////////控制器操作:
<?php
namespace Annonces\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Annonces\Model\Annonces;
use Annonces\Form\AnnoncesForm;
class AnnoncesController extends AbstractActionController
{
protected $annoncesTable;
public function indexAction()
{
return new ViewModel(array(
'annonces' => $this->getAnnoncesTable()->fetchAll(),
));
}
public function annonceAction()
{
return new ViewModel(array(
'annonces' => $this->getAnnoncesTable()->fetchAll(),
));
}
public function addAction()
{
$form = new AnnoncesForm();
$form->get('submit')->setValue('Add');
//fonction qui permet d'applet la méthode de création et vérification des champs de saisie de formulaire
$request = $this->getRequest();
if ($request->isPost()) {
$annonces = new Annonces();
$form->setInputFilter($annonces->getInputFilter());
$form->setData($request->getPost());
//apré la validation du formaulaire , remplir l'objet de type "Annonce" et applet la méthode "save annonces" pour ajouter ou modifier l'annonce
if ($form->isValid()) {
$annonces->exchangeArray($form->getData());
$this->getAnnoncesTable()->saveAnnonces($annonces);
// Redirect to list of annonces
return $this->redirect()->toRoute('annonces');
}
}
return array('form' => $form);
}
public function editAction()
{
//test si l'id à était saisie pour la rediriction (add/mod)
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('annonces', array(
'action' => 'add'
));
}
//appllé la fonction getAnnoce //
$annonces = $this->getAnnoncesTable()->getAnnonces($id);
$form = new AnnoncesForm();
$form->bind($annonces);
$form->get('submit')->setAttribute('value', 'Edit');
//vérification les valeur de formulaire de la page "edit"
$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter($annonces->getInputFilter());
$form->setData($request->getPost());
//applé la méthode "save" apré la validation de formulaire
if ($form->isValid()) {
$this->getAnnoncesTable()->saveAnnonces($form->getData());
// Redirect to list of annoncess
return $this->redirect()->toRoute('annonces');
}
}
return array(
'id' => $id,
'form' => $form,
);
}
public function deleteAction()
{
//test si l'id à était saisie pour la rediriction
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('annonces');
}
//supprimé l'article si l'utilisateur confirm l'action "del"
$request = $this->getRequest();
if ($request->isPost()) {
$del = $request->getPost('del', 'No');
if ($del == 'Yes') {
$id = (int) $request->getPost('id');
$this->getAnnoncesTable()->deleteAnnonces($id);
}
// Redirect to list of annoncess
return $this->redirect()->toRoute('annonces');
}
return array(
'id' => $id,
'annonces' => $this->getAnnoncesTable()->getAnnonces($id)
);
}
public function getAnnoncesTable()
{
//permet de recupéré les champs de la table "Article"
if (!$this->annoncesTable) {
$sm = $this->getServiceLocator();
$this->annoncesTable = $sm->get('Annonces\Model\AnnoncesTable');
}
return $this->annoncesTable;
}
}
?>
请在您的问题中添加控制器-操作代码,您正在调用saveAnnonces以更新记录。我将控制器添加到我的帖子@kunaldeshe!思考:在公共函数editAction中,$annonces似乎有id值,但$form->getData中没有id值。请执行此操作,以检查id值是否存在-if$form->isValid{echo;print\u rget\u object\u vars$form->getData;exit;[…其余代码]。让我们知道打印的数组是否具有id值。它不返回id:=>int 20 get array[id]=>=>sdq@kunaldeshe多亏了你,我发现了一个诀窍!我的位置:公共函数交换array$data{$this->id=isset$data['id\u art']?$data['id\u art']:null;//我添加此对齐以返回id值$this->id.=isset$data['id']?$data['id']:null;$this->titre\u art=isset$data['titre\u art']?$data['titre\u art']:null;}