0

I'm following this tutorial which explains a few approaches to this (I'm doing the first one he lists) and I'm running into some trouble.

My form has Zend\Form\Element\Select populate with values from a DB. Following the tutorial I have this is my controller:

...
//So form can pull select list options from DB
$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
var_dump($dbAdapter); //debug
$form = new UpdateForm($dbAdapter);
...

And this in my form model:

...
use Zend\Db\Adapter\AdapterInterface;

class UpdateTicketForm extends Form {
    protected $dbAdapter;

    protected function setDbAdapter(AdapterInterface $dbAdapter) {
        $this->dbAdapter = $dbAdapter;
        return $this;
    }

    protected function getDbAdapter() {
        return $this->dbAdapter;
    }

    public function __construct($name = null,AdapterInterface $dbAdapter) {
        $this->setDbAdapter($dbAdapter);
        parent::__construct('updateForm');
...

When I run it I can see $dbAdapter dumped to the screen appears to be valid, but I still get an error:

object(Zend\Db\Adapter\Adapter)#267 (5) { ["driver":protected]=> object(Zend\Db\Adapter\Driver\Pdo\Pdo)#268 (4) { [...

Catchable fatal error: Argument 2 passed to MyModule\Form\UpdateForm::__construct() must implement interface Zend\Db\Adapter\AdapterInterface, none given, called in ...Controller\MyController.php on line 73 and defined in ...\Form\UpdateForm.php on line 20

The person who wrote the tutorial also posted his source which I, after looking at, still could not determine the issue with my code (corresponding to the "form-db-adapter" approach in his code)

What could be the cause of this issue?

3
  • $name = null,AdapterInterface $dbAdapter NetBeans screams for this with wrong order of arguments Commented May 2, 2014 at 19:21
  • In addition to the issue mentioned in the two answers, you added the DB adapter code to the UpdateTicketForm, but your code is instantiating the UpdateForm. Commented May 2, 2014 at 19:41
  • Yeah that was just a typo, tried to generalize the names in my code before posting to make it clearer. Commented May 2, 2014 at 20:05

2 Answers 2

1

The construtoer of your UpdateForm expects two arguments $name and $adapter as shown below.

public function __construct($name = null, AdapterInterface $dbAdapter) {

}

So you should pass two arguments while creating the instance of UpdateForm() as shown below.

$form = new UpdateForm('formName', $dbAdapter);

And if you just want to pass $dbAdapter as the argument change the constructor of UpdateForm to this.

public function __construct(AdapterInterface $dbAdapter, $name = null) {

}
Sign up to request clarification or add additional context in comments.

1 Comment

When I do that I still get the error: __construct() must implement interface Zend\Db\Adapter\AdapterInterface, string given
0

your form is expecting the DBAdapter as second argument, first argument is form name. Try the follow:

$form = new UpdateForm('myform', $dbAdapter);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.