Question

I have the following model:

class Model_Job extends ORM
{
    public $ID;

    public $user_ID;
    public $title;
    //some more variables
}

In my controller I have a function action_view(); view job details single job, implemented exactly like http://kohanaframework.org/3.3/guide/orm/using#finding-an-object

public function action_view()
{
    $this->render('Shit');
    $this->template->content = View::factory('jobs/job')
                                    ->bind('job', $job);

    $job = ORM::factory('job', $this->request->param('id'));                    
}

I have another function action_all() which simply fetches all jobs using find_all and puts them on the page, this works great (meaning echo $job->ID does what it should do; echo the ID. However action_view() does not. I'll place some the output of echo Debug::vars($job)

object Model_Job(39) {
    public ID => NULL //Note they are NULL
    public user_ID => NULL 
    public title => NULL
    ......................
    protected _object => array(5) (
        "ID" => string(1) "1"
        "user_ID" => string(1) "1"
        "title" => string(14) "Testbaantjeeee"
        ................
    )
    .....................
 }

Whereas an example of an echo Debug::vars($job) from action_all() would look like:

object Model_Job(39) {
    public ID => 1 //Note they are NOT NULL
    public user_ID => 1
    public title => "Testbaantjeeee"
    ......................
    protected _object => array(5) (
        "ID" => string(1) NULL       //now these are NULL
        "user_ID" => string(1) NULL
        "title" => string(14) NULL
        .....................
    )
    .....................
 }

I looked in kohena's documentation about factory, find, find_all etc. but could not figure out what factory or find is NOT doing what find_all is doing. Am I missing something? I got it working using:

$job = ORM::factory('job')
           ->where('ID', '=', $this->request->param('id'))
           ->find_all()[0];

But doing that makes absolutely no sense to me. What am I missing?

Was it helpful?

Solution

Well I went ahead and worked around it.

I created a class Model_Base

class Model_Base extends ORM
{
    public function Load()
    {
        foreach($this->object() as $key => $value)
            if(!is_object($value))
                if(property_exists(get_class($this), $key))
                    $this->$key = $value;
    }
}

Now I extend all my models from here

class Model_Job extends Model_Base
{
    ..................
}

And now my controller uses this:

public function action_view()
{
    $this->render('Shit');
    $this->template->content = View::factory('jobs/job')
                                    ->bind('job', $job);

    $job = ORM::factory('job', $this->request->param('id'));
    $job->Load();
}

And it dumps:

object Model_Job(39) {
    protected _primary_key => string(2) "ID"
    public ID => string(1) "1"
    public user_ID => string(1) "1"
    public title => string(14) "Testbaantjeeee"
    ......................
}

I still think it makes no sense. But whatever. If anyone knows why they made find()/factory('foo', $id) and find_all() so fundamentally different that the former is useless, let me know :)

OTHER TIPS

You are using a different key for your id, it is capitalized ID. Make sure in your model to set

protected $_primary_key = 'ID';

Because following your example, both methods just should echo exactly the same.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top