{"__v":1,"_id":"55097b11dd77250d00736a07","category":{"__v":16,"_id":"550974cc368a56170041475c","project":"55070e814bb83b2500ec9404","version":"550974cb368a561700414757","pages":["55097a674c7c3f2300aabf07","55097a8a4c7c3f2300aabf09","55097a92ad1f0523008ecbd4","55097a9faa9bd525001a065c","55097aa92dd6a11900e6e7b4","55097ab2ad1f0523008ecbd6","55097ac74c7c3f2300aabf0b","55097ace2dd6a11900e6e7b7","55097ad5dd77250d007369fa","55097adead1f0523008ecbd9","55097ae72dd6a11900e6e7b9","55097aefdd77250d007369fc","55097af8dd77250d00736a05","55097aff4c7c3f2300aabf0d","55097b07aa9bd525001a0660","55097b11dd77250d00736a07"],"reference":false,"createdAt":"2015-03-18T11:08:27.090Z","from_sync":false,"order":4,"slug":"database-interaction-through-models","title":"Database Interaction Through Models"},"project":"55070e814bb83b2500ec9404","user":"55070d24d30b3f190011b941","version":{"__v":1,"_id":"550974cb368a561700414757","forked_from":"55070e814bb83b2500ec9407","project":"55070e814bb83b2500ec9404","createdAt":"2015-03-18T12:51:23.709Z","releaseDate":"2015-03-18T12:51:23.709Z","categories":["550974cc368a561700414758","550974cc368a561700414759","550974cc368a56170041475a","550974cc368a56170041475b","550974cc368a56170041475c","550974cc368a56170041475d","550974cc368a56170041475e"],"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.4.0","version":"1.4"},"updates":[],"createdAt":"2015-03-18T13:18:09.383Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"required","params":[],"url":""},"order":17,"body":"Sometimes you need to pull data from more than one database, whether it's by choice (for performance or security reasons, perhaps) or because that's the way your infrastructure is set up. It's something you have to find a way to deal with.\n\nWheels has built-in functionality for this so that you don't have to revert back to writing the queries and setting the data source manually whenever you need to use a data source other than the default one. In order accomplish this, you will use the [dataSource()](doc:datasource) function.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Using the dataSource() Function\"\n}\n[/block]\nOverriding the default data source is done on a per model basis in Wheels by calling the [dataSource()](doc:datasource) function from within your model's `init()` method. By doing this, you instruct wheels to use that data source whenever it interacts with that model.\n\nHere's an example of a model file:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<cfcomponent extends=\\\"Model\\\">\\n\\n    <cffunction name=\\\"init\\\">\\n        <cfset dataSource(\\\"mySecondDatabase\\\")>\\n    </cffunction>\\n\\n</cfcomponent>\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\nIt's important to note that in order for Wheels to use the data source, it must first be configured in your respective CFML engine (i.e. in the Adobe ColdFusion or Railo Administrator).\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Does Not Work with Associations\"\n}\n[/block]\nOne thing to keep in mind when using multiple data sources with Wheels is that it doesn't work across associations. When including another model within a query, Wheels will use the calling model's data source for the context of the query.\n\nLet's say you have the following models set up:\n\n`models/Photo.cfc:`\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<cfcomponent extends=\\\"Model\\\">\\n\\n    <cffunction name=\\\"init\\\">\\n        <cfset dataSource(\\\"myFirstDatabase\\\")>\\n        <cfset hasMany(\\\"photoGalleries\\\")>\\n    </cffunction>\\n\\n</cfcomponent>\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\n`models/PhotoGallery.cfc:`\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<cfcomponent extends=\\\"Model\\\">\\n\\n    <cffunction name=\\\"init\\\">\\n        <cfset dataSource(\\\"mySecondDatabase\\\")>\\n    </cffunction>\\n\\n</cfcomponent>\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\nBecause the `photo` model is the main model being used in the following example, its data source (`myFirstDatabase`) will be the one used in the query that [findAll()](doc:findall) ends up executing.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<cfset myPhotos = model(\\\"photo\\\").findAll(include=\\\"photoGalleries\\\")>\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]","excerpt":"How to use more than one database in your Wheels application.","slug":"using-multiple-data-sources","type":"basic","title":"Using Multiple Data Sources"}

Using Multiple Data Sources

How to use more than one database in your Wheels application.

Sometimes you need to pull data from more than one database, whether it's by choice (for performance or security reasons, perhaps) or because that's the way your infrastructure is set up. It's something you have to find a way to deal with. Wheels has built-in functionality for this so that you don't have to revert back to writing the queries and setting the data source manually whenever you need to use a data source other than the default one. In order accomplish this, you will use the [dataSource()](doc:datasource) function. [block:api-header] { "type": "basic", "title": "Using the dataSource() Function" } [/block] Overriding the default data source is done on a per model basis in Wheels by calling the [dataSource()](doc:datasource) function from within your model's `init()` method. By doing this, you instruct wheels to use that data source whenever it interacts with that model. Here's an example of a model file: [block:code] { "codes": [ { "code": "<cfcomponent extends=\"Model\">\n\n <cffunction name=\"init\">\n <cfset dataSource(\"mySecondDatabase\")>\n </cffunction>\n\n</cfcomponent>", "language": "text" } ] } [/block] It's important to note that in order for Wheels to use the data source, it must first be configured in your respective CFML engine (i.e. in the Adobe ColdFusion or Railo Administrator). [block:api-header] { "type": "basic", "title": "Does Not Work with Associations" } [/block] One thing to keep in mind when using multiple data sources with Wheels is that it doesn't work across associations. When including another model within a query, Wheels will use the calling model's data source for the context of the query. Let's say you have the following models set up: `models/Photo.cfc:` [block:code] { "codes": [ { "code": "<cfcomponent extends=\"Model\">\n\n <cffunction name=\"init\">\n <cfset dataSource(\"myFirstDatabase\")>\n <cfset hasMany(\"photoGalleries\")>\n </cffunction>\n\n</cfcomponent>", "language": "text" } ] } [/block] `models/PhotoGallery.cfc:` [block:code] { "codes": [ { "code": "<cfcomponent extends=\"Model\">\n\n <cffunction name=\"init\">\n <cfset dataSource(\"mySecondDatabase\")>\n </cffunction>\n\n</cfcomponent>", "language": "text" } ] } [/block] Because the `photo` model is the main model being used in the following example, its data source (`myFirstDatabase`) will be the one used in the query that [findAll()](doc:findall) ends up executing. [block:code] { "codes": [ { "code": "<cfset myPhotos = model(\"photo\").findAll(include=\"photoGalleries\")>", "language": "text" } ] } [/block]