Welcome Guest, Not a member yet? Register   Sign In
Accessing fields in many-to-many table
#1

[eluser]Unknown[/eluser]
Hello,
I'm using Datamapper ORM v1.8, and I'm trying to access a field that is specific to the many-to-many relationship table, but I'm having difficulty as this isn't discussed in the documentation.

If we use the example in the documentation where the models Author and Book are joined by the authors_books table, where this table contains the fields id, author_id and book_id. Let's say that this table also includes a field called random_data. How would the value of random_data be accessed from the author object? The following does not work

Code:
class Author extends DataMapper {

var $has_many = array('book');
function get_random_data($author_id){
    return $this->book->select('random_data')->get_where(array('author_id'=>$author_id),1,0);
}
}

If I tried selecting $this->authors_books->, I get an error regarding authors_books being a non-object. How should I proceed?
#2

[eluser]WanWizard[/eluser]
You can't access join fields directly from the object, as join tables are 'invisible' from the relations point of view.

Read the section in the manual about Join Fields.
#3

[eluser]bEz[/eluser]
sample code should be updated as following correct?
Code:
// Create objects
$u = new User();
$u->get_by_id($userid);

$alarm = new Alarm();
$alarm->get_by_id($alarmid);

// mark this alarm as fired
$alarm->set_join_field($u, 'wasfired', TRUE);  // not $user
#4

[eluser]WanWizard[/eluser]
You're right, a typo. I'll correct it.
#5

[eluser]Unknown[/eluser]
Thanks, it works as expected.




Theme © iAndrew 2016 - Forum software by © MyBB