Skip to content

Application Entities (Models)

sunrise-php edited this page Jan 7, 2019 · 1 revision

Entity for example

Do not forget to update the database after creating the model

declare(strict_types=1);
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="note")
 */
class Note extends AbstractEntity
{

    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     *
     * @var null|int
     */
    protected $id;

    /**
     * @ORM\Column(
     *   type="string",
     *   length=128,
     *   nullable=false
     * )
     *
     * @Assert\NotBlank
     * @Assert\Type("string")
     * @Assert\Length(max=128)
     *
     * @var null|string
     */
    protected $title;

    /**
     * @ORM\Column(
     *   type="text",
     *   nullable=false
     * )
     *
     * @Assert\NotBlank
     * @Assert\Type("string")
     *
     * @var null|string
     */
    protected $content;

    /**
     * Gets the note ID
     *
     * @return null|int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Gets the note title
     *
     * @return null|string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Gets the note content
     *
     * @return null|string
     */
    public function getContent()
    {
        return $this->content;
    }

    /**
     * Sets the note title
     *
     * @param string $title
     *
     * @return void
     */
    public function setTitle($title)
    {
        $this->title = $title;
    }

    /**
     * Sets the note content
     *
     * @param string $content
     *
     * @return void
     */
    public function setContent($content)
    {
        $this->content = $content;
    }
}

Controller for example

declare(strict_types=1);
namespace App\Http\Controller;

/**
 * Import classes
 */
use App\Entity\Note;
use Doctrine\ORM\EntityManager;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

/**
 * @Route(
 *   id="note.create",
 *   path="/note",
 *   methods={"POST"}
 * )
 */
class NoteCreateController implements MiddlewareInterface
{

    /**
     * @Inject
     *
     * @var EntityManager
     */
    protected $entityManager;

    /**
     * @param ServerRequestInterface $request
     * @param RequestHandlerInterface $handler
     *
     * @return ResponseInterface
     */
    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler) : ResponseInterface
    {
        $data = (array) $request->getParsedBody();
        $response = $handler->handle($request);

        $note = new Note();
        $note->setTitle($data['title'] ?? null);
        $note->setContent($data['content'] ?? null);

        $violations = $note->validate();
        if ($violations->count() > 0) {
            $response->getBody()->write((string) $violations);

            return $response->withStatus(400);
        }

        $this->entityManager->persist($note);
        $this->entityManager->flush();

        $response->getBody()->write(
            \sprintf("successful; note id: %d\n", $note->getId())
        );

        return $response->withStatus(201);
    }
}

Check that everything works

Update the database

composer db:update

Invalid request

Request

curl -d "title=&content=" -X POST http://0.0.0.0:8080/note

Response

Object(App\Entity\Note).title:
    This value should not be blank. (code c1051bb4-d103-4f74-8988-acbcafc7fdc3)
Object(App\Entity\Note).content:
    This value should not be blank. (code c1051bb4-d103-4f74-8988-acbcafc7fdc3)

Valid request (Content-Type: application/x-www-form-urlencoded)

Request

curl -d "title=foo&content=bar" -X POST http://0.0.0.0:8080/note

Response

successful; note id: 1

Valid request (Content-Type: application/json)

Request

curl -d '{"title": "baz", "content": "qux"}' -H "Content-Type: application/json" -X POST http://0.0.0.0:8080/note

Response

successful; note id: 2