j4mie / idiorm

A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5.
http://j4mie.github.com/idiormandparis/
2.01k stars 369 forks source link

Non static usage #271

Closed codecowboy closed 9 years ago

codecowboy commented 9 years ago

If I want to reuse a configured connection, what pattern would you recommend for using it throughout a small application? Use a singleton? The objective is to not have to repeat calls to ORM::configure()

I may also need multiple connections so maybe a factory would be better. Are there any examples of this anywhere?

tag commented 9 years ago

See the documentation on multiple connections for examples. All of the queries accept an optional trailing parameter that specifies the connection using a string key.

You should only have to use ORM::configure() once.

codecowboy commented 9 years ago

"You should only have to use ORM::configure() once." That's what I thought but it was not true in my use case. I am developing a WordPress plugin which needs access to a legacy database.

If I called ORM::Configure in plugin code, it was not available in templates. e.g.:

$job = ORM::for_table('jobs_ol')->where('job_ref', (int) $wp_query->get('job'))->find_one(); resulted in: Uncaught exception 'PDOException' with message 'could not find driver'

However, ensuring that ORM::Configure() was called earlier in the plugin execution chain resolved this for me. In case others are using idiorm and WordPress Plugin Boilerplate, this meant ensuring it was called by the main plugin file's constructor.

treffynnon commented 9 years ago

I don't know much of WordPress plugin development, but it seems to me there shouldn't be any queries from templates anyway unless I am misunderstanding terminology here. Either way I am glad you got it sorted out. On 4 May 2015 15:42, "Luke Mackenzie" notifications@github.com wrote:

Closed #271 https://github.com/j4mie/idiorm/issues/271.

— Reply to this email directly or view it on GitHub https://github.com/j4mie/idiorm/issues/271#event-296311016.