use Symfony\Component\HttpFoundation\JsonResponse;
$data = [
'List' => [
0 => [
'id' => 'id0',
],
1 => [
'id' => 'id1',
],
],
];
dump(
new JsonResponse(
json_encode($data, JsonResponse::DEFAULT_ENCODING_OPTIONS | JSON_FORCE_OBJECT),
200,
[],
true
)
);
// or
$response = new JsonResponse($data);
$response->setEncodingOptions(JsonResponse::DEFAULT_ENCODING_OPTIONS | JSON_FORCE_OBJECT);
dump($response->getContent());
use Pagerfanta\Pagerfanta;
class PaginatedCollection
{
private int $page;
private int $size;
private int $total;
private int $pages;
private array $items;
public function __construct(Pagerfanta $pagerfanta)
{
$this->page = $pagerfanta->getCurrentPage();
$this->size = $pagerfanta->getMaxPerPage();
$this->total = $pagerfanta->getNbResults();
$this->pages = $pagerfanta->getNbPages();
$this->items = iterator_to_array($pagerfanta);
}
public function getPage(): int
{
return $this->page;
}
public function getSize(): int
{
return $this->size;
}
public function getTotal(): int
{
return $this->total;
}
public function getPages(): int
{
return $this->pages;
}
public function getItems(): array
{
return $this->items;
}
}
// Repository
public function findPaginatedBySupplierId(int $supplier_id)
{
return new Pagerfanta(DoctrineORMAdapter($this->getSupplierIdQueryBuilder($supplier_id)));
}
private function getSupplierIdQueryBuilder(int $supplier_id)
{
$qb = $this->createQueryBuilder('sj');
$qb = $qb
->select("sj")
->orderBy('sj.datetime', 'ASC')
->andWhere("sj.supplier = :supplier_id")
->setParameter("supplier_id", $supplier_id);
return $qb;
}
// Controller
public function action()
{
// ...
return $this->json($this->paginatedCollection($repository->findPaginatedBySupplierId($id), $request));
}
// AbstractController
protected function paginatedCollection(Pagerfanta $pagination, Request $request, int $size = 20): PaginatedCollection
{
$pagination = $this->paginate($pagination, $request, $size);
return new PaginatedCollection($pagination);
}
protected function paginate(Pagerfanta $pagination, Request $request, int $size = 20): Pagerfanta
{
$pagination->setMaxPerPage($size);
$pagination->setCurrentPage($request->query->getInt('page', 1));
return $pagination;
}
{
"page": 1,
"size": 20,
"total": 76,
"pages": 4,
"items": [...]
}
/**
* @var \App\Entity\IpContract[]
*
* @ORM\OneToMany(targetEntity="App\Entity\IpContract", mappedBy="users", orphanRemoval=true)
*/
private $ipContracts;
return $this->json($user, 200, [], [
AbstractNormalizer::IGNORED_ATTRIBUTES => ['__initializer__', '__cloner__', '__isInitialized__'],
]);
// App\Kernel
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
{
// ...
$container->addCompilerPass(new class implements CompilerPassInterface {
public function process(ContainerBuilder $container)
{
$container->getDefinition('serializer.normalizer.object')->setArgument(6, [
AbstractNormalizer::IGNORED_ATTRIBUTES => ['__initializer__', '__cloner__', '__isInitialized__'],
]);
}
});
}
// App\Kernel
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
{
// ...
$container->addCompilerPass(new class implements CompilerPassInterface {
public function process(ContainerBuilder $container)
{
$container->getDefinition('oneup_flysystem.adapter.local')->setLazy(true);
}
});
}
Since every developer uses a different IDE, the recommended way to enable this feature is to configure it on a system level. This can be done by setting the xdebug.file_link_format option in your php.ini configuration file.
# services.yaml
imports:
- { resource: 'local.yaml', ignore_errors: true }
# local.yaml
framework:
ide: 'phpstorm://open?file=%%f&line=%%l'
# .gitignore
/config/local.yaml
// Этот класс в бандле
/** @MappedSuperclass */
class BaseAnimal
{
/** @Column(type="string") */
protected $name;
/** @Column(type="integer") */
protected $size;
/** @Column(type="boolean") */
protected $canFly;
}
// Этот класс в проекте
/** @Entity */
class Animal extends BaseAnimal
{
/** @Column(type="string") */
private $id;
/** @ManyToOne(targetEntity="Person") */
private $owner;
}
$validator = (new ValidatorBuilder())->getValidator();
$constraints = [
new Assert\Collection([
'fields' => [
'address' => [
new Assert\NotNull(),
new Assert\Type('array'),
],
],
'allowMissingFields' => true,
'allowExtraFields' => true,
]),
new Assert\Collection([
'fields' => [
'required' => [
new Assert\NotNull(),
],
],
'allowExtraFields' => true,
]),
];
$data = [];
dump($data);
dump($validator->validate($data, $constraints)); // Ko
$data = [
'address' => null,
];
dump($data);
dump($validator->validate($data, $constraints)); // Ko
$data = [
'address' => [],
];
dump($data);
dump($validator->validate($data, $constraints)); // Ko
$data = [
'address' => 'address',
];
dump($data);
dump($validator->validate($data, $constraints)); // Ko
$data = [
'address' => [],
'required' => 'value',
];
dump($data);
dump($validator->validate($data, $constraints)); // Ok
/** @var UserInterface $user */
/** @var RoleHierarchyInterface $hierarchy */
$roles = $hierarchy->getReachableRoleNames($user->getRoles());
$result = array_search('ROLE_GROUP_MANAGER', $roles) !== false;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\RouteCollectionBuilder;
require_once __DIR__.'/../vendor/autoload.php';
$locator = new FileLocator([__DIR__.'/../data']);
$loader = new YamlFileLoader($locator);
$builder = new RouteCollectionBuilder($loader);
$builder->import('dir1/routes.yaml');
$builder->import('dir2/routes.yaml');
$routes = $builder->build();
var_dump($routes);
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\DelegatingLoader;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\Routing\Loader\GlobFileLoader;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\RouteCollectionBuilder;
require_once __DIR__.'/../vendor/autoload.php';
$locator = new FileLocator([__DIR__.'/../data']);
$resolver = new LoaderResolver([
new GlobFileLoader($locator), // needs symfony/finder
new YamlFileLoader($locator),
]);
$loader = new DelegatingLoader($resolver);
$builder = new RouteCollectionBuilder($loader);
$builder->import('**/*/routes.yaml', '/', 'glob');
$routes = $builder->build();
var_dump($routes);
class Locale
{
/**
* @var string
*/
private $value;
public function __construct(string $value)
{
$this->value = $value;
}
public function __toString()
{
return $this->value;
}
}
class LocaleFactory
{
/**
* @var RequestStack
*/
private $requestStack;
/**
* @var string
*/
private $default;
public function __construct(RequestStack $requestStack, string $default)
{
$this->requestStack = $requestStack;
$this->default = $default;
}
public function create(): Locale
{
if ($request = $this->requestStack->getMasterRequest()) {
return new Locale($request->get('locale', $this->default));
}
return new Locale($this->default);
}
}
services:
App\Locale\Locale:
factory: ['@App\Locale\LocaleFactory', 'create']
public function add(Request $request, EntityManagerInterface $manager, Locale $lang)
$parameters = $matcher->match($request->getPathInfo());
$invoker = new Invoker\Invoker(null, $container);
$invoker->call($parameters['_controller'], $parameters);
$parameters = $matcher->match($request->getPathInfo());
list($class, $action) = $parameters['_controller'];
unset($parameters['_controller']);
$controller = new $class;
$controller->$action($parameters);
referencedColumnName: Name of the primary key identifier that is used for joining of this relation.
/**
* @OneToMany(targetEntity="Address", mappedBy="customer", cascade={"persist", "remove"})
* @JoinColumn(name="customer_id", referencedColumnName="customer_id")
*/
protected $addresses;
Address::$customer_id
- это primary key?/**
* @Entity
* @Table(name="`address`")
*/
class Address
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="IDENTITY")
*/
protected $address_id;
/**
* @Column(type="string")
*/
protected $firstname;
/**
* @ManyToOne(targetEntity="Customer", inversedBy="address")
* @JoinColumn(name="customer_id", referencedColumnName="customer_id")
*/
protected $customer;
}
/**
* @Entity(repositoryClass="\Core\Repositories\CustomerRepository")
* @Table(name="`customer`")
*/
class Customer
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="IDENTITY")
*/
protected $customer_id;
/**
* @OneToMany(targetEntity="Address", mappedBy="customer", cascade={"persist", "remove"})
*/
protected $addresses;
public function __construct()
{
$this->addresses = new ArrayCollection();
}
}
referencedColumnName: Name of the primary key identifier that is used for joining of this relation.
/**
* @OneToOne(targetEntity="\Core\Entities\Delivery\BoxberryTtn", mappedBy="ttn_boxberry", cascade={"persist", "remove"})
* @JoinColumn(name="order_code", referencedColumnName="id")
*/
protected $boxberry_ttn;
/**
* @Entity
* @Table(name="`ttn_boxberry`")
*/
class BoxberryTtn
{
/**
* @Id
* @Column(type="string")
* @GeneratedValue(strategy="NONE")
*/
protected $id;
public function __construct(string $id)
{
$this->id = $tid;
}
}
order_code
/**
* @Entity
* @Table(name="`order`")
*/
class Order
{
/**
* @Id
* @Column(type="integer")
*/
protected $order_code;
/**
* @OneToOne(targetEntity="\Core\Entities\Delivery\BoxberryTtn", mappedBy="ttn_boxberry", cascade={"persist", "remove"})
* @JoinColumn(name="order_code", referencedColumnName="order_code")
*/
protected $boxberry_ttn;
}
/**
* @Entity
* @Table(name="`order`")
*/
class Order
{
/**
* @Id
* @Column(type="integer")
*/
protected $order_id;
/**
* @Id
* @Column(type="integer")
*/
protected $order_code;
/**
* @Column(type="integer")
*/
protected $warehouse_id;
/**
* @OneToOne(targetEntity="\Core\Entities\Delivery\BoxberryTtn", cascade={"persist", "remove"})
* @JoinColumn(referencedColumnName="order_code")
*/
protected $boxberry_ttn;
public function __construct(int $order_id, string $order_code)
{
$this->order_id = $order_id;
$this->order_code = $order_code;
}
}
/**
* @Entity
* @Table(name="`ttn_boxberry`")
*/
class BoxberryTtn
{
/**
* @Id
* @Column(type="string")
* @GeneratedValue(strategy="NONE")
*/
protected $order_code;
/**
* @Column(type="string")
*/
protected $ttn_num;
/**
* @Column(type="float", scale=2)
*/
protected $delivery_cost;
public function __construct(Order $order)
{
$this->order_code = $order->getOrderCode();
}
}
public function __toString()
{
return $this->name;
}
\Symfony\Component\Form\Extension\Core\Type\ColorType
в котором, судя по всему, ваша сущность кастится в строку.\App\Form\ColorType
метод getBlockPrefix
и все заработает:public function getBlockPrefix()
{
return 'app_color';
}