1
votes

I'm using kohana 3.2. and I'm having some trouble getting this new model to work.. There's the model: Model_User_Unavailability which works fine and there's the model Model_User_Unavailability_Status which doesn't.

The problem isn't finding the model, it's using it. I can make a status using: ORM::factory('user_unavailability_status'); and it works just fine, but when I want to add the status to the unavailability class it won't work and I'm getting the following exception:

Incorrect table name '' [ INSERT INTO `` (`user_unavailability_id`, `status_id`) VALUES ('670', NULL) ]

The classes look like this:

class Model_User_Unavailability extends ORM
{
    protected $_belongs_to = array(
        'user' => array(),
    );
    protected $_table_name = 'user_unavailability';

    protected $_has_many = array(
        'status' => array(
            'model' => 'User_Unavailability_Status', 
            'foreign_key' => 'user_unavailability_id'
        )
    );
...etc

­

class Model_User_Unavailability_Status extends ORM
{
    protected $_table_name = 'user_unavailability_status';
    protected $_belongs_to = array(
        'user_unavailability' => array(
            'foreign_key' => 'user_unavailability_id', 
            'model' => 'user_unavailability'
        )
    );
    etc...

Now the exception appears when I try the following:

$db = $user->unavailable->where('id', '=', $id)->find(); // Returns User_Unavailability object
//... do some other stuf...
$db->save();

if($db->loaded())
{
    $status = ORM::factory('user_unavailability_status');
    $status->status = 'requested';
    $status->responder_id = 1;
    $db->add('status',$status);
}

As you can see in the exception earlier it won't use the specified table name. However, when I remove the $_table_name variable from the status class I get the following error:

Table 'urenregistratie.user_unavailability_statuses' doesn't exist [ SHOW FULL COLUMNS FROM `user_unavailability_statuses` ]

As you can see it does find the model, but won't find the database table. So my question here is, what's going on here? I guess I'm missing something but I don't know what...

Same happens when i use the $this->_table_names_plural = false in the status model.

So... any suggestions / ideas?

Edit:

It seems to work when I change the $db->add('status',$status); line to:

$status->user_unavailability_id = $db->id;
$status->save();

Not pretty though and still wondering what's preventing me from using the add function.

1
Its better to use $status->user_availability = $db. Also, you dont need to call $status->save() before saving belongs_to relationship. - biakaveron
I'm having the same trouble. @biakaveron can you help clarify this as it is exactly what I'm confused by. If Model_A belongs to Model_B, and I do $a->b = $b;, I don't need to call $a->save() or I don't need to call $b->save()?? Or wait, I simply never call save because the assignment (__set()) will do the job? - Lim
No, __set() will just modify model without saving (github.com/kohana/orm/blob/3.3/master/classes/Kohana/…). Make all changes in $a model and then save it. - biakaveron

1 Answers

2
votes

add() is used in has_many "through" relationships.

In your case you have already written the correct way:

$status->user_unavailability_id = $db->id;
$status->save();