In my global.php file:
return [
'db' => [
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=admin;host=localhost;charset=utf8',
],
'main' => [ // this is the database I wanna to use for some RecordExist validator!!!
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=main;host=localhost;charset=utf8',
],
'msdb' => [
'platform' => 'SqlServer',
'dirver' => 'Pdo',
'dsn' => 'dblib:host=sds2something.com;dbname=somedbname_db;',
'charset' => 'UTF-8',
'pdotype' => 'dblib',
],
]
I used to config the validator as an array in the entity model as follow:
class SomeEntity implements InputFilterAwareInterface
{
//some columns here as attributes
...
private $inputFilter;
public function exchangeArray(array $data){...}
public function getArrayCopy(){...}
public function setInputFilter(InputFilterInterface $inputFilter){...}
public function getInputFilter()
{
...
$inputFilter->add([
'name' => 'customerid',
'required' => true,
'filters' => [
['name' => ToInt::class],
],
'validators' => [
[
'name' => RecordExists::class,
'options' => [
'table' => 'main_customers',
'field' => 'id',
'adapter' => $dbAdapter, //TODO: how to pass this $var !!!!!???????
'message' => 'this customer id is not exist',
],
],
],
]);
...
}
}
In the controller, it is normal to new the entity like following:
class SomeController extends AbstractActionController
{
...
public function addsomethingAction()
{
...
$role = new Role(); //should I pass the $dbadapter here? or there is a more stander way?
$form->setValidationGroup('customerid','name', 'description');
$form->setInputFilter($role->getInputFilter());
$form->setData($request->getPost());
if (!$form->isValid()) return ['form' => $form];
$role->exchangeArray($form->getData());
$this->roleTable->saveRole($role, $this->adminTable);
...
}
}
Some other way?
If it is the best way to pass the $dbadapter via the __construction() function, it means that I need to write a __construction in the entityModel, it is abnormal to do so I think.