New issue is raised while creating crud operation, I have province module which is similar to Album module. Here I want to add new province as will as delete and update province. I copied code from album, everything is ok, but it shows an error:
Fatal error: Uncaught Error: Class 'Admin\Provinces' not found in C:\xampp\htdocs\new_project\module\Admin\Module.php:61 Stack trace: #0 [internal function]: Admin\Module->Admin{closure} (Object(Zend\ServiceManager\ServiceManager), 'provincestableg...', 'ProvincesTableG...') #1 C:\xampp\htdocs\new_project\vendor\zendframework\zend-servicemanager\src\ServiceManager.php(939): call_user_func(Object(Closure), Object(Zend\ServiceManager\ServiceManager), 'provincestableg...', 'ProvincesTableG...') #2 C:\xampp\htdocs\new_project\vendor\zendframework\zend-servicemanager\src\ServiceManager.php(1099): Zend\ServiceManager\ServiceManager->createServiceViaCallback(Object(Closure), 'provincestableg...', 'ProvincesTableG...') #3 C:\xampp\htdocs\new_project\vendor\zendframework\zend-servicemanager\src\ServiceManager.php(638): Zend\ServiceManager\ServiceManager->createFromFactory('provincestableg...', 'ProvincesTableG...') #4 C:\xampp\htdocs\new_project\vendor\zendframework\zend-servicemanager\src\ServiceManager.php(598): Zend\ServiceMana in C:\xampp\htdocs\new_project\module\Admin\Module.php on line 61
Module.php:
<?php
namespace Admin;
use Admin\Model\Profile;
use Admin\Model\ProfileTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
class Module
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getServiceConfig()
{
return array(
'factories' => array(
'Admin\Model\ProfileTable' => function($sm) {
$tableGateway = $sm->get('ProfileTableGateway');
$table = new ProfileTable($tableGateway);
return $table;
},
'ProfileTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Profile());
return new TableGateway('user', $dbAdapter, null, $resultSetPrototype);
},
'Admin\Model\ProvincesTable' => function($sm) {
$tableGateway = $sm->get('ProvincesTableGateway');
$table = new ProvincesTable($tableGateway);
return $table;
},
'ProvincesTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Provinces());
return new TableGateway('provinces', $dbAdapter, null, $resultSetPrototype);
},
),
);
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}
ProvincesController.php:
<?php
namespace Admin\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Admin\Model\Provinces;
use Admin\Form\ProvincesForm;
class ProvincesController extends AbstractActionController
{
protected $provincesTable;
public function getAuthService()
{
$this->authservice = $this->getServiceLocator()->get('AuthService');
return $this->authservice;
}
public function indexAction()
{
$this->layout("layout/layout_admin");
// below condition is new and for back browser validation
if (!$this->getServiceLocator()
->get('AuthService')->hasIdentity()) {
return $this->plugin(redirect)->toRoute('login', array('action' => 'index'));
}
return new ViewModel(array(
'pk_provinces' => $this->getProvincesTable()->fetchAll(),
));
}
public function addAction()
{
$form = new ProvincesForm();
$form->get('submit')->setValue('Add');
$request = $this->getRequest();
if ($request->isPost()) {
$provinces = new Provinces();
$form->setInputFilter($provinces->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$provinces->exchangeArray($form->getData());
$this->getProvincesTable()->saveProvinces($provinces);
// Redirect to list of albums
return $this->redirect()->toRoute('provinces');
}
}
return array('form' => $form);
}
public function editAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('provinces', array(
'action' => 'add'
));
}
// Get the Album with the specified id. An exception is thrown
// if it cannot be found, in which case go to the index page.
try {
$provinces = $this->getProvincesTable()->getProvinces($id);
}
catch (\Exception $ex) {
return $this->redirect()->toRoute('provinces', array(
'action' => 'index'
));
}
$form = new ProvincesForm();
$form->bind($provinces);
$form->get('submit')->setAttribute('value', 'Edit');
$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter($provinces->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$this->getProvincesTable()->saveProvinces($provinces);
// Redirect to list of albums
return $this->redirect()->toRoute('provinces');
}
}
return array(
'id' => $id,
'form' => $form,
);
}
public function deleteAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('provinces');
}
$request = $this->getRequest();
if ($request->isPost()) {
$del = $request->getPost('del', 'No');
if ($del == 'Yes') {
$id = (int) $request->getPost('id');
$this->getProvincesTable()->deleteProvinces($id);
}
// Redirect to list of albums
return $this->redirect()->toRoute('provinces');
}
return array(
'id' => $id,
'provinces' => $this->getProvincesTable()->getProvinces($id)
);
}
public function getProvincesTable()
{
if (!$this->provincesTable) {
$sm = $this->getServiceLocator();
$this->provincesTable = $sm->get('Admin\Model\ProvincesTable');
}
return $this->provincesTable;
}
}
ProvincesForm.php:
<?php
namespace Admin\Form;
use Zend\Form\Form;
class ProvincesForm extends Form
{
public function __construct($name = null)
{
// we want to ignore the name passed
parent::__construct('provinces');
$this->add(array(
'name' => 'id',
'type' => 'Hidden',
));
$this->add(array(
'name' => 'p_name',
'type' => 'Text',
'options' => array(
'label' => 'Province',
),
));
$this->add(array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'value' => 'Go',
'id' => 'submitbutton',
),
));
}
}
Model/Provinces.php:
<?php
namespace Admin\Model;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
class Provinces implements InputFilterAwareInterface
{
public $id;
public $p_name;
protected $inputFilter;
public function exchangeArray($data)
{
$this->id = (!empty($data['id'])) ? $data['id'] : null;
$this->p_name = (!empty($data['p_name'])) ? $data['p_name'] : null;
}
public function getArrayCopy()
{
return get_object_vars($this);
}
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$inputFilter->add(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
));
$inputFilter->add(array(
'name' => 'p_name',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
Model/ProvincesTable.php:
<?php
namespace Admin\Model;
use Zend\Db\TableGateway\TableGateway;
class ProvincesTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchAll()
{
$resultSet = $this->tableGateway->select();
return $resultSet;
}
public function getProvinces($id)
{
$id = (int) $id;
$rowset = $this->tableGateway->select(array('id' => $id));
$row = $rowset->current();
if (!$row) {
throw new \Exception("Could not find row $id");
}
return $row;
}
public function saveProvinces(Provinces $provinces)
{
$data = array(
'p_name' => $provinces->p_name,
);
$id = (int) $provinces->id;
if ($id == 0) {
$this->tableGateway->insert($data);
} else {
if ($this->getProvinces($id)) {
$this->tableGateway->update($data, array('id' => $id));
} else {
throw new \Exception('Province id does not exist');
}
}
}
public function deleteProvinces($id)
{
$this->tableGateway->delete(array('id' => (int) $id));
}
}