0

Here is a small example. I want to reuse the mongodb object in many other methods in this class, without initializing it within each function.

Or is there a better approach in general?

 <?php
class Model_Users extends Model 
{

    public static function _init()
    {
        $mongodb = \Mongo_Db::instance('content');
    }


    public static function get_user($name)
    {

        $user = $mongodb->get('users', array( 'name' => $name ));

        return  $user; 
    }


}

3 Answers 3

1

Do something like this:

class Model_Users extends Model 
{
    private static $mongodb;

    public static function _init()
    {
        self::$mongodb = \Mongo_Db::instance('content');
    }


    public static function get_user($name)
    {

        $user = self::$mongodb->get('users', array( 'name' => $name ));

        return  $user; 
    }


}

This way you're creating the object for use in every method within the class.

Sign up to request clarification or add additional context in comments.

8 Comments

I tried that, but this was the response: ErrorException [ Error ]: Using $this when not in object context: $this->mongodb = \Mongo_Db::instance('content');
Yeah, sorry - you'll need to initialize the object instead of making a direct call to the instance method - so it will be: $this->mongodb = new Mongo_Db(); and then you can use: $this->mongodb->instance('content');
Thank you. That partially works, but doesnt access the instance of mongo_db that includes the relevant connection details. It essentially creates a new / fresh instance and not through the fuelphp core. I appreciate your help.
Is there already an variable setup for $mongodb somewhere else in the code? if so, you can use global $mongodb; and then $this->mongodb = $mongodb; - I will edit the code to reflect
Thank you so much. That actually didnt work, but after much trial and error I found a solution.
|
1

How about something like this? Note, the PHP probably doesn't compile. The thrust of my post is that your model objects aren't database objects. Create a database object that is separate and provides the basic functionality that most of your model/DAO/ActiveRecord/whatever classes need. Then let your model focus on business logic, not database code. You might find that if you need to switch from Mongo to some other database later, it'll be easier because you've encapsulated all the database specific code into one place.

So, something like the following. My post is about the design, not the language.

class Db {
    private static $mongoDb;
    public static function _init() {
        self::$mongoDb = \Mongo_Db::instance( 'content' );
    }
    public static function getValue( $table, $name ) {
        $value = "";
        return self::$mongoDb->get( $table, array( "name", $name ) );
    }
}

class Model_Users extends Model {
    private $db;
    public static function _init() {
        $this->db = new Db();
    }
    public static function getUser( $name ) {
        return $this->db->getValue( "users", $name );
    }
}

2 Comments

Thank you so much for this excellent advice. There is a logic controller that is calling this model controller, so in effect it is doing what you are saying, but I agree that it could be abstracted even further.
Glad if it helps. I wish it was a full working example, but you probably know your PHP better than I do and can make something out of it.
0

I believe that is the purpose of using $this. It makes things accessible to the rest of the object.

<?php
class Model_Users extends Model 
{

    private $mongodb;

    public static function _init()
    {
        $this->mongodb = \Mongo_Db::instance('content');
    }

    public static function get_user($name)
    {

        $user = $this->mongodb->get('users', array( 'name' => $name ));

        return  $user; 
    }
}

1 Comment

I tried that, but this was the response: ErrorException [ Error ]: Using $this when not in object context: $this->mongodb = \Mongo_Db::instance('content');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.