Welcome Guest, Not a member yet? Register   Sign In
Datamapper Relationships : Database Error
#2

[eluser]WanWizard[/eluser]
First of all, I'm suprised you get this far.

Relation names are case sensitive, to be able to find the mapping between the two models the case should be the same.

The same is true for the mapping between the relation names and the foreign keys. If you use 'News' as relation name, Datamapper expects a foreign key called 'News_id', and not 'news_id'.

Because Datamapper can't find the foreign key, it starts looking for a relationship table. And gives you this error because it doesn't exist too.

Code:
class NewsModel extends DataMapper {

var $table = "news";

var $has_many = array(
  'comment' => array(
   'class' => 'CommentModel',
   'other_field' => 'news'
  )
);
}

class CommentModel extends DataMapper {

var $table = "comment";

var $has_one = array(
  'news' => array(
   'class' => 'NewsModel',
   'other_field' => 'comment'
  )
);
}

Now this works fine.

Code:
$news = new NewsModel();
$news->get_by_id(1);
$news->comment->get();

Note that you have an error in your comment table definition too. There is no column called 'comment_id', so the create fails on the primary key.


Messages In This Thread
Datamapper Relationships : Database Error - by El Forum - 05-19-2012, 06:22 PM
Datamapper Relationships : Database Error - by El Forum - 05-20-2012, 03:27 AM
Datamapper Relationships : Database Error - by El Forum - 05-20-2012, 06:52 AM
Datamapper Relationships : Database Error - by El Forum - 05-20-2012, 07:00 AM



Theme © iAndrew 2016 - Forum software by © MyBB