Closed raeldc closed 14 years ago
You can refer to the docs, but I'll give you an example as well. Your problem is that you're not telling the field where to find the relationship, so how is it supposed to know?
'children' => new Field_HasMany(array(
'foreign' => 'your_model_name.myparent_id'
// You can also do this and it will conver the parent
// to the right column automatically
'foreign' => 'your_model_name.parent'
))
Note: I'm organizing my models into subdirectories so by default, Jelly assumes subdirectory_parent is to be used as prefixes based on the model's class name. I don't want that so I have to explicitly specify which prefixes/columns/tables to use
That's how it has to be, though. How else can it magically know what models relate to what? If you want relationships fields to map to models that aren't the same as the actual name of the field, you have to manually specify it.
Jon, I didn't take the bold bit as a complaint just for our information. The issue here is that the related hasmany/hasone has to have foreign
set up correctly otherwise a non-standard column
value won't work. This is something again that could be clearer in the docs. I'll close this now as there is no apparent bug here.
Raeldc: if there is still an issue, please let us know and I'll re-open. Thanks for all your help with testing this.
I didn't take the bold bit as a complaint just for our information
In that case, my apologies raeldc. Silly text ambiguficating communication!
Just as I thought, I just don't understand how it works. But yes, a better explanation in the docs please? I'm really not sure what foreign
was until now. Thanks anyway.
I tried to test $column
with non-relationship fields. Is it supposed to work there too? For example, this model:
------------------------------------
->fields(array(
'id' => new Field_Primary,
'title' => new Field_String(array('column'=>'name')),
------------------------------------
Why can't I access $model->title
but I can access $model->name
?
Thanks again :)
I'm really not sure what foreign was until now
I'll put a better explanation in the docs, but suffice it to say for now that foreign specifies the model and column in the foreign table of the relationship. Setting it properly requires a basic understanding of how relationships map to each other.
For example, suppose we have an Author who has many Articles:
'articles' => new Field_HasMany(array(
'foreign' => 'article.author_id'
))
In the case of has many, you would specify the model name as well as the field or column name that connects the author's primary key.
I tried to test $column with non-relationship fields. Is it supposed to work there too?
Yes. This is the whole point of the aliasing in Jelly. It allows you to call a field by a different name than the column it references. This means you can decouple your database schema from your models. If, for whatever reason, you need to change the name of a column, you don't have to go hunting around everywhere in your model code to find the old column and change it as was the case with ORM.
It also means you can attach a model to an otherwise "ugly" database schema. For example, if you don't like the naming conventions used in the database, or they don't make sense within the context of your code you can always change the field names and have them reference a differently named column.
Why can't I access $model->title but I can access $model->name?
I don't know. You might want to post a separate bug report. I did, however, just test that aliasing works (it's unit tested as well) and your example "should" work.
------------------------------------
->fields(array(
'id' => new Field_Primary,
'title' => new Field_String(array('column'=>'name')),
------------------------------------
Just to be clear, the field 'title' is the name you use in your code. The 'column' is how the field is named in the database schema. We use this distinction in alot of different places in the documentation and such, so it's important to understand the difference. The same distinction exists for 'model' and 'table'.
OK got it. I now fully understand how this works. Thanks Jon for your help :)
I'm working on some One-To-Many relationships where I wanted to specify the column field of the parent instead of using the defaults.
I'm not really sure if I understand how this should work but here's a sample of my model declaration.
This is the parent
This is the children
The error when I access $parent->children Database_Exception [ 1054 ]: Unknown column 'childrentable.parent_id' in WHERE clause
It generates the default which is
parent_id
but I want to usemyparent_id
as the real column._Note: I'm organizing my models into subdirectories so by default, Jelly assumes
subdirectory_parent
is to be used as prefixes based on the model's class name. I don't want that so I have to explicitly specify which prefixes/columns/tables to use_