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

Calling idiorm from inside classes #21

Closed jockster closed 13 years ago

jockster commented 13 years ago

Hi again Jamie,

I guess that this is a no-brainer for you, but it's starting to give me grey hairs :)

I have instantiated idiorm like in your example; ORM::configure('mysql:host=192.168.1.1;dbname=myDb'); ORM::configure('username', 'root'); ORM::configure('password', 'root');

I need to access the ORM static variable from a method inside a class, like this for instance

class Sessions extends myModels {
    function get($id) {
        ORM::for_table('sessions')->find_one($id); // Throws Fatal error: Class 'ORM' not found
    }  
}

How should I do this the most proper way?

j4mie commented 13 years ago

Hi,

I really have no idea what's going on here - are those two bits of code inside the same file? If the top code works but the bottom code doesn't, I really don't know what the problem is. But it's definitely not Idiorm specific - it's a general problem with the structure of your code.

Hint: if the first code section is in a file called a.php and the second code section is in a file called b.php, then you need the following line at the top of b.php:

require_once 'a.php';
jockster commented 13 years ago

Hi Jamie,

Really sorry for the confusion caused.

The both snippets are in the same file - the Sessions class are written just below the ORM::configure...-lines. I can't just get it to work. Obviously the idiorm.php is properly included as I can make the "ORM::for_table('sessions')->find_one($id);" line work perfectly if I move it out of my class.

Any ideas?

Thanks,

jockster commented 13 years ago

Hi Jamie, Sorry for taking your time with this. It appears that as I used the class to create my own session handler, I needed to call session_write_close(); before the end of execution, so nothing wrong with Idiorm :)

Thanks a lot for your support, again - sorry to take your time.

j4mie commented 13 years ago

Great, glad you fixed it.

Jamie