Jaydata中的复场操纵

时间:2013-06-12 07:42:06

标签: jaydata

Database Diagram

我有这样的数据库结构。每个员工都属于一个部门,每个部门都属于一个部门,每个部门都有一个员工清单和与之相关的股票清单。我使用以下代码初始化数据库:

$data.define("Org.Department",{
  ID: {key: true, type: "integer", computed: true},
  DepartmentID: {type: "integer", required: true},
  DepartmentName: {type: "string", required: true},
  Employee:{type: Array, elementType: "Org.Employee", navigationProperty:"Department"},
  Stock:{type: Array, elementType: "Org.Stock", navigationProperty:"Department"}
});

$data.define("Org.Employee",{
  ID: {key: true, type: "integer", computed: true},
  Name: {type: "string", required: true},
  DepartmentID: {type: "integer", required: true},
  Department: {type: "Org.Department", inverseProperty: "Employee"}
});

$data.define("Org.Stock",{
  ID: {key: true, type: "integer", computed: true},
  DepartmentID: {type: "integer", required: true},
  Description: {type: "string"},
  NumItems: {type: "integer", required: true},
  Department: {type: "Org.Department", inverseProperty: "Stock"}
});

$data.EntityContext.extend("Company",{
  Employees: {type: $data.EntitySet, elementType: Org.Employee},
  Departments: {type: $data.EntitySet, elementType: Org.Department},
  Stocks: {type: $data.EntitySet, elementType: Org.Stock}
});

var x= new Company({
  provider: "webSql",
  databaseName: "DB",
  version: 1,
  dbCreation: $data.storageProviders.DbCreationType.DropAllExistingTables
});

我想要做的是向该数据库添加元素,但我必须建立正确的连接。我的意思是正确的连接,我想查询所有员工的姓名,这些员工要求我查找部门名称并从中获取员工列表,然后列出他们的名字。 我尝试过很多东西,但它彻底失败了。例如:

x.onReady(function(){
  var dep;
  x.Departments.filter(function(res){
    return res.DepartmentID==4;
  }).forEach(function(it){
    dep=it;
  });
  console.log(dep);
    var emp=new Org.Employee({
    Name: 'Employee1',
    DepartmentID: 4,
    Department:dep
  });
  x.Employees.add(emp);
  x.Employees.saveChanges();
});

未能在员工中插入正确的部门信息。当我查询Employee1的Department字段时,我得到了未定义。

这也失败了:

x.Employees.add({Name:"Employee1",DepartmentID:4});

所有查询都成功地将一些数据插入数据库,但是他们的Department字段为空,这违背了拥有关系数据库的全部目的。

1 个答案:

答案 0 :(得分:2)

我在代码中修复了两个重要的事情,现在它正在运行:

  1. navigationProperty应为inverseProperty。你总是要用inverseProperty定义关系的另一面 - 请分享你找到这个navigationProperty的URL,我检查一下我是否可以修复它
  2. 如果您从数据库中读取项目并将其设置为关系,则必须将该项目附加到上下文

    $ data.define( “Org.Department”,{   ID:{key:true,type:“int”,computed:true},   DepartmentID:{type:“int”,required:true},   DepartmentName:{type:“string”,required:true},   员工:{type:Array,elementType:“Org.Employee”,inverseProperty:“Department”},   库存:{type:Array,elementType:“Org.Stock”,inverseProperty:“Department”} });

  3. 固定代码:

    $data.define("Org.Employee",{
      ID: {key: true, type: "int", computed: true},
      Name: {type: "string", required: true},
      DepartmentID: {type: "int", required: true},
      Department: {type: "Org.Department", inverseProperty: "Employee"}
    });
    
    $data.define("Org.Stock",{
      ID: {key: true, type: "int", computed: true},
      DepartmentID: {type: "int", required: true},
      Description: {type: "string"},
      NumItems: {type: "int", required: true},
      Department: {type: "Org.Department", inverseProperty: "Stock"}
    });
    
    $data.EntityContext.extend("Company",{
      Employees: {type: $data.EntitySet, elementType: Org.Employee},
      Departments: {type: $data.EntitySet, elementType: Org.Department},
      Stocks: {type: $data.EntitySet, elementType: Org.Stock}
    });
    
    var x= new Company({
      provider: "webSql",
      databaseName: "DB",
      version: 1
    });
    
    x.onReady(function(){
        var newDep = new x.Departments.elementType({DepartmentName: 'dep1', DepartmentID: 7});
        x.Departments.add(newDep);
        x.saveChanges(function() {
            x.Departments.first("it.DepartmentName == 'dep1'", {}, function(dep){
                  console.log(dep);
                  x.Departments.attach(dep);
                  var emp = new Org.Employee({
                    Name: 'Employee1',
                    DepartmentID: dep.DepartmentID,
                    Department:dep
                  });
                  console.log(emp);
                  x.Employees.add(emp);
                  x.Employees.saveChanges();
            });
        })
    });
    

    您可以在jsfiddle

    上找到实时版本