New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spot2 Relations? #6
Comments
You need to have also have transformer for your relations ie. in this case messages. Something like the following untested code. class ChatTransformer extends Fractal\TransformerAbstract
{
protected $availableIncludes = [
"messages"
];
public function includeMessages(Chat $chat)
{
$messages = $chat->messages;
return $this->collection($messages, new MessageTransformer);
}
public function transform(Chat $chat)
{
return [
"chat_event_id" => (integer)$chat->chat_event_id ?: 0,
"chat_expires_at" => (string)$chat->chat_expires_at ?: null
];
}
}
class ChatMessageTransformer extends Fractal\TransformerAbstract
{
public function transform(ChatMessage $message)
{
return [
"body" => (string)$message->body ?: null
];
}
} |
That got it! Thanks! |
Thanks again! One more question... What would be different about a public static function relations(MapperInterface $mapper, EntityInterface
'user_university_id'),
Here is my transformer snippet protected $defaultIncludes = [
I tried swapping it to $this->item because I thought that made more sense Sorry to be such a bother...I really appreciate the help! |
Not sure if it causes your problem but but public function includeUniversity(User $user)
{
$university = $user->university;
return $this->item($university, new UniversityTransformer);
} |
First, I would like to say how much I am loving this! I have used this to spin up like 3 APIs now in the blink of an eye. The latest one I am using requires me to have relationships built into my entities and I was wondering if that was possible with this setup? I did it just like it is in the Spot docs....here is my entity class:
namespace App;
use Spot\EntityInterface;
use Spot\MapperInterface;
use Spot\EventEmitter;
use Tuupola\Base62;
use Ramsey\Uuid\Uuid;
use Psr\Log\LogLevel;
class Chat extends \Spot\Entity
{
protected static $table = "chats";
public static function fields()
{
return [
"chat_id" => ["type" => "integer", "unsigned" => true, "primary" => true, "autoincrement" => true],
"chat_event_id" => ["type" => "integer", "unsigned" => true],
"chat_expires_at" => ["type" => "string", "length" => 100],
"chat_created_at" => ["type" => "datetime", "value" => new \DateTime()],
"chat_updated_at" => ["type" => "datetime", "value" => new \DateTime()]
];
}
public static function events(EventEmitter $emitter)
{
$emitter->on("beforeUpdate", function (EntityInterface $entity, MapperInterface $mapper) {
$entity->chat_updated_at = new \DateTime();
});
}
public static function relations(MapperInterface $mapper, EntityInterface $entity)
{
return [
'messages' => $mapper->hasMany($entity, 'App\ChatMessage', 'chat_message_chat_id')->order(['chat_message_created_at' => 'ASC']),
];
}
public function timestamp()
{
return $this->chat_updated_at->getTimestamp();
}
}
and here is my Transformer class for Fractal:
namespace App;
use App\Chat;
use League\Fractal;
class ChatTransformer extends Fractal\TransformerAbstract
{
public function transform(Chat $chat)
{
return [
"chat_event_id" => (integer)$chat->chat_event_id ?: 0,
"chat_expires_at" => (string)$chat->chat_expires_at ?: null,
"chat_messages" => $chat->messages
];
}
}
Everything else is pretty much setup like you do in your examples. When I run this, I only get a blank for the 'chat_messages' return even though I know there should be something in there so I was just wondering if I was missing something.
The text was updated successfully, but these errors were encountered: