jonathangeiger / kohana-jelly

See the link below for the most up-to-date code
https://github.com/creatoro/jelly
MIT License
146 stars 34 forks source link

Unexpected behaviour with HasOne and load_with #76

Closed glamorous closed 14 years ago

glamorous commented 14 years ago

I don't know if I did something wrong or if it's a bug but I have this code:

class Model_Newsitem extends Jelly_Model
{
public static function initialize(Jelly_Meta $meta)
{
    $meta->fields(array(
        'id' => new Field_Primary,
        'author' => new Field_HasOne(array(
            'foreign' => 'user',
        )),
        'event' => new Field_HasOne,
        'date' => new Field_Timestamp(array(
            'auto_now_create' => TRUE,
        )),
        'name' => new Field_String,
        'uri' => new Field_Slug,
        'body' => new Field_Text,
        'status' => new Field_Enum(array(
            'choices' => array('open','closed','pending'),
        )),
        'categories' => new Field_ManyToMany,
    ))
        ->load_with(array('author'))
        ->sorting(array('date' => 'DESC'));
}
}

And it breaks on the load_with:

Unknown column 'rcc_users.newsitem_id' in 'on clause' [ SELECT rcc_newsitems.id AS id, rcc_newsitems.date AS date, rcc_newsitems.name AS name, rcc_newsitems.uri AS uri, rcc_newsitems.body AS body, rcc_newsitems.status AS status, rcc_users.id AS :author:id FROM rcc_newsitems LEFT JOIN rcc_users ON (rcc_newsitems.id = rcc_users.newsitem_id) WHERE rcc_newsitems.status = 'open' ORDER BY rcc_newsitems.date DESC LIMIT 3 ]

So there's something wrong with what I did here:

'author' => new Field_HasOne(array(
    'foreign' => 'user',
)),

Isn't this correct? Every newsitem has ONE author (= user)

Sorry if I did something wrong but there isn't much documentation about this :s

jonathangeiger commented 14 years ago

Is rcc_users.newsitem_id a valid column? What should it be joining on?

glamorous commented 14 years ago

No, not a valid column, I have a column user_id in rcc_newsitems. But thinking about it, that should be named author_id I suppose and my code will work? Gonna give it a try tonight.

banks commented 14 years ago

I suppose and my code will work?

I doubt it. For a start , surely the foreign_key is in this model? So the author field should be Belongs_to user? HasOne implies that there is a BenlongsTo field in the user model which seems completely wrong to me - a user doesn't belong to a news story, they have (presumably) many news stories so you need to change that for a start.

THEN if you use

'author' => new Field_BelongsTo(array(
    'foreign' => 'user'
))

And your foreign key column in the newsitems table is called author_id it should work.

glamorous commented 14 years ago

Wow, my relation is indeed wrong.. :( I read it wrong.. I read it as: "a newsitem hasone user instead of: "a newsitem belongsto an user and a user hasmany newsitems"

thanks for the info, I will try it tonight and close this when it works..

glamorous commented 14 years ago

my column may still be named user_id and it worked!