CodeIgniter Forums
DataMapper ORM v1.8.0 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: DataMapper ORM v1.8.0 (/showthread.php?tid=37531)



DataMapper ORM v1.8.0 - El Forum - 05-03-2011

[eluser]WanWizard[/eluser]
That is not going to work, it is a not supported configuration.

Code:
$u->save($b);
means "save $u, and save the relation between $u and $b". That statement is not aware of anything else. Other fields in the relationship record are considered join fields, and need to be set afterwards.

And if you would save one relation first, and then the second, it would simply generate new records, in which one of the other id's would be NULL.

What you can do is use set_join_field to set the missing id manually:
Code:
$category->save($element->all);
$category->set_join_field($element->all, 'island_id', $island->id);



DataMapper ORM v1.8.0 - El Forum - 05-03-2011

[eluser]Atas[/eluser]
@WanWizard thankyou for your answer. I'll try your suggestions and let you know the results.


DataMapper ORM v1.8.0 - El Forum - 05-03-2011

[eluser]nomie7[/eluser]
I have a table ...

Code:
CREATE TABLE IF NOT EXISTS `messages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `to` int(11) NOT NULL,
  `from` int(11) NOT NULL,
  `subject` varchar(50) NOT NULL,
  `message` varchar(1000) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
To and From is the id(pk) from Users Table.

How can I get the user details when I get records from this table with DataMapper ORM.

I tried following the examples from advanced relations from the manual but no luck.


DataMapper ORM v1.8.0 - El Forum - 05-04-2011

[eluser]WanWizard[/eluser]
That won't work, as Datamapper requires you do define keys with an '_id' suffix (as stated in the manual):
Code:
CREATE TABLE IF NOT EXISTS `messages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `to_id` int(11) NOT NULL,
  `from_id` int(11) NOT NULL,
  `subject` varchar(50) NOT NULL,
  `message` varchar(1000) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

You can then define the Message model as
Code:
class Message extends DataMapper {
    $has_one = array(
        'from' => array(
            'class' => 'user',
            'other_field' => 'from'
        ),
        'to' => array(
            'class' => 'user',
            'other_field' => 'to'
        )
    );
}

class User extends DataMapper {
    $has_many = array(
        'from' => array(
            'class' => 'message',
            'other_field' => 'from'
        ),
        'to' => array(
            'class' => 'message',
            'other_field' => 'to'
        )
    );
}

You can then retrieve data using
Code:
$user = new User();
$message = new Message();

// get all messages send by user 1
$user->get_by_id(1)->from->get();

// get a message and it's from and to info
$message->include_related('from')->include_related('to')->get_by_id(1);



DataMapper ORM v1.8.0 - El Forum - 05-04-2011

[eluser]nomie7[/eluser]
Tried and not working try figure out an alternative solution.


DataMapper ORM v1.8.0 - El Forum - 05-04-2011

[eluser]WanWizard[/eluser]
What doesn't work? Any error messages? Wrong output? Others?

It's difficult to help if you don't provide me with any information (and I'm not in a position to try things myself, I have to work from memory as my silicon brain has crashed and Dell is slow to fix it).


DataMapper ORM v1.8.0 - El Forum - 05-04-2011

[eluser]nomie7[/eluser]
Oh sorry I was in a hurry leaving for work. It does not populate the information from the users table. I can grab information from the messages table but none from users. I am using ion_auth users's table if that helps.


DataMapper ORM v1.8.0 - El Forum - 05-04-2011

[eluser]WanWizard[/eluser]
I can setup some tests here, but that will have to wait until my laptop is back from the dead. I'm now limited to using a Blackberry...


DataMapper ORM v1.8.0 - El Forum - 05-04-2011

[eluser]nomie7[/eluser]
Doest this help?
Code:
SELECT messages.subject, messages.message, users.username FROM messages INNER JOIN users ON messages.from_id=users.id WHERE messages.to_id=1

With that query i can grab information from the both tables. But i am trying to avoid writing my own queries and stick to datamapper.


DataMapper ORM v1.8.0 - El Forum - 05-04-2011

[eluser]WanWizard[/eluser]
Well, that's the query Datamapper generates too, if the relationship is defined correctly.