Closed cayasso closed 14 years ago
The meta data looks like it is set up correctly. We'll have to work out where this is failing. Thanks for the bug report.
Can you confirm that the following doesn't work? You should be referencing your fields by the names you give them, not the columns they represent.
Jelly::factory('comment')
->set(array(
'post' => '10',
'author' => 'me',
'email' => 'cayasso@gmail.com',
))->save();
(Notice I've changed 'post_id' to 'post')
It works perfectly the way you show it, however this can lead to confusion, it would be good to have the model identify by default the foreign key.
Thank you for the solution.
JB
The whole point of the fields is so that you can reference columns by different names than their equivalent in the database.
If so, this shouldn't fail
public static function initialize(Jelly_Meta $meta)
{
$meta->fields(array(
'id' => new Field_Primary,
'post_id' => new Field_BelongsTo,
'url' => new Field_String,
'email' => new Field_Email,
'author' => new Field_String,
'subject' => new Field_String,
));
}
Jelly::factory('comment')
->set(array(
'post_id' => '10',
'author' => 'me',
'email' => 'cayasso@gmail.com',
))->save();
But it does fail because it converts the database field to post_id_id
Thank you Jonathan
JB
Fields aren't that smart. By default, A Field_BelongsTo assumes the columns it's referencing is the name you've given it plus '_id', simply because that's the convention in many ORMs and that's what most people are used to.
If you want to call it 'post_id' you'll have to declare the field as such:
'post_id' => new Field_BelongsTo(aray('column' => 'post_id')),
Or, as a shortcut, you can just pass a string:
'post_id' => new Field_BelongsTo('post_id'),
Most people would choose to simply call it 'post' because the field, when referenced, returns a post object, not an id, so calling it 'post' seems more correct semantically.
If so, this shouldn't fail
Jelly is doing exactly what you would expect here. If you called your field 'post_id' and the default is to append '_id' to the FIELD name if no COLUMN name is specified, then you would expect COLUMN to be 'post_id_id'.
You should define the FIELD names to be what you want your object properties to be and then set up the COLUMNs to reflect your database column names assuming you are not using adhering to the default conventions. The convention is that a belongsTo field called 'post' would represent a column called 'post_id' and would be accessed in the model as $model->post etc.
So really you should define the field name to be 'post', the column will by default by 'post_id' and when you set the value in your model, you always use the FIELD name i.e.
->set(array(
'post' => 10,
...
))
Interesting issue, I have these models
Comment Model
Post Model
When I try to do a simple insert like this in my controller
I get the fallowing error Database_Exception [ 1048 ]: Column 'post_id' cannot be null [ INSERT INTO
comments
(author
,email
,post_id
,url
,subject
) VALUES ('me', 'cayasso@gmail.com', NULL, NULL, NULL) ] Of course the error is because the field is required in my database setup, but the error should not be triggering because I already set a value of 10 for post_id. Shouldn't Jelly recognize the "post" field in the Comment Model as "post_id" by default? If I change the Comment Model like this, it works, but this doesn't make sense because Jelly should already be converting to default foreign keys.class Model_Comment extends Jelly_Model {
} // End Comment Model
Thank in advance for the help.
My model metadata info is:
object Jelly_Meta(16) { protected initialized => bool TRUE protected model => string(7) "comment" protected db => string(7) "default" protected table => string(8) "comments" protected primary_key => string(2) "id" protected name_key => string(4) "name" protected foreign_key => string(10) "comment_id" protected sorting => array(0) protected load_with => array(0) protected input_prefix => string(11) "jelly/field" protected fields => array(12) ( "id" => object Field_Primary(13) { public primary => bool TRUE public model => string(7) "comment" public column => string(2) "id" public label => string(2) "id" public name => string(2) "id" public unique => bool FALSE public description => string(0) "" public in_db => bool TRUE public default => NULL public null => bool FALSE public filters => array(0) public rules => array(0) public callbacks => array(0) } "post" => object Field_BelongsTo(14) { public in_db => bool TRUE public foreign => array(2) ( "model" => string(4) "post" "column" => string(12) ":primary_key" ) public model => string(7) "comment" public column => string(7) "post_id" public label => string(4) "post" public name => string(4) "post" public unique => bool FALSE public description => string(0) "" public primary => bool FALSE public default => NULL public null => bool FALSE public filters => array(0) public rules => array(0) public callbacks => array(0) } "url" => object Field_String(13) { public model => string(7) "comment" public column => string(3) "url" public label => string(3) "url" public name => string(3) "url" public unique => bool FALSE public description => string(0) "" public primary => bool FALSE public in_db => bool TRUE public default => NULL public null => bool FALSE public filters => array(0) public rules => array(0) public callbacks => array(0) } "email" => object Field_Email(13) { public rules => array(1) ( "email" => NULL ) public model => string(7) "comment" public column => string(5) "email" public label => string(5) "email" public name => string(5) "email" public unique => bool FALSE public description => string(0) "" public primary => bool FALSE public in_db => bool TRUE public default => NULL public null => bool FALSE public filters => array(0) public callbacks => array(0) } "author" => object Field_String(13) { public model => string(7) "comment" public column => string(6) "author" public label => string(6) "Author" public name => string(6) "author" public unique => bool FALSE public description => string(0) "" public primary => bool FALSE public in_db => bool TRUE public default => NULL public null => bool FALSE public filters => array(0) public rules => array(0) public callbacks => array(0) } "subject" => object Field_String(13) { public model => string(7) "comment" public column => string(7) "subject" public label => string(7) "subject" public name => string(7) "subject" public unique => bool FALSE public description => string(0) "" public primary => bool FALSE public in_db => bool TRUE public default => NULL public null => bool FALSE public filters => array(0) public rules => array(0) public callbacks => array(0) } "page" => object Field_String(13) { public model => string(7) "comment" public column => string(4) "page" public label => string(3) "Web" public name => string(4) "page" public unique => bool FALSE public description => string(0) "" public primary => bool FALSE public in_db => bool TRUE public default => NULL public null => bool FALSE public filters => array(0) public rules => array(0) public callbacks => array(0) } "message" => object Field_Text(13) { public model => string(7) "comment" public column => string(7) "message" public label => string(7) "Comment" public name => string(7) "message" public unique => bool FALSE public description => string(0) "" public primary => bool FALSE public in_db => bool TRUE public default => NULL public null => bool FALSE public filters => array(0) public rules => array(0) public callbacks => array(0) } "ip" => object Field_String(13) { public model => string(7) "comment" public column => string(2) "ip" public label => string(2) "IP" public name => string(2) "ip" public unique => bool FALSE public description => string(0) "" public primary => bool FALSE public in_db => bool TRUE public default => NULL public null => bool FALSE public filters => array(0) public rules => array(0) public callbacks => array(0) } "agent" => object Field_String(13) { public model => string(7) "comment" public column => string(5) "agent" public label => string(5) "agent" public name => string(5) "agent" public unique => bool FALSE public description => string(0) "" public primary => bool FALSE public in_db => bool TRUE public default => NULL public null => bool FALSE public filters => array(0) public rules => array(0) public callbacks => array(0) } "created" => object Field_Timestamp(17) { public auto_now_create => bool TRUE public auto_now_update => bool FALSE public format => NULL public pretty_format => string(1) "r" public model => string(7) "comment" public column => string(7) "created" public label => string(7) "created" public name => string(7) "created" public unique => bool FALSE public description => string(0) "" public primary => bool FALSE public in_db => bool TRUE public default => NULL public null => bool FALSE public filters => array(0) public rules => array(0) public callbacks => array(0) } "status" => object Field_Boolean(17) { public true => integer 1 public label_true => string(3) "Yes" public false => integer 0 public label_false => string(2) "No" public model => string(7) "comment" public column => string(6) "status" public label => string(6) "status" public name => string(6) "status" public unique => bool FALSE public description => string(0) "" public primary => bool FALSE public in_db => bool TRUE public default => NULL public null => bool FALSE public filters => array(0) public rules => array(0) public callbacks => array(0) } ) protected aliases => array(0) protected builder => string(21) "model_builder_comment" protected columns => array(12) ( "id" => array(1) ( 0 => string(2) "id" ) "post_id" => array(1) ( 0 => string(4) "post" ) "url" => array(1) ( 0 => string(3) "url" ) "email" => array(1) ( 0 => string(5) "email" ) "author" => array(1) ( 0 => string(6) "author" ) "subject" => array(1) ( 0 => string(7) "subject" ) "page" => array(1) ( 0 => string(4) "page" ) "message" => array(1) ( 0 => string(7) "message" ) "ip" => array(1) ( 0 => string(2) "ip" ) "agent" => array(1) ( 0 => string(5) "agent" ) "created" => array(1) ( 0 => string(7) "created" ) "status" => array(1) ( 0 => string(6) "status" ) ) protected defaults => array(12) ( "id" => NULL "post" => NULL "url" => NULL "email" => NULL "author" => NULL "subject" => NULL "page" => NULL "message" => NULL "ip" => NULL "agent" => NULL "created" => NULL "status" => NULL ) protected field_cache => array(0) }