PHP code example of pportelette / crud-bundle

1. Go to this page and download the library: Download pportelette/crud-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pportelette / crud-bundle example snippets


// config/bundles.php

return [
    Pportelette\CrudBundle\PporteletteCrudBundle::class => ['all' => true],
];

namespace App\Entity;

use App\Repository\CategoryRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: CategoryRepository::class)]
class Category
{ 
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: "integer")]
    private $id;

    #[ORM\Column(type: "string", length: 100)]
    private $name;

    #[ORM\Column(type: "datetime_immutable")]
    private $created_at;

    /*
        GETTERS AND SETTERS
    */
}

namespace App\Model;

use Pportelette\CrudBundle\Model\ViewModel;
use App\Entity\Category;

class CategoryVM extends ViewModel {
    public $id;
    public $name;
    private $createdAt;

    public function fromEntity(Category $category = null): void {
        if(!$category) {
            return;
        }
        $this->id = $category->getId();
        $this->name = $category->getName();
        $this->createdAt = $category->getCreatedAt();
    }

    public function toEntity(Category $category = new Category()): Category {
        $category->setName($this->name);
        $category->setCreatedAt($this->createdAt);

        return $category;
    }

    /**
     * Customize the response for the GET /category endpoint
     * Optional
     */
    public function getAll(): array {
        return [
            'id' => $this->id,
            'name' => $this->name
        ];
    }

    /**
     * Customize the response for the GET /category/list endpoint
     * Optional
     */
    public function getList(): array {
        return [
            'name' => $this->name,
        ];
    }
}

namespace App\Repository;

// ...
use Pportelette\CrudBundle\Repository\CrudRepository;

class CategoryRepository extends CrudRepository
{
    // ...
}

namespace App\Controller;

// ...
use Pportelette\CrudBundle\Controller\CrudController;

class WordController extends CrudController
{
    public function __construct(SerializerInterface $serializer, CategoryRepository $repository)
    {
        $this->configure(
            $serializer,
            $repository, 
            CategoryVM::class
        );
    }
}

public function getAll(int $page, array $filters = []): Pageable;
public function getList(array $filters = []): array;
public function getEntity(int $id): ViewModel;
public function createEntity(array $properties): ViewModel;
public function updateEntity(int $id, array $properties): ViewModel;
public function deleteEntity(int $id): void;

// src/Service/CategoryService.php
// ...
use Pportelette\CrudBundle\Service\CrudService;
use Pportelette\PageableBundle\Model\Pageable;

class WordService extends CrudService
{
    public function __construct(CategoryRepository $wordRepository)
    {
        parent::__construct($wordRepository, CategoryVM::class);
    }

    public function getAll(int $page, array $params = []): Pageable
    {    
        // Your custom code
    }
}

public function getAll(int $page, array $filters = []): Pageable;
public function getList(array $filters = []): array;

// src/Repository/CategoryRepository.php

// ...

public function getAll(int $page, array $filters = []): Pageable
{
    $qb = $this->createQueryBuilder('w');

    // Your custom code

    return $this->getPage(
        $qb,
        $page
    );
}