How should a Model be turned to a Instance in ThinkPHP framework?

211 Views Asked by At

This question concerns merely ThinkPHP programmers.

So there are Model and D() in ThinkPHP. A model is very useful to communicate with the DB. But when we use D() to create a Model then find a piece of data. actually what we get is a array.

Then how can I get a instance that is of a class and has methods to use. To build methods in the model seems useless.

Should I have another class apart from the model, for example UserModel.class + User.class

1

There are 1 best solutions below

0
On

Below is what I thought out.

class BasicModel extends Model
{

    protected $instance;

    public function loadInstance($id)
    {
        $this->instance = $this->find($id);
    }

    public function saveInstance()
    {
        if ($this->create($this->instance))
            return $this->add();
        return false;
    }
}