Reputation: 25
I'm trying to learn ZF2, and I am not able to edit the data in the edit form. I have two classes Pessoa and Endereco and I have a form to edit the data, but it only changes the data related to Pessoa, those about the Endereco is stored as blank.
And when trying to add a new record (add action) i get the following error:
Fatal error: Cannot use object of type Pessoa\Model\Pessoa as array in /opt/lampp/htdocs/cad/module/Pessoa/src/Pessoa/Model/Pessoa.php on line 23
Pessoa.php
class Pessoa
{
public $id;
public $nome;
public $dtNasc;
public $endereco;
protected $inputFilter;
public function __construct()
{
$this->endereco = new Endereco();
}
public function exchangeArray($data)
{
$this->id = (!empty($data['id'])) ? $data['id'] : null;
$this->nome = (!empty($data['nome'])) ? $data['nome'] : null;
$this->dtNasc = (!empty($data['dtNasc'])) ? $data['dtNasc'] : null;
//$this->getEndercoInstance();
$this->endereco->exchangeArray($data);
/*if (isset($data["endereco"]))
{
if (!is_object($this->endereco)) $this->endereco = new Endereco();
$this->endereco->exchangeArray($data["endereco"]);
}*/
}
public function getArrayCopy()
{
$data = get_object_vars($this);
if (is_object($this->endereco)) {
$data["endereco"] = $this->endereco->getArrayCopy();
}
return $data;
}
public function setEndereco($end)
{
$this->endereco = $end;
}
private function getEndercoInstance()
{
$di = new Zend\Di;
$config = new Zend\Di\Configuration(array(
'instance' => array(
'Pessoa' => array(
// letting Zend\Di find out there's a $bar to inject where possible
'parameters' => array('endereco' => 'Endereco\Model\Endereco'),
)
)
));
$config->configure($di);
$pessoa = $di->get('Pessoa');
$this->endereco = $pessoa->endereco;
}
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' => true,
'filters' => array(
array('name' => 'Int'),
),
));
$inputFilter->add(array(
'name' => 'nome',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
Endereco.php
class Endereco
{
public $rua;
public $bairro;
public function exchangeArray($data)
{
$this->rua = (!empty($data["rua"])) ? $data["rua"] : null;
$this->bairro = (!empty($data["bairro"])) ? $data["bairro"] : null;
}
public function getArrayCopy()
{
return get_object_vars($this);
}
}
PessoaController.php add e edit actions
public function addAction()
{
$form = new PessoaForm();
$form->get('submit')->setValue('Add');
$request = $this->getRequest();
if ($request->isPost()) {
$pessoa = new Pessoa();
//$form->setInputFilter($pessoa->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$pessoa->exchangeArray($form->getData());
$this->getPessoaTable()->savePessoa($pessoa);
// Redirect to list of pessoa
return $this->redirect()->toRoute('pessoa');
}
}
return array('form' => $form);
}
public function editAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('pessoa', array(
'action' => 'add'
));
}
try {
$pessoa = $this->getPessoaTable()->getPessoa($id);
}
catch (\Exception $ex) {
return $this->redirect()->toRoute('pessoa', array(
'action' => 'index'
));
}
$form = new PessoaForm();
$form->bind($pessoa);
$form->get('submit')->setAttribute('value', 'Edit');
$request = $this->getRequest();
if ($request->isPost()) {
//$form->setInputFilter($pessoa->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$this->getPessoaTable()->savePessoa($pessoa);
// Redirect to list of pessoa
return $this->redirect()->toRoute('pessoa');
}else{
echo "not valid";
}
}
return array(
'id' => $id,
'form' => $form,
);
}
EnderecoFieldSet.php
class EnderecoFieldSet extends Fieldset
{
public function __construct($name = null)
{
parent::__construct('endereco');
$this->setHydrator(new ArraySerializableHydrator())
->setObject(new Endereco());
$this->add(array(
'name' => 'rua',
'option' => array(
'label' => 'Rua: ',
),
));
$this->add(array(
'name' => 'bairro',
'option' => array(
'label' => 'Bairro: ',
),
));
}
}
PessoaFieldSet.php
class PessoaFieldSet extends Fieldset
{
//public $endereco;
public function __construct()
{
//$this->endereco = new Endereco()
$this->init();
}
public function init()
{
parent::__construct('pessoa');
$this->setHydrator(new ArraySerializableHydrator())
->setObject(new Pessoa());
$this->add(array(
'name' => 'id',
'type' => 'Hidden',
));
$this->add(array(
'name' => 'nome',
'type' => 'Text',
'options' => array(
'label' => 'Nome:',
),
'attributes' => array(
'required' => 'required',
),
));
$this->add(array(
'name' => 'dtNasc',
'type' => 'Text',
'options' => array(
'label' => 'Data Nascimento:',
),
));
$this->add(array(
'type' => 'Pessoa\Form\EnderecoFieldSet',
'name' => 'endereco',
'options' => array(
'label' => 'endereco',
),
));
}
PessoaForm.php
class PessoaForm extends Form
{
public function __construct()
{
$this->init();
}
public function init()
{
// we want to ignore the name passed
parent::__construct('pessoa_form');
$this->setHydrator(new ArraySerializableHydrator());
//->setInputFilter(new InputFilter());
$this->add(array(
'type' => 'Pessoa\Form\PessoaFieldSet',
'options' => array(
'use_as_base_fieldset' => true
)
));
$this->add(array(
'type' => 'Zend\Form\Element\Csrf',
'name' => 'csrf'
));
$this->add(array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'value' => 'Go',
'id' => 'submitbutton',
),
));
}
}
And trying to add a new record (add action) i get the following error:
Fatal error: Cannot use object of type Pessoa\Model\Pessoa as array in /opt/lampp/htdocs/cad/module/Pessoa/src/Pessoa/Model/Pessoa.php on line 23
Please, can anyone help me?
Upvotes: 0
Views: 989
Reputation: 754
So let me explain better my self,
You are sending an array to the method
exchangeArray($data);
but $data is not an array;
Example of Array:
$data['username'] = 'someuser';
$data['email'] = '[email protected]';
$data['phone'] = '235346346';
print_r($data);die;
//output:
//Array ( [username] => someuser [email] => [email protected] [phone] => 235346346 )
Example Object:
$data['username'] = 'someuser';
$data['email'] = '[email protected]';
$data['phone'] = '235346346';
$object = (object)$data;
print_r($object);die;
//Output
//stdClass Object ( [username] => someuser [email] => [email protected] [phone] => 235346346 )
So i reproduce part of the code you have above and i gave to the method exchangeArray($data); of type Array and i got no errors then i gave to the method exchangeArray($object); and the result was Fatal error: Cannot use object of type stdClass as array in /blah/blah/... on line 39
So Basically in your method you are passing $data of type object and not Array
Upvotes: 1