Skip to content

用cursor 做一个完整的增删改查

做一个完整的增删改查,如果有则修改,没有则新增

  1. 创建一个名zt_testfab,字段有short_name,full_name,company 默认为1,openby,opendate的表
  2. 然后创建两个页面,一个testfablist,一个testfabForm 页面结构大至长这样
php
include '../../common/view/header.html.php';
?>
<?php include '../../pgchecklist/view/pgheader.html.php'; ?>
<?php include '../../common/view/datepicker.html.php'; ?>
<?php include '../../common/view/kindeditor.html.php'; ?>
<?php include "../../common/view/chosen.html.php"; ?>

<style>
    form {
        width: 80% !important;
    }
    .flex-table-container {
        display:         flex;
        justify-content: center; /* 水平居中 */
        padding-top:     10px;
    }

    .flex-table {
        display:        flex;
        flex-direction: column;
        width:          100%; /* 新增: 设置宽度为80% */
    }

    .flex-row {
        display: flex;
        width:   100%;
    }

    .flex-cell {
        flex:         1;
        padding:      4px;
        box-sizing:   border-box;
        border:       1px solid #000;
        border-width: 0 1px 1px 0; /* 只设置左右下边框 */
        line-height:  16px; /* 新增行高设置 */
    }

    /* 设置第一列和最后一列的边框 */
    .flex-row > :first-child {
        border-left-width: 1px;
    }

    .flex-row > :last-child {
        border-right-width: 1px;
    }

    /* 设置第一行和最后一行的边框 */
    .flex-row:first-child > .flex-cell {
        border-top-width: 1px;
    }

    .flex-row:last-child > .flex-cell {
        border-bottom-width: 1px;
    }

    /* 避免角落处边框叠加 */
    .flex-row:first-child > .flex-cell:first-child {
        border-top-left-radius: 4px;
    }

    .flex-row:first-child > .flex-cell:last-child {
        border-top-right-radius: 4px;
    }

    .flex-row:last-child > .flex-cell:first-child {
        border-bottom-left-radius: 4px;
    }

    .flex-row:last-child > .flex-cell:last-child {
        border-bottom-right-radius: 4px;
    }

    .header-row {
        /* 可选:为表头行添加特定样式,如更明显的下边框 */
        background-color: #edf3fe; /* 示例背景颜色 */
        font-weight:      bold; /* 字体加粗 */
    }

    .forminput {
        width: 99.9%;
    }

    .flex-item {
        flex: 0 0 auto; /* 关闭增长和收缩,使用指定的宽度 */
    }

    .flex-item1 {
        flex-basis: 10%;
    }

    .flex-item2 {
        flex-basis: 20%;
    }

    .flex-item3 {
        flex-basis: 30%;
    }

    .flex-item4 {
        flex-basis: 40%;
    }

    .flex-item5 {
        flex-basis: 50%;
    }

    .flex-item6 {
        flex-basis: 60%;
    }

    .flex-item7 {
        flex-basis: 70%;
    }

    .flex-item8 {
        flex-basis: 80%;
    }

    .flex-item9 {
        flex-basis: 90%;
    }

    .flex-item10 {
        flex-basis: 100%;
    }

    input[type="radio"] {
        margin-left: 15px;
    }

    .btn-submit {
        text-align: center;
    }

    .section-block {
        font-weight: bolder;
    }

    .text-red {
        color: red;
    }

    .my-select {
        width: 70%;
    }

    .flex-between {
        display:         flex;
        justify-content: space-between;
    }

    .flex-column {
        display:        flex;
        flex-direction: column; /* 垂直排列子元素 */
    }
    .flex-part1 {
        flex: 1;
    }
    .flex-part2 {
        flex: 2;
    }
    .flex-part3 {
        flex: 3;
    }
    .flex-part4 {
        flex: 4;
    }
    .flex-part5 {
        flex: 5;
    }
    .flex-part6 {
        flex: 6;
    }
    .flex-part7 {
        flex: 7;
    }
    .flex-part8 {
        flex: 8;
    }
    .flex-part9 {
        flex: 9;
    }
    .flex-wrap {
        display:   flex;
        flex-wrap: wrap;
    }

    .flex-line {
        display:        flex;
        flex-direction: row;
    }
    .left-side {
        justify-content: flex-start; /* 左侧子元素靠左对齐 */
        padding-left:    10px;
    }
    .right-side {
        padding-right:   10px;
        justify-content: flex-end; /* 右侧子元素靠右对齐 */
    }
    .add-btn, .del-btn {
        display:     inline-block;
        width:       24px;
        font-size:   36px;
        color:       blue;
        width:       100%;
        height:      100%;
        line-height: 36px;
        text-align:  center;
    }
    .del-btn {
        color: red;
    }
    .text-bold {
        font-weight: bolder;
    }
    .text-center {
        text-align: center;
    }
    select {
        overflow: hidden;
    }
