In my model I wrote something like this:
Table1 = new JazzRecord.Model({
table: "parent",
foreignKey: "parent_id",
hasMany: {childs: "child"},
columns: {
table1Column: "text"
}
Table2 = new JazzRecord.Model({
table: "child",
belongsTo: {parent: "parent"},
columns: {
table2Column: "text",
parent_id: "number"
}
To create a "parent" record with "child" records and save all together
should I do something like this?:
//Create parent record
var record = Table1.newRecord({
table1Column: "someText"
});
//Create childs array
record.childs = new Array();
//Create child #1
record.childs.push(Table2.newRecord({
table2Column: "someText",
parent_id: record.id
});
//Create child #2
record.childs.push(Table2.newRecord({
table2Column: "anotherText",
parent_id: record.id
});
//Save all togegher to dataBase
record.save();
That's the correct way to do it?
That's almost correct, but a few close parentheses are missing, and
you need to be sure to call JazzRecord.migrate() after declaring the
models. Also, a base record needs to be saved before new associated
records will save correctly. An updated version of the code follows:
Table1 = new JazzRecord.Model({
table: "parent",
foreignKey: "parent_id",
hasMany: {childs: "child"},
columns: {table1Column: "text"}
});
Table2 = new JazzRecord.Model({
table: "child",
belongsTo: {parent: "parent"},
columns: {
table2Column: "text",
parent_id: "number"
}
});
//JazzRecord's internal picture of the schema must be updated/tables
created by calling migrate()
JazzRecord.migrate();
//Create parent record
//base record must be saved before associated records can be pushed
onto the association array
//create() is like newRecord() and save() combined
var record = Table1.create({
table1Column: "someText"
});
//Create child #1
//previously missing parenthesis
record.childs.push(Table2.newRecord({
table2Column: "someText"
}));
//Create child #2
//previously missing parenthesis
record.childs.push(Table2.newRecord({
table2Column: "anotherText"
}));
//Save all togegher to dataBase
record.save();
--
You received this message because you are subscribed to the Google Groups "JazzRecord" group.
To post to this group, send email to jazzr...@googlegroups.com.
To unsubscribe from this group, send email to jazzrecord+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jazzrecord?hl=en.
On Feb 8, 1:49 am, Sergio Viudes <djpep...@gmail.com> wrote:
> Thanks for your answer!!
> Sorry about missing parentheses, I wrote this code to ask you, it isn't my
> real code.
> It's working 100%, thank you!
>
> 2010/2/8 Nick Carter <thynct...@gmail.com>
> > jazzrecord+...@googlegroups.com<jazzrecord%2Bunsu...@googlegroups.com>