逸岸,你不必为你的等级创建一个新的模块型号 =>把它放在你的相册模块:)
它不是多样信息模块个好主意......每一个表是很过分:d
正如我看你还是有麻烦我的先例aswer后,我写了评论和所有的代码,这样就可以更好地理解:)
该工厂的ServiceManager是一个概念非常重要非常了解!
所以,你首先必须使你的module.php文件出厂配置:
namespace Album;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Album\Model\Album;
use Album\Model\AlbumTable;
use Album\Model\Category;
use Album\Model\CategoryTable;
class Module
{
public function getServiceConfig()
{
return array (
'factories' => array(
//here already the AlbumTable config
// here begin the categoryTable factory
'Album\Model\CategoryTable' => function($sm)
{
//get the tableGateway just below in his own factory
$tableGateway = $sm->get('CategoryTableGateway');
//inject the tableGateway in the Table
$table = new CategoryTable($tableGateway);
return $table;
},
//here is the tableGateway Factory for the category
'CategoryTableGateway' => function($sm)
{
//get adapter to donnect dataBase
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
// create a resultSet
$resultSetPrototype = new ResultSet();
//define the prototype of the resultSet
// => what object will be cloned to get the results
$resultSetPrototype->setArrayObjectPrototype(new Category());
//here you define your database table (category)
//when you return the tableGateway to the CategoryTable factory
return new TableGateway('category', $dbAdapter, null, $resultSetPrototype);
},
),
),
}
}
创建模块/专辑/ src目录/专辑/型号您Category.php文件:
namespace Album\Model;
class Category
{
public $id;
public $name;
public function exchangeArray(){
$this->id = (isset($data['id'])) ? $data['id'] : null;
$this->name = (isset($data['name'])) ? $data['name'] : null;
}
}
建立在同一个文件夹中的文件CategoryTable.php(只有使用fetchall()需要选择输入法,你只是想所有类别的列表)
namespace Album\Model;
use Zend\Db\TableGateway\TableGateway;
use Album\Model\Category;
class CategoryTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchAll()
{
$resultSet = $this->tableGateway->select();
return $resultSet;
}
public function getCategory($id){
// ...
}
public function saveCategory(Category $category)
{
// ...
}
public function deleteCategory($id)
{
// ...
}
}
不要忘记添加在您的AlbumTable :: saveAlbum()方法的新领域CAT_ID;)
创建表单和他的专辑/表夹筛选
namespace Album\Form;
class AlbumForm{
public function __construct($cat_options){
$this->add(array(
'name' => 'id',
'required' => false,
'type' => 'hidden',
));
$this->add(array(
'name' => 'cat_id',
'required' => true,
'type' => 'Select',
'options' => array(
'value_options' => $cat_options,
'label' => 'Category',
)
'attributes' => array(
'value' => 3, // to select a default value
)
));
//and the rest of the form
$this->add(array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'id' => 'submitbutton',
'value' => 'Go',
),
));
}
}
注意,我把在与Form相同的命名空间中的特定类中的筛选器。 =>当你在一个模块中有很多表单和模型时,更容易找到... =>我喜欢这样,所以我推荐它:D =>您可以为同一个模型注册,登录和resetPassword形式,例如,所有使用相同的用户类
namespace Album\Form;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
class AlbumFilter implements InputFilterAwareInterface
{
protected $inputFilter;
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' => false,
'filters' => array(
array('name' => 'Int'),
),
));
$inputFilter->add(array(
'name' => 'cat_id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
));
$inputFilter->add(array(
'name' => 'artist',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 45,
),
),
),
));
$inputFilter->add(array(
'name' => 'title',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 45,
),
),
),
));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
而现在,一切都被配置,使控制器:)
namespace Album\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Album\Model\Album;
use Album\Form\AlbumForm;
class MyControler extends AbstractActionController
{
private $CategoryTable;
public function getAlbumsTable(){
if(!$this->Album){
// get AlbumTable from the ServiceLocator factory (defined in Module.php)
$this->Album = $this->getServiceLocator()
->get('Album\Model\AlbumTable');
}
}
public function getCategoryTable(){
if(!$this->CategoryTable){
$this->CategoryTable = $this->getServiceLocator()
->get('Album\Model\CategoryTable');
}
}
public function addAction(){
//get the categories from the database
$categories = $this->getCategoryTable()->fetchall();
//make an array from the resultSet
// you can do it as well in your controller
//or in your form class like I did in my yersterday's answer
$cat_options = array();
foreach ($categories as $category) {
$cat_options[$category->id]=$category->name;
}
//create your form with category list as parameter
$form = new AlbumForm($cat_options);
//here I recomend to create a specific class for you filters
// and not put it in your Album class as describe in the zf tutorial
// => so you can have several forms using the same Model
// and it is more easy to find on the form folder ;)
$filter = new AlbumFilter();
//inject InputFilter in your form
$form->setInputFilter($filter->getInputFilter());
//inject data in form
$form->setData($request->getPost());
//check the form validity (data vs filters)
if($form->isValid()){
//create a new album
$album = new Album();
//hydrate your Album
$album->exchangeArray($form->getData());
//ask the albumsTable to save album in the database
$this->albumsTable->saveAlbum($album);
//here you can redirect to the Album list
$this->redirect()->toRoute('Album');
}
//call the viewModel if first time you see the page
// or if validation errors of the form.
return array('form' =>$form);
}
}
然后你只需要让你的看法...我不会在这里把这个代码放进去;)
如果您仍然需要在应用程序中创建多个模块,则可以使用工厂在getobjectTable()方法中调用控制器中的任何objectTable:如Carlos所说,getServiceConfig()数组全部合并为一个,所以你可以从应用程序的任何地方得到任何表格:)
我建议我的博文[Zend \ Form \ Element \ Select and Database Values](http://samminds.com/2013/03/zendformelementselect-and-database-值/) – Sam