</style>
<form method='post'
      target='hiddenwin'
      id="layout-form"
      enctype="multipart/form-data">

    <div class="flex-table-container">
        <div class="flex-table">
            <div class="flex-row">
                <div class='flex-cell'>xxx</div>
                <div class='flex-cell'>
                    <?php echo common::printPrivRadioType("main[xxx]", $data->select->xxx, $data->main->xxx, "class='xxx chosen_one'  title='{$data->main->xxx}'  data-field='xxx '     ", 'select', $data->priv->admin || $data->priv->main) ?>
                </div>
            </div>
            <div class="flex-row">
                <div class='flex-cell'>xxx</div>
                <div class='flex-cell'>
                    <?php echo common::printPrivRadioType("main[xxx]", $data->select->xxx, $data->main->xxx, "class='xxx chosen_one'  title='{$data->main->xxx}'  data-field='xxx '     ", 'input', $data->priv->admin || $data->priv->main) ?>
                </div>
            </div>
          
    <div class="flex-table-container">
        <div class="flex-table">
            <div class="flex-row btn-submit">
                <div class='flex-cell'>
                    &nbsp;&nbsp;&nbsp;<?php echo html::submitButton() . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . html::resetButton(); ?>
                </div>
            </div>
        </div>
    </div>
</form>
<script></script>
<?php include '../../common/view/action.html.php'; ?>
<?php include '../../common/view/footer.html.php'; ?>
php
<?php
/**
 * The browse view file of product dept of silergyPMS.
 *
 * @copyright    矽力杰
 * @license      silergyPMS
 * @author       silergyPMS
 * @package      product
 * @version      $Id: browse.html.php 5096 2013-07-11 07:02:43Z chencongzhi520@gmail.com $
 * @link         http://www.silergy.com
 */
?>
<?php
include '../../common/view/header.html.php';

?>

<style>
    th {
        text-align: left !important;
        width: 7%;
    }
    .first-space{
        text-indent: 8px;
    }
    table{
        width: 100%;
    }
    .action-width{
        width: 200px;
    }
    #bysearchTab{
        list-style: none;
    }
    .icon-search{
        display: inline-block;
        width: 3px;
        height: 12px;
    }
</style>

<script language='Javascript'>
    var browseType = '<?php echo $browseType;?>';
</script>
<div id='featurebar'>
        <div class='f-right'>
            <?php
            echo "<span id='allTab' $all>"; common::printLink('test', 'testfabForm', "", "Socket|Kit Form"); echo  '</span>' ;
            echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
            echo "<span id='testfabsTab'>"; common::printLink('test', 'testfabsList', "", "Test Fabs List"); echo  '</span>' ;
            echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
            ?>
    </div>
    <div class='nav'>
        <?php
        ?>
    </div>

</div>

<div id='querybox' style="margin-top:0px;" class="<?php if($type=='bysearch'){echo "show";}?>"><?php echo $searchForm?></div>


