Michael D Mon Jun 04 04:17:37 -0400 2012

Subject: Problem with ssociations

I want to get a users messages. What am I doing wrong? I get a huge fatal error.

Fatal error: Uncaught exception 'ActiveRecord\UndefinedPropertyException' with message 'Undefined property: Message->receivers

The messages table
id | sender_id | receiver_id | subject | message | sent
class Message extends ActiveRecord\Model 
    static $belongs_to = array(
        array('sender', 'class_name' => 'User', 'foreign_key' => 'sender_id'),
        array('receiver', 'class_name' => 'User', 'foreign_key' => 'receiver_id')

class User extends ActiveRecord\Model 
static $has_many = array(
        array('senders', 'class_name' => 'Message'),
        array('receivers', 'class_name' => 'Message')

Inbox controller

$this->view_data['message'] = Message::find_by_receiver_id($this->user_id);

Inbox view

<?php if($message->receivers): ?>
    <?php foreach($receiver->user as $message): ?>

        <p><?php echo $message->subject; ?></p>

    <?php endforeach; ?>
<?php else: ?>

    No messages!

<?php endif; ?>

Nanne Huiges Mon Jun 04 11:17:39 -0400 2012

You have defined a 'belongs_to' that is called reciever.
You ask for the property "$message->recievers" (with an s)
Then you get an error saying receivers does not exist :)

AKA; receivers does not exist, you might've mean receiver?