DataMapper ORM for Codeigniter Relations

I have a table ...

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 are the primary key id from the user table.

How can I get user information when I receive each message using CodeIgniter DataMapper.

+4
source share
3 answers

You are missing a couple of key points to use DataMapper for CodeIgniter. First of all, you need to do some simple and important things. DataMapper (DM) uses the normalized db name to search for relationships. This means that if you request your dB. Now it’s a little harder to use DM for two columns, and I think you really don't need this.

First, if you are not using DM, you really only need two queries

 SELECT u.*, m.* FROM messages AS m, users AS u WHERE m.from = u.id AND m.id = SOME_ID. 

This request will provide you with all user information and message information for some message identifier. Now this is a semi-simple case, because I assume that a message can only be from one user.

For a field, on the other hand, I assume you should use a relational table. To use a DM for this, you should call the table something like users_messages , but again why do you need to use the DM when it is really full.

Now for the from field, you have a many, many relationship, because a message can have many users to whom it is intended, and a user can have many messages that they sent.

So create a table like this

 CREATE TABLE message_to ( user_id BIGINT UNSIGNED NOT NULL, message_to_id BIGING UNSIGNED NOT NULL, PRIMARY KEY (user_id, message_to_id), ); 

If you want to do it right, you will also use foreign keys, but it depends on your DB

Now you can request very easily and get all the users to whom the message was sent.

 SELECT u.*, m.* FROM users AS u, m AS messages JOIN messages_to AS m_t ON (u.id = m_t.user_id) 

And requesting another way is just as easy to get all the messages sent by the user.

Remember only that a tool like DM does not mean that it is the best tool for work and actually uses DM, in this case it incurs rather decent overhead when it is not necessary.

Doing this with DM will require the same thing that you simply cannot call your tables / columns as you see fit, and you need a class for each table, creating it with other tables.

Bearing in mind that you have a lot of extra work to use DM, you need to study their syntax.

+2
source

What you are looking for is independence.

If this has_one relationship, you can do this using foreign keys in the table. You must follow the naming rules for the keys (to_id and from_id instead of and from).

Currently (v1.8.0) you can have only one relationship between any two models:

 $has_one = array( 'to' => array( 'class' => 'messages', 'other_field' => 'messages' ), 'messages' => array( 'other_field' => 'to' ) ); 

}

See http://datamapper.wanwizard.eu/pages/advancedrelations.html for more details.

+2
source
  • You should make your models [models / users.php] and [models / messages.php] as follows:

  class User extends DataMapper { var $has_many = array( 'sent_message' => array( 'class' => 'Message', 'other_field' => 'sender', ), 'received_message' => array( 'class' => 'Message', 'other_field' => 'receiver', ), ); } 

 class Message extends Datamapper { var $has_one = array( 'sender' => array( 'class' => 'User', 'other_field' => 'sent_message', ), 'receiver' => array( 'class' => 'User', 'other_field' => 'received_message' ), ); } 

I only have $ has_one and $ has_many, and you must include the rest of the models.


  • you need to use your tables as follows:

Table name: user fields: id, email, ....

Table name: message fields [this is an important table in this case]: id, sender_id, receiver_id, subject, message, created, ....


Now you need to populate your database with message examples, and then you can test as follows:

for example, user X logs in and is now an object. You receive the last 10 messages:

  $messages = new Message(); $messages->where_related_user('id', $user->id); $messages->limit(10); $messages->get(); 

You can get the recipient and sender of each message as follows:

 $messages = new Message(); $messages->include_related('sender'); $messages->include_related('receiver'); $messages->get(); 

Now type the name of each sender and recipient:

 foreach($messages as $message): echo $message->sender->name; echo $message->receiver->name; endforeach; 
0
source

All Articles