<div class='main' >
    <table class='table table-condensed table-hover table-striped tablesorter table-fixed' id='userList'>
        <thead>
        <tr class='colhead'>
            <th>ID</th>
            <th>Part</th>
            <th>Process</th>
            <th>Vendor</th>
            <th>Package</th>
            <th>Bu</th>
            <th>Open by</th>
            <th>Open Date</th>
            <th><?php echo $lang->actions; ?></th>
        </tr>
        </thead>
        <?php foreach ($list as $v): ?>
        <tr <?php if($v->status == '4') echo "style='background:#999999;'"?>>
            <td><?php echo $v->id; ?></td>
            <td><?php echo $v->part; ?></td>
            <td><?php echo $v->process; ?></td>
            <td><?php echo $v->vendor; ?></td>
            <td><?php echo $v->package; ?></td>
            <td><?php echo $v->buuser; ?></td>
            <td title="<?php  echo $v->openby; ?>"><?php echo $v->openby; ?></td>
            <td><?php echo $v->opendate; ?></td>
            <td> <?php

                    $btns = array(
                        array(
                            'title' => 'Sign/Edit',
                            'href' => array(
                                "test",
                               'testfabForm',
                                "id={$v->id}",
                                "Edit",
                                '_self'
                            ),
                            'class' => 'btn btn-info btn-xs',
                            'style' => '',
                            'attr' => '',
                        ),
                        array(
                            'title' => 'Delete',
                            'href' => array(
                                "test",
                                "testfabFormDel",
                                "id={$v->id}",
                                "Delete",
                                'hiddenwin'
                            ),
                            'class' => 'btn btn-danger btn-xs delete',
                            'style' => '',
                            'attr' => '',
                            "status" => $v->status == '0' && $this->app->user->account === $v->openby
                        ),
                    )
                    ?>
                    <?php echo $this->fetch('btn','moreBtn',array('btn' => $btns)); ?></td>
        </tr>
        <?php endforeach; ?>
        <tfoot>
        <tr>
            <td colspan='13'>
                <?php echo $pager->show(); ?>
            </td>
        </tr>
        </tfoot>
    </table>
</div>
<script>
    $(function() {
        // $("#userList").colResizable();
        $('.delete').click(function() {
            if(confirm('Are you sure you want to delete this record?')){
                return true;
            }
            else{
                return false;
            }

        });
    })
</script>
<?php include '../../common/view/footer.html.php'; ?>
  1. control testfablist
php
    /**
     * 供应商列表
     *
     * @author shengland
     * @Date   2025/2/27---9:58
     */
    public function testfabList($param = 0, $type = 'all', $orderBy = 'id_desc', $recTotal = 0, $recPerPage = 20, $pageID = 1) {
        /* Set the pager. */
        $this->app->loadClass('pager', $static = true);
        $pager = pager::init($recTotal, $recPerPage, $pageID);

        if ($type != 'bysearch') {
            $where = '1 = 1';              // all
        }
        else {
            $queryID = (int)$param;
            if ($queryID) {
                $query = $this->search->getQuery($queryID);

                if ($query) {
                    $this->session->set('testtestfabListQuery', $query->sql);
                    $this->session->set('testtestfabListForm', $query->form);
                }
                else {
                    $this->session->set('testtestfabListQuery', "1 = 1");
                }
            }

            $where = $this->session->testtestfabListQuery;
        }


        $this->config->test->testfabList->search['actionURL'] = $this->createLink('test', 'testfabList', "param=myQueryID&type=bysearch");   // search
        $this->config->test->testfabList->search['queryID'] = $param;
        $this->view->searchForm = $this->fetch('search', 'buildForm', $this->config->test->testfabList->search);
        //apply process 列表页
        $list = $this->test->testfabList($where, $orderBy, $pager, $type);

        $this->view->title = "Socket List";
        $this->assign('pager', $pager);
        $this->assign('list', $list);
        $this->assign('type', $type);
        $this->assign('param', $param);
        $this->display();
    }
  1. model testfablist
php

    public function testfabList($search = '', $orderby = 'id_desc', $pager, $type) {
        if (!$search) {
            $search = '1 = 1';
        }

        $dataobj = $this->dao->select('*')->from('zt_testfab_main_checklist');
        // 添加搜索条件
        if ($search) {
            $dataobj->where($search);
        }

// 如果是 admin 或 caihua_sun,则查询所有记录
        if ($this->app->user->account === 'admin') {
            // 不需要添加额外的条件
        }
        else {
            // 查询当前用户或其下属的记录
            $dataobj->andWhere('( openby')->eq($this->app->user->account)->markRight(true);

        }

// 排序和分页
        $data = $dataobj->orderBy($orderby)
            ->page($pager)
            ->fetchAll();

        return $data;
    }
  1. control testfabForm
php
    /**
     * 新项目IOS时AE/PM在PM 在PM系统发起新客户选型
     *
     * @author shengland
     * @Date   2025/2/26---10:07
     */
    public function testfabForm($id = '') {
        // 定义一个PHP数组,使用key-value形式表示不同阶段
        $data = $this->test->getSocketFormDetail($id);
        if ($_POST) {
            $form_data = fixer::input('post')->callFunc($_POST, 'trim')->get();
            $changes = $this->test->testfabForm($id, $form_data, $data);
            if ($changes) {
                if (!$id) {
                    $id = $changes['openby'];
                }
                $this->loadModel('action')->recordLogs($id, 'testfabForm', $changes);
                echo js::alert('success');
            }


            die(js::locate(helper::createLink('test', 'testfabForm', "id=$id"), 'parent.parent'));
        }
        // 将所有检查项传递到视图
        $this->assign('actions', $this->action->getList('testfabForm', $id));
        $this->assign('data', $data);
        $this->display('test','testfabForm');
    }
  1. model getSocketFormDetail
php
    public function getSocketFormDetail($id = '') {
        $data = null;
        $data->radio->type = $this->config->test->sockForm->radio->type;
        $data->radio->bustatus = [ 'approved' => "approve", 'rejected' => "reject"];

        $data->select->vendor = ['' => ''] + $this->config->test->sockForm->select->vendor;

        if ($id) {
            $data->main = $this->dao->select('*')->from('zt_testfab_main_checklist')
                ->where('id')->eq($id)
                ->fetch();
        }

        if ($this->app->user->account == 'admin') {
            $data->priv->admin = true;
            $data->priv->line = true;
        }
        
        $data->priv->main = true;
        return $data;
    }
  1. model testfabForm
php
    /**
     * 更新 supplierDesignForm 方法
     *
     * @author shengland
     * @Date   2025/2/27---14:16
     */
    public function testfabForm($id, $form_data, $old_data) {
        //上传pcb 文件
//        if ($_FILES['file1']['error'] === 0 && $id) {
//            $file_upload_id = $this->loadModel('file')->setOtherSavePath('test/upload/')->saveUploadOne('file1', 'my', $id);
//            $form_data->main['file1id'] = key($file_upload_id);
//            $form_data->main['file1name'] = current($file_upload_id);
//        }
        //end

        if ($_FILES['file1']['error'] === 0 && $id) {
            $file_upload_id = $this->loadModel('file')->setOtherSavePath('test/upload/')->saveUploadOne('file1', 'testfabForm', $id);
            $form_data->main['file1id'] = key($file_upload_id);
            $form_data->main['file1name'] = current($file_upload_id);
        }
        //end

        if ($_FILES['file2']['error'] === 0 && $id) {
            $file_upload_id = $this->loadModel('file')->setOtherSavePath('test/upload/')->saveUploadOne('file2', 'testfabForm', $id);
            $form_data->main['file2id'] = key($file_upload_id);
            $form_data->main['file2name'] = current($file_upload_id);
            //判断file2name是否 有重复
            $is_purchase_number = $this->dao->select('file2name')->from('zt_testfab_main_checklist')->where('id')->ne($id)->andWhere('file2name')->eq($form_data->main['file2name'])->fetch();
            if ($is_purchase_number) {
                die(js::error('请购单编号在其他单子已经使用过了'));
            }

            $form_data->main['status'] = 1;
        }

        if ($form_data->line['serial_number']) {
            $form_data->main['status'] = 2;
        }

        if (isset($form_data->main['bustatus']) && $form_data->main['bustatus'] == 'approved') {
            $form_data->main['status'] = 3;
        }
        //end
//        $this->dao->beginTransaction();

        $extra_form = [];
        foreach ($form_data as $k => $v) {
            if ($k == 'main') {
//                if ($this->app->user->account == $old_data->main->applicant) {
//                    if (isset($v['project'])) {
//                        $extra_form['status'] = 0;
//                    }
//                }
                if ($id) {
//                    if ($old_data->priv->admin) {
//                        if (isset($_POST['main']['status'])) {
//                            $v['status'] = $_POST['main']['status'];
//                        }
//                    }

                    $changes['main'] = common::createChanges($old_data->main, $v);
                    if ($changes['main']) {
                        $this->dao->update('zt_testfab_main_checklist')->data($v)
                            ->where('id')->eq($id)
                            ->exec();
                    }
                }
                else {
                    $v['openby'] = $this->app->user->account;
                    $v['opendate'] = helper::now();
                    $v['buuser'] = $old_data->main->buuser;
                    $this->dao->insert('zt_testfab_main_checklist')
                        ->data($v)
                        ->autoCheck()
                        ->batchcheck($this->config->test->sockForm->requiredFields, 'notempty')
//                            ->check('barcode', 'unique')
                        ->exec();


                    if (dao::isError()) die(js::error(dao::getError()));
                    $id = $this->dao->lastInsertID();
                    $changes['openby'] = $id;
                    $opened = true;

                }


            }
            else if ($k == 'line') {
                $changes['line'] = common::createChanges($old_data->line, $v);
                if ($changes['line'] && $old_data->line->id) {
                    $this->dao->update('zt_testfab_line_checklist')->data($v)
                        ->autoCheck()
                        ->where('pid')->eq($id)
                        ->exec();
                }
                else {
                    $v['pid'] = $id;
                    $v['openby'] = $this->app->user->account;
                    $v['opendate'] = helper::today();
                    $this->dao->insert('zt_testfab_line_checklist')
                        ->data($v)
                        ->autoCheck()
//                            ->check('barcode', 'unique')
                        ->exec();
                }
            }

        }

//        $this->dao->commit();

        return $changes;
    }
  1. control
php
    /**
     *
     * testfab form del
     * @author shengland
     * @Date   2025/5/8---14:14
     */
    public function testfabFormDel($id) {
        $res = $this->dao->select('*')->from('zt_testfab_main_checklist')
            ->where('id')->eq($id)
            ->fetch();
        if (!$res) {
            die(js::error('This record does not exist'));
        }

        if ($res->status > 2) {
            die(js::error('暂不允许删除'));
        }

        $is_del = $this->dao->delete()->from('zt_testfab_main_checklist')->where('id')->eq($id)->exec();
        $is_del2 = $this->dao->delete()->from('zt_testfab_line_checklist')->where('pid')->eq($id)->exec();
        if ($is_del) {
            echo js::alert('Delete successful ');
            die(js::locate($this->createLink('test', 'testfabList'), 'parent.parent'));
        }
    }

修改testfabsform 不按常理出牌

text
 test 保存是跟控制器一样的名字  
然后上保存时按这样的逻辑结构修改  
  /**
     * 更新 supplierDesignForm 方法
     *
     * @author shengland
     * @Date   2025/2/27---14:16
     */
    public function testfabForm($id, $form_data, $old_data) {
        //上传pcb 文件
//        if ($_FILES['file1']['error'] === 0 && $id) {
//            $file_upload_id = $this->loadModel('file')->setOtherSavePath('test/upload/')->saveUploadOne('file1', 'my', $id);
//            $form_data->main['file1id'] = key($file_upload_id);
//            $form_data->main['file1name'] = current($file_upload_id);
//        }
        //end

        if ($_FILES['file1']['error'] === 0 && $id) {
            $file_upload_id = $this->loadModel('file')->setOtherSavePath('test/upload/')->saveUploadOne('file1', 'testfabForm', $id);
            $form_data->main['file1id'] = key($file_upload_id);
            $form_data->main['file1name'] = current($file_upload_id);
        }
        //end

        if ($_FILES['file2']['error'] === 0 && $id) {
            $file_upload_id = $this->loadModel('file')->setOtherSavePath('test/upload/')->saveUploadOne('file2', 'testfabForm', $id);
            $form_data->main['file2id'] = key($file_upload_id);
            $form_data->main['file2name'] = current($file_upload_id);
            //判断file2name是否 有重复
            $is_purchase_number = $this->dao->select('file2name')->from('zt_testfab_main_checklist')->where('id')->ne($id)->andWhere('file2name')->eq($form_data->main['file2name'])->fetch();
            if ($is_purchase_number) {
                die(js::error('请购单编号在其他单子已经使用过了'));
            }

            $form_data->main['status'] = 1;
        }

        if ($form_data->line['serial_number']) {
            $form_data->main['status'] = 2;
        }

        if (isset($form_data->main['bustatus']) && $form_data->main['bustatus'] == 'approved') {
            $form_data->main['status'] = 3;
        }
        //end
//        $this->dao->beginTransaction();

        $extra_form = [];
        foreach ($form_data as $k => $v) {
            if ($k == 'main') {
//                if ($this->app->user->account == $old_data->main->applicant) {
//                    if (isset($v['project'])) {
//                        $extra_form['status'] = 0;
//                    }
//                }
                if ($id) {
//                    if ($old_data->priv->admin) {
//                        if (isset($_POST['main']['status'])) {
//                            $v['status'] = $_POST['main']['status'];
//                        }
//                    }

                    $changes['main'] = common::createChanges($old_data->main, $v);
                    if ($changes['main']) {
                        $this->dao->update('zt_testfab_main_checklist')->data($v)
                            ->where('id')->eq($id)
                            ->exec();
                    }
                }
                else {
                    $v['openby'] = $this->app->user->account;
                    $v['opendate'] = helper::now();
                    $v['buuser'] = $old_data->main->buuser;
                    $this->dao->insert('zt_testfab_main_checklist')
                        ->data($v)
                        ->autoCheck()
                        ->batchcheck($this->config->test->sockForm->requiredFields, 'notempty')
//                            ->check('barcode', 'unique')
                        ->exec();


                    if (dao::isError()) die(js::error(dao::getError()));
                    $id = $this->dao->lastInsertID();
                    $changes['openby'] = $id;
                    $opened = true;

                }


            }
            else if ($k == 'line') {
                $changes['line'] = common::createChanges($old_data->line, $v);
                if ($changes['line'] && $old_data->line->id) {
                    $this->dao->update('zt_testfab_line_checklist')->data($v)
                        ->autoCheck()
                        ->where('pid')->eq($id)
                        ->exec();
                }
                else {
                    $v['pid'] = $id;
                    $v['openby'] = $this->app->user->account;
                    $v['opendate'] = helper::today();
                    $this->dao->insert('zt_testfab_line_checklist')
                        ->data($v)
                        ->autoCheck()
//                            ->check('barcode', 'unique')
                        ->exec();
                }
            }

        }

//        $this->dao->commit();

        return $changes;
    }