如何使用PHP Codeigniter向MongoDB角度插入数据输入

时间:2019-01-14 08:59:34

标签: angular mongodb codeigniter insert

我开始使用Angular Framework,MongoDB和Codeigniter。我想问一下Angular基本输入表单的名称是:电子邮件:地址:。我希望将三个归档数据插入到MongoDB中。数据不直接转到MongoDB,首先将数据传输到PHP Codeigniter,然后将名称输入字段,然后转到Database(MongoDB)。我已经在使用Nodejs。

请告诉我什么是实际的Angular数据传输代码或数据插入代码。


branch-list.component.ts

      name: {
        title: 'Branch-Name',
        width: '40%'
      },
       Address: {
        title: 'Address',
        width: '40%'
      },
    }
  };

  constructor(private router: Router, private generalApiService: GeneralApiService
    , private route: ActivatedRoute, private modalService: NgbModal) { }

  ngOnInit() {
    this.generalApiService.getAll(this.apiUrl).subscribe(res => {
      this.branch = res;
    });
  }
---------------------------------------------------------------
pages.module.ts
---------------------------------------------------------------
import { NgModule } from '@angular/core';

import { PagesComponent } from './pages.component';
import { DashboardModule } from './dashboard/dashboard.module';
import { PagesRoutingModule } from './pages-routing.module';
import { ThemeModule } from '../@theme/theme.module';
import { MiscellaneousModule } from './miscellaneous/miscellaneous.module';
import { ModalComponent } from './components/modal.component';

const PAGES_COMPONENTS = [
  PagesComponent,
  ModalComponent
];

@NgModule({
  imports: [
    PagesRoutingModule,
    ThemeModule,
    DashboardModule,
    MiscellaneousModule,

  ],
  entryComponents:[
    ModalComponent
  ],
  declarations: [
    `...PAGES_COMPONENTS,`
  ],
})
export class PagesModule {
}

----------------------------------------------------------------------
`mongo_db.php in CI
---------------------------------------------------------------------
$config['mongo_db']['active'] = 'default';

$config['mongo_db']['default']['no_auth'] = true;
$config['mongo_db']['default']['hostname'] = 'localhost';
$config['mongo_db']['default']['port'] = '27017';
$config['mongo_db']['default']['username'] = '';
$config['mongo_db']['default']['password'] = '';
$config['mongo_db']['default']['database'] = 'myancon';
$config['mongo_db']['default']['db_debug'] = true;
$config['mongo_db']['default']['return_as'] = 'array';
$config['mongo_db']['default']['write_concerns'] = (int) 1;
$config['mongo_db']['default']['journal'] = true;
$config['mongo_db']['default']['read_preference'] = 'primary';
$config['mongo_db']['default']['read_concern'] = 'local'; //'local', 'majority' or 'linearizable'
$config['mongo_db']['default']['legacy_support'] = false;

------------------------------------------------------------------
branch.php Controller in CI
-------------------------------------------------------------------
class Branches extends REST_Controller
{
  public function index_post() {
        if ($this->input->method(TRUE) === 'POST') {
            $_POST = json_decode(file_get_contents('php://input'), true);
            $data = array(
                "branchname" => $this->input->post('branchname')
                ,"address" => $this->input->post('address')
                ,"phone" => $this->input->post('phone')
        );

            if ($this->input->post('_id') != null) {
                $_id = $this->input->post("_id");

            } else {
                $this->Branch_model->add($data);
            }

            $this->response([], 201);
        }
    }
}

---------------------------------------------------------------------
Branch_model.php
--------------------------------------------------------------------
class Branch_model extends CI_Model
{
 public function add($data) {
        return $this->`mongo`_db->insert("branch", $data);
    }
}

Angular数据连接代码和数据插入代码请用Angular文件名解释!

`

0 个答案:

没有答案