我正在尝试将验证器 RecordExists 添加到我的表单中,但出现错误“没有 db 适配器存在”。如何将 db 适配器设置为此验证器?我使用骨架应用程序中的示例,我正在尝试做这样的事情(是的,我知道 $dbAdapter 是未定义的 :) 我正在寻找如何将此变量更改为 db 适配器资源的解决方案):
namespace Album\Model;
use Zend\InputFilter\Factory as InputFactory; // <-- Add this import
use Zend\InputFilter\InputFilter; // <-- Add this import
use Zend\InputFilter\InputFilterAwareInterface; // <-- Add this import
use Zend\InputFilter\InputFilterInterface; // <-- Add this import
class Album implements InputFilterAwareInterface
{
public $id;
public $artist;
public $title;
protected $inputFilter; // <-- Add this variable
public function exchangeArray($data)
{
$this->id = (isset($data['id'])) ? $data['id'] : null;
$this->artist = (isset($data['artist'])) ? $data['artist'] : null;
$this->title = (isset($data['title'])) ? $data['title'] : null;
}
// Add content to this method:
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
'validators' => array(
array(
'name' => 'Db\RecordExists',
'options' => array(
'table' => 'album',
'field' => 'title',
'adapter' => $dbAdapter
),
),
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}