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

declaring foriegn and primary key to one attribute #145

Closed pundit closed 14 years ago

pundit commented 14 years ago

Is it possible to declare two properties to one attribute. for example, i have an image table that is a child of another table called media. the id field in image is the foriegn key that links image to media as well as the primary key for the said table. is it possible to do this this using jelly.

This is what i have so far:

public static function initialize(Jelly_Meta $meta) { $meta->table('image') ->fields(array( 'id' => new Field_Primary, 'caption' => new Field_String, )); }

leth commented 14 years ago

I'm not sure I understand, do you want to be able to do the following?

echo $media->image->caption;
banks commented 14 years ago

I think what you want is to use the same database column for two different fields right? on a primary field and the other a belongs to field correct?

Yes Jelly can do this. In fact it is a rare feature but is possible in Jelly. Off the top of my head I can't remember the exact syntax but look at the docs for defining relationships/fields. You need two fields with different field names in the module but where their column property points to the same column.

The caveat is that you will need to be careful about updating the relationship on the fly as it will also change the primary key and Jelly wouldn't automatically handle that - you could get into a bit of a mess.