Question

I have 2 similar pairs of tables (Table1 and Table2, TableA and TableB).

Table1 has many Table2. Table2 belongs to Table1. In other words, there is a one-to-many relationship between Table1 and Table2. TableA and TableB has the same one-to-many relationship as Table1 and Table2.

I would like to move a row in Table1 plus all the associated rows in Table2 to the matching table pair TableA and TableB.

What is the best way to transfer the table row and the associated rows? Do I save each row to the new table one by one using save() or is there a way to save all the rows at one go using saveall()?

Is it a problem to use saveall() to do the data transfer if the table fields are similar but not exactly the same. Between the table pair, some rows are the same and some rows are different.

I am using Cakephp 2.4.5. Thank you for your help.

Was it helpful?

Solution

You'll need to use saveAssociated to solve this, but if your tables are not exactly the same then you will first need to transform your data into something manageable. Here is an example:

$table_1 = $this->Table_1->find('first', array('conditions => array('Table_1.whatever' => 'something')));

// here you can manually build the data you need to save.
$transfer_data['Table_A'] = $table_1['whatever you needed to change'];

You might need a for loop or something to convert your data to the slightly different version that you need for table a and b, but without knowing what the difference is there I cannot write the code that does the transformation. Once the transformation is complete you can just save your save as explained in the manual using saveAssociated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top