Save to Multiple Tables Zend Framework 2

Asked

Viewed 127 times

0

I am new to PHP and Zend Framework 2, I am passing on a project written in C# ASP.NET to PHP using Zend and I came across the following problem.

I have 2 tables: user and street.

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

In the street table I have: id, id_usuario, address, number.

How do I insert/update both tables correctly?

How should the User and Street model be created? Together, separate? The mapping of the table (Usuariotable, Logradourotable) together, separate?

What would the Forms issue look like? Folder organization?

  • Whether two entities will be used or only one depends on the business rule of your Felipe application. Are two tables really necessary? Can a user have more than one address? If yes you need two models, otherwise you do not need to normalize and the user can contain all attributes (address and number)

  • @gmsantos, it would be more for organizing the tables, and I will also use a third table to search data on the street of several users, so I think it would be easier to pull this way the select. Because in fact, the scenario is that I have a table of cities, states, street and the user himself. The table of cities has the code of the state, the street has the code of the city and the user has the code of the street. I just wanted to understand how this matter of inserting into multiple tables would work... Can you help me?

  • Felipe, your doubt is quite specific the way it is. If the doubt is to insert in more than one table just create two Inserts. Now helping you with the full implementation goes beyond the scope of the stack overflow. I recommend that you read the [tour] and also [Ask].

  • All right, thank you very much... but could you give me an example of how I would do that? If I need to, I can put the structure of my project, which is in each file...

1 answer

1

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

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 (so I see no logic in having a model and a form for user and another for patio).

In the user model you could make the persistence 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.