我正在导入CSV文件,并将其添加到我的数据库表中。在下一页中,我想向刚刚插入表中的数据添加信息。此数据将插入import_map
表。
当选择Table1Model选项时,它将根据map_table
使用Ajax在Table2Model选择框中选择选项。我无法在id
中创建map_table
字段并将其插入import_map
表,因为用户可以在Table2Model选择框中选择与{无关}的选项{1}}。
我的想法是在table1
方法中构建一个包含选定值的数组。但问题是它似乎没有达到add()
方法的if($this->request->is('ajax'))
部分。至少,当选择一个选项时,它根本没有做任何事情。
map_table
add()
表1
+-------------+-------------+
| `table1_id` | `table2_id` |
+-------------+-------------+
| 1 | 1 |
+-------------+-------------+
| 1 | 2 |
+-------------+-------------+
| 2 | 1 |
+-------------+-------------+
表2
+------------------+
| `id` | `name` |
+------+-----------+
| 1 | Table_1_1 |
+------+-----------+
| 2 | Table_1_2 |
+------+-----------+
进口
+------------------+--------------+
| `id` | `name` | `url` |
+------+-----------+--------------+
| 1 | Table_2_1 | example.com |
+------+-----------+--------------+
| 2 | Table_2_2 | example2.com |
+------+-----------+--------------+
import_map
+------------------+---------+--------+------------+
| `id` | `country` | `month` | `year` | `imported` |
+------+-----------+---------+--------+------------+
| 1 | USA | 05 | 2016 | 0 |
+------+-----------+---------+--------+------------+
| 2 | CAN | 08 | 2015 | 0 |
+------+-----------+---------+--------+------------+
导入控制器
+-------------+---------+
| `id` | int(11) |
| `table1_id` | int(5) |
| `table2_id` | int(5) |
| `import_id` | int(7) |
+-------------+---------+
导入模型
<?php
App::uses('AdminController', 'Controller');
/**
* Class ImportController
*/
class ImportController extends AdminController
{
public $components = array('RequestHandler');
public function add()
{
$importedIds = $this->Session->read('importedIds');
if(!isset($importedIds))
{
$this->redirect(array('action' => 'index'));
}
if($this->request->is('post'))
{
# Save logic here
}
$this->loadModel('Table1Model');
$table1 = $this->Table1Model->find('list', array(
'fields' => array(
'id',
'name'
),
'order' => 'name ASC'
));
$this->set('table1', $table1);
$this->loadModel('Table2Model');
$table2 = $this->Table2Model->find('list', array(
'fields' => array(
'id',
'name'
),
'order' => 'name ASC'
));
$selectedTable2Ids = array();
if($this->request->is('ajax'))
{
# Build what options to select.
foreach($this->request['data']['Table1Model']['table1_ids'] as $table1)
{
$selectedTable2Ids = '';
}
}
$this->set('table2', $table2);
$this->set('selectedTable2Ids', $selectedTable2Ids);
$this->render('add');
}
}
ImportMap模型
<?php
App::uses('AppModel', 'Model');
/**
* Class Import
*/
class Import extends AppModel
{
public $useTable = 'import';
public $hasMany = array(
'ImportMap' => array(
'class' => 'ImportMap',
'foreign_key' => 'import_id'
)
);
}
Table1Model
<?php
App::uses('AppModel', 'Model');
/**
* Class ImportMap
*/
class ImportMap extends AppModel
{
public $belongsTo = array(
'Import' => array(
'class' => 'Import',
'foreign_key' => 'id'
),
'Table1',
'Table1'
);
}
Table2Model
<?php
App::uses('AppModel', 'Model');
/**
* Class Table1
*/
class Table1 extends AppModel
{
public $useTable = 'table1';
public $hasMany = array(
'MapTable',
'ImportMap'
);
}
add.ctp
<?php
App::uses('AppModel', 'Model');
/**
* Class Table2
*/
class Table2 extends AppModel
{
public $useTable = 'table2';
public $hasMany = array(
'MapTable',
'ImportMap'
);