This plugin exposes the ability to map columns on other tables to this Model.
See patio.plugins.ColumnMapper.mappedColumn for more information.
Static PropertiesProperty | Type | Default Value | Description |
fetchMappedColumnsOnSave | property | true
| Boolean flag indicating if mapped columns should be re-fetched on save. NOTE This can be overridden by passing {reload : false} to the patio.Model#save method. |
fetchMappedColumnsOnUpdate | property | true
| Boolean flag indicating if mapped columns should be re-fetched on update. NOTE This can be overridden by passing {reload : false} to the patio.Model#update method. |
Add a mapped column from another table. This is useful if there columns on another table but you do not want to load the association every time.
For example assume we have an employee and works table. Well we might want the salary from the works table, but do not want to add it to the employee table.
NOTE: mapped columns are READ ONLY.
patio.addModel("employee") .oneToOne("works") .mappedColumn("salary", "works", {employeeId : patio.sql.identifier("id")});
You can also change the name of the of the column
patio.addModel("employee") .oneToOne("works") .mappedColumn("mySalary", "works", {employeeId : patio.sql.identifier("id")}, { column : "salary" });
If you want to prevent the mapped columns from being reloaded after a save or update you can set the
fetchMappedColumnsOnUpdate
or fetchMappedColumnsOnSave
to false.
var Employee = patio.addModel("employee") .oneToOne("works") .mappedColumn("mySalary", "works", {employeeId : patio.sql.identifier("id")}, { column : "salary" }); //prevent the mapped columns from being fetched after a save. Employee.fetchMappedColumnsOnSave = false; //prevent the mapped columns from being re-fetched after an update. Employee.fetchMappedColumnsOnUpdate = false;
You can also override prevent the properties from being reloaded by setting the reload
or reloadMapped
options when saving or updating.
//prevents entire model from being reloaded including mapped columns employee.save(null, {reload : false}); employee.update(null, {reload : false}); //just prevents just the mapped columns from being reloaded employee.save(null, {reloadMapped : false}); employee.update(null, {reloadMapped : false});Arguments
the name you want the column represented as on the model.
the table or model you want the property mapped from
the join condition. See patio.Dataset#joinTable.
{}
] : additional options
"left"
] String
: the join type to use when gathering the properties.
null
] String|patio.sql.Identifer
: the column on the remote table that should be used as the local copy.
patio.Model
returns the model for chaining.
function (name,table,condition,opts){ opts = opts || {}; if (name) { name = sql.stringToIdentifier(name); if (table && condition) { opts = comb.merge({joinType: "left", table: table, condition: condition, column: name}, opts); this._mappedColumns[name] = opts; } else { throw new ModelError("mapped column requires a table and join condition"); } } return this; }