My entity model which strictly follow tutorial Forms and Actions - tutorials - Laminas Docs, I create a setAdapter() method to solve this problem, I don’t think it is a good way, any other better solution?
namespace Application\Model;
use Application\Model\AbstractBase\EntityBase;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Filter\StringTrim;
use Laminas\Filter\StripTags;
use Laminas\Filter\ToInt;
use Laminas\InputFilter\InputFilter;
use Laminas\InputFilter\InputFilterAwareInterface;
use Laminas\InputFilter\InputFilterAwareTrait;
use Laminas\Validator\Db\RecordExists;
use Laminas\Validator\NotEmpty;
use Laminas\Validator\StringLength;
class ThreadMain extends EntityBase implements InputFilterAwareInterface
{
public $id;
public $uid;
public $forumid;
public $title;
public $content;
public $haspic;
public $ip;
public $valid;
public $machine;
public $validator;
public $addtime;
public $uptime;
public $valtime;
private $dbAdapter;
use InputFilterAwareTrait;
public function setAdapter(AdapterInterface $dbAdapter)
{
$this->dbAdapter = $dbAdapter;
}
public function exchangeArray(array $data)
{
$this->id = !empty($data['id']) ? $data['id'] : null;
$this->uid = !empty($data['uid']) ? $data['uid'] : null;
$this->forumid = !empty($data['forumid']) ? $data['forumid'] : null;
$this->title = !empty($data['title']) ? $data['title'] : null;
$this->content = !empty($data['content']) ? $data['content'] : null;
$this->haspic = !empty($data['haspic']) ? $data['haspic'] : null;
$this->ip = !empty($data['ip']) ? $data['ip'] : null;
$this->valid = !empty($data['valid']) ? $data['valid'] : null;
$this->machine = !empty($data['machine']) ? $data['machine'] : null;
$this->validator = !empty($data['validator']) ? $data['validator'] : null;
$this->addtime = !empty($data['addtime']) ? $data['addtime'] : null;
$this->uptime = !empty($data['uptime']) ? $data['uptime'] : null;
$this->valtime = !empty($data['valtime']) ? $data['valtime'] : null;
}
public function getArrayCopy()
{
return [
'id' => $this->id,
'uid' => $this->uid,
'forumid' => $this->forumid,
'title' => $this->title,
'content' => $this->content,
'haspic' => $this->haspic,
'ip' => $this->ip,
'valid' => $this->valid,
'machine' => $this->machine,
'validator' => $this->validator,
'addtime' => $this->addtime,
'uptime' => $this->uptime,
'valtime' => $this->valtime,
];
}
public function getInputFilter()
{
if ($this->inputFilter) return $this->inputFilter;
$inputFilter = new InputFilter();
$inputFilter->add([
'name' => 'id',
'required' => true,
'filters' => [
['name' => ToInt::class],
],
]);
$inputFilter->add([
'name' => 'uid',
'required' => true,
'filters' => [
['name' => ToInt::class],
],
'validators' => [
[
'name' => NotEmpty::class,
'options' => ['message' => 'uid不能为空'],
],
[
'name' => RecordExists::class,
'options' => [
'table' => 'forum_user',
'field' => 'uid',
'adapter' => $this->dbAdapter,
'message' => 'uid不存在',
],
],
],
]);
$inputFilter->add([
'name' => 'forumid',
'required' => true,
'filters' => [
['name' => ToInt::class],
],
'validators' => [
[
'name' => NotEmpty::class,
'options' => ['message' => 'forumid不能为空'],
],
[
'name' => RecordExists::class,
'options' => [
'table' => 'forum_main',
'field' => 'id',
'adapter' => $this->dbAdapter,
'message' => 'forumid不存在',
],
],
],
]);
$inputFilter->add([
'name' => 'title',
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100, //先写100试下
'message' => '长度1-100',
],
],
],
]);
$inputFilter->add([
'name' => 'content',
'required' => true,
'filters' => [
['name' => StringTrim::class],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 1000, //先写1000试下
'message' => '长度1-1000',
],
],
],
]);
$this->inputFilter = $inputFilter;
return $this->inputFilter;
}
I just found a topic I raised before About zf3 validator of RecordExists - #11 by froschdesign
It seems I didn’t solved it yet.