Save in Multiple Tables Zend Framework 2

0

I'm new to PHP and Zend Framework 2, I'm going through a project written in C # ASP.NET for PHP using Zend and I came across the following problem.

I have 2 tables: user and public place.

In the user table I have: id, name, email, password.

In the field table I have: id, userid, address, number.

How do I insert / give update in both tables correctly?

How should the User and Backdoor model be created? Together, separated? The table mapping (UserTable, LogRetourTable) together, separated?

What would the Forms look like? Folder organization?

    
asked by anonymous 19.10.2015 / 13:58

1 answer

1

You can make the model and user form receive both the user data and the data from the street.

And save the values in their respective tables, with this you will use only a form and a model to perform the persistence of the data of a user, that although they are different tables the values are only of a user (soon I do not see logical to have one model and one form for user and another one for public place).

In the user model you could persist as follows.

    #Array com dados do usuario
    $usuario = array(
        'id' => $user->id,
        'nome'  => $user->nome,
        'email' => $user->email,
        'senha' => $user->senha,
    ); 

$this->tableGateway->insert($usuario); #inseri os dados na tabela usuario

$ultimo_usuario = $this->tableGateway->lastInsertValue; #recupera o id do insert

#prepara o TableGateway da tabela de logradouro
$adapter=$this->tableGateway->getAdapter();
$logradouro = new TableGateway('logradouro', $adapter); 

#Array com os dados do logradouro
$dados_logradouro = array(
        'id' => $user->logradouro_id,
        'id_usuario'  => $ultimo_usuario,
        'endereco'  => $user->endereco,
        'numero'  => $user->numero,
        );

$logradouro->insert($dados_logradouro); #Inseri o logradouro do usuario
    
09.02.2016 / 01:04