Cakephp - page using more than one table

Asked

Viewed 621 times

6

I have a question, about saving data in the database using Cakephp, when you need to manipulate with more than one table. I am creating a customer registration system and in it has a system to register login’s and customer passwords, such as control panel, database, etc.

The structure of the database

Customers

Have fields with customer data such as phone, email, etc

options

  • id
  • option (option name such as control panel, database, etc)
  • usuario_ativ (field to indicate if such option will have user fill)
  • host_ativ (field to indicate if you have the host/ip, as database... control panel would not have this option

rel_op_client

  • id_client
  • id_opcao
  • password
  • host
  • user

I managed to resolve the issue of the form appear data, in the client controller I put public $uses = array('Cliente','Opcao', 'RelInfoCliente'); and with it it can capture the data of the 3 tables... but in case of saving the form data, what would be the best way that when saving the client save the data of the options, if I change the username change in the table rel_op_cliente

  • what is the relationship between these tables?

  • clients would be like the parent table, the rel_op_client relates to the customers. Table clients have the id field and rel_op_clients have the id_client, the two link this way. The options table connects with rel_op_client via the id_option. I do not know if I explained clear, but summarizing, rel_op_clients is where it is stored the data and options would be the name of this data, as control panel, database and would also have the field to enable or disable some fields (in the view)as control panel that it is not necessary to appear the ip, it is only user and password

  • 1

    I didn’t get it right. But it seems to me that you didn’t establish a relationship (hasMany, hasOne, etc...) between them, did you? If not, you should rethink your tables and models and link between them correctly. http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

  • I get it, I’m going to do some research on that. The scheme would be simple, rel_op_client connects with client, options connects with rel_op_client... but I’ll analyze it better, which seems to be the best solution to the link problem, and would be the correct way I did

  • 1

    I think you have a relção hasManyor hasMany trought take a look and establish this relationship using the correct cakephp convection. It will become much easier to work with

  • Thanks man, I had already done with hasMany and was much better than the solution I made of array( 'joins'... etc.... In case, would have a father, son and grandson, the association, I will take a look at this issue of relation hasMany trought.... For display seems perfect, then I’ll see if to save follows the same idea

  • Well, I’m still breaking a little the head, the client linking with the table rel_op_client is perfect, now this rel_op_client would have to be connected with the options... I used the hasmany in client and in rel_op_client put, but when I give a vardump does not appear the information from the options table, as if I was not making the connection, the options would be like a grandson class. Client calls with rel_op_client who calls with options. That part I am not able to solve

  • In order for cakephp to work properly you have to use your convections. Your table rel_op_cliente does not follow the table name convection of cakephp. Explain a little more about what you want to do and the function of these tables in your question that I help you relate them to.

  • Well, putting the structure | Customers id = 3 name = Braulio ;rel_op_clients id_client = 3 id_option = 2 password = 123 user = braulio_holtz host = 192.168.0.1 ;options id = 2 option = Mysql database user_ativ = 1 host_ativ = 1 | It would be more or less this scheme that would be, I think you can understand a little, in clients is, in rel_op_clients is the relation clients and options, in options is the names of the fields that enter the user and password, type, control panel, database, etc

  • Dude, so that’s it. You have a relationship where Usuarios HABTM Opcoes. You should look at the option HABTMon the link I gave you and name the table according to what is there!!

  • But what about the case of rel_op_clients table? Options would be like show the name of the field, for example, there would have the id and the name, where the name would be for example, Control Panel, database, etc, basically this table would just show which is the chosen field, avoiding having to each customer registration put the same things (as control panel that all hosts have)... It is not a table of users to log into the system, it is a table with login client data records and passwords for multiple services (Control panels, database, etc)

  • Exactly. The users table can have the information you want and Voce must create a third table opcoes_usuarios where it will contain user_id and option_id so you can list several users with various options. Read about the HABTM

Show 7 more comments

1 answer

1

Settled the question

public  $hasAndBelongsToMany  =  array (
    'opcoes_clientes'  =>  array (
        'className'  =>  'opcoes' ,
        'joinTable' => 'rel_infos_clientes',
        'foreignKey'  =>  'id_cliente' ,
        'associationForeignKey'  => 'id_opcao',
        'dependent'  =>  true
    )
);

In case I’m just changing the table name to stay inside the convention, but now I get it. joinTable would be the intermediate table and in classname would be the class with the table "neto"...

  • Glad you made it, man! =)

  • Thanks for the help, I moved to the convention, it got better still, so I didn’t have to put joinTable, Foreignkey, Associationforeignkey

Browser other questions tagged

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