I have a base class Message for a mailbox using a polymorphic relationship to attach custom message types that all implement the same interface and behave differently in looks based on their type. Displaying this all works smoothly, but I fell into the trap when I tried to add them with code.
This is the message class:
<?php class Message extends Eloquent { public function author() { $this->belongsTo("User", "author_id"); } public function recipient() { $this->belongsTo("User", "recipient_id"); } public function message() { $this->morphTo(); } }
The model that I attach to message() implements MessageInterface , so I thought I could make a quick helper to bind this model relation through Message::send() :
public static function send(MessageInterface $message, User $to, User $from) { if (! $message->exists) $message->save(); $parent = new static; $parent->author()->associate($from); $parent->recipient()->associate($to); $parent->message()->associate($message);
But it ends up throwing endless recursion on me:
FatalErrorException: Maximum function nesting level of '100' reached, aborting!
This is a studly function, and from some search, this happens when two models refer to each other.
Schema for the message table:
$table->increments("id"); $table->integer("message_id")->unsigned(); $table->string("message_type"); $table->integer("recipient_id")->unsigned(); $table->integer("author_id")->unsigned(); $table->timestamps();
Am I just doing something really wrong here? I looked at the morphTo method morphTo in the source and tried to see if there is a problem with reflection (capturing the function name and snake shell this), but I can not find what is happening. Invoking the associate method is simply setting the attributes and getting the class name for message_type , and then returning the relationship.
There is no useful information in the error; this is a Symfony\Component\Debug\Exception\FatalErrorException with no context.
I am running Laravel 4.1