Skip to content
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

Closed
danielwlockhart opened this issue Jun 17, 2016 · 4 comments
Closed

Spot2 Relations? #6

danielwlockhart opened this issue Jun 17, 2016 · 4 comments

Comments

@danielwlockhart
Copy link

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.

@tuupola
Copy link
Owner

tuupola commented Jun 20, 2016

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
        ];
    }
}

@danielwlockhart
Copy link
Author

That got it! Thanks!

@danielwlockhart
Copy link
Author

danielwlockhart commented Jun 22, 2016

Thanks again! One more question... What would be different about a
belongsTo relationship? I tried the same as above and it returned an empty
result...here is my Spot Entity class snippet for relations:

public static function relations(MapperInterface $mapper, EntityInterface
$entity)

{

    return [

        'university' => $mapper->belongsTo($entity, 'App\University',

'user_university_id'),

    ];

}

Here is my transformer snippet

protected $defaultIncludes = [
"university"
];

public function includeUniversity(User $user)
{
    $university = $user->university;
    return $this->collection($university, new UniversityTransformer);
}

I tried swapping it to $this->item because I thought that made more sense
but it throws an error that way saying that it expects an object of type
App/University and got an item of type Spot/Relation/BelongsTo

Sorry to be such a bother...I really appreciate the help!

@tuupola
Copy link
Owner

tuupola commented Jun 22, 2016

Not sure if it causes your problem but but belongsTo returns one object not collection.

public function includeUniversity(User $user)
{
    $university = $user->university;
    return $this->item($university, new UniversityTransformer);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants