Use select x from y where z in symfony2 (doctrine) - sql

So I have little problem, I don't know how I can use:
SELECT * FROM product WHERE nazwa = 'Sprite'
in Symfony. Here is my file from "Entity":
<?php
namespace My\MainBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping AS ORM;
/**
* #ORM\Entity
* #ORM\Table(name="product")
*/
class Product
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="integer", length=10)
*/
protected $cena;
/**
* #ORM\Column(type="integer", length=10)
*/
protected $ilosc;
/**
* #ORM\Column(type="string", length=50)
*/
protected $nazwa;
public function getCena()
{
return $this->cena;
}
public function setCena($cena)
{
$this->cena = $cena;
}
public function getIlosc()
{
return $this->ilosc;
}
public function setIlosc($ilosc)
{
$this->ilosc = $ilosc;
}
public function getNazwa()
{
return $this->nazwa;
}
public function setNazwa($nazwa)
{
$this->nazwa = $nazwa;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
I tried something like this:
$repository = $this->getDoctrine()->getRepository('MainBundle:Product');
$query = $repository->createQueryBuilder('p')->select('p')->where('p.nazwa == Sprite')->getQuery();
$test = $query->getResult();
But when I try to use it, I got an error. Somebody have idea what can be wrong?

$repository = $this->getDoctrine()->getRepository('MainBundle:Product');
$result = $repository->findByNazwa('Sprite');
or with QueryBuilder
$query = $repository->createQueryBuilder('p')->select('p')->where('p.nazwa = :nazwa')->setParameter('nazwa', 'Sprite')->getQuery();
$test = $query->getResult();

Try this:
$repository = $this->getDoctrine()->getRepository('MainBundle:Product');
$query = $repository->createQueryBuilder('p');
$query->where('p.nazwa = :brand')
->setParameter('brand', 'Sprite');
$test = $query->getQuery()->getResult();

Related

Doctrine custom entity repository (QueryBuilder)

Howe create custom repository QueryBuilder for Room entity, witch will return all available rooms between two dates. I have two Entities Room.php and Reservation.php In RoomRepository.php i create function findAvailableRooms($from, $to) Hove i create query builder who return all rooms who do not have a reservation in the selected period? Tnx Room.php
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\AvailableRoom;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Bridge\Doctrine\IdGenerator\UuidV4Generator;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Uid\Uuid;
use App\Repository\RoomRepository;
use Doctrine\ORM\Mapping as ORM;
use App\Controller\AvailableRoomController;
/**
* #ApiResource(
* normalizationContext={"groups"={"room:read"}},
* denormalizationContext={"groups"={"room:write"}},
* attributes={
* "pagination_client_items_per_page"=true
* },
* collectionOperations={
* "get",
* "post",
* "get_available"={
* "method"="GET",
* "path"="/rooms/available/{from}/{to}",
* "controller"=AvailableRoomController::class
* }
* }
* )
* #ORM\Entity(repositoryClass=RoomRepository::class)
*/
class Room
{
/**
* #ORM\Id
* #ORM\Column(type="uuid", unique=true)
* #ORM\GeneratedValue(strategy="CUSTOM")
* #ORM\CustomIdGenerator(class=UuidV4Generator::class)
* #Groups({"room:read"})
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
* #Groups({"room:read", "room:write", "reservation:read"})
*/
private $name;
/**
* #ORM\Column(type="string", length=255)
* #Gedmo\Slug(fields={"name"})
* #Groups({"room:read", "room:write"})
*/
private $slug;
/**
* #ORM\Column(type="text", nullable=true)
* #Groups({"room:read", "room:write"})
*/
private $description;
/**
* #ORM\Column(type="datetime")
* #Gedmo\Timestampable(on="create")
* #Groups({"room:read", "room:write"})
*/
private $createdAt;
/**
* #ORM\Column(type="datetime")
* #Gedmo\Timestampable(on="update")
* #Groups({"room:read", "room:write"})
*/
private $updatedAt;
/**
* #ORM\OneToMany(targetEntity=Reservation::class, mappedBy="room")
* #Groups({"room:read", "room:write"})
*/
private $reservations;
public function __construct()
{
$this->reservations = new ArrayCollection();
}
public function getId(): ?Uuid
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* #return Collection|Reservation[]
*/
public function getReservations(): Collection
{
return $this->reservations;
}
public function addReservation(Reservation $reservation): self
{
if (!$this->reservations->contains($reservation)) {
$this->reservations[] = $reservation;
$reservation->setRoom($this);
}
return $this;
}
public function removeReservation(Reservation $reservation): self
{
if ($this->reservations->removeElement($reservation)) {
// set the owning side to null (unless already changed)
if ($reservation->getRoom() === $this) {
$reservation->setRoom(null);
}
}
return $this;
}
}
Reservation.php
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
use App\Repository\ReservationRepository;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Bridge\Doctrine\IdGenerator\UuidV4Generator;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Uid\Uuid;
/**
* #ApiResource(
* normalizationContext={"groups"={"reservation:read"}},
* denormalizationContext={"groups"={"reservation:write"}},
* attributes={
* "pagination_client_items_per_page"=true
* }
* )
* #ApiFilter(DateFilter::class, properties={"dateFrom"})
* #ORM\Entity(repositoryClass=ReservationRepository::class)
*/
class Reservation
{
/**
* #ORM\Id
* #ORM\Column(type="uuid", unique=true)
* #ORM\GeneratedValue(strategy="CUSTOM")
* #ORM\CustomIdGenerator(class=UuidV4Generator::class)
* #Groups({"reservation:read"})
*/
private $id;
/**
* #ORM\Column(type="date")
* #Groups({"reservation:read", "reservation:write", "room:read"})
*/
private $dateFrom;
/**
* #ORM\Column(type="date")
* #Groups({"reservation:read", "reservation:write", "room:read"})
*/
private $dateTo;
/**
* #ORM\Column(type="json", nullable=true)
* #Groups({"reservation:read", "reservation:write", "room:read"})
*/
private $meta = [];
/**
* #ORM\ManyToOne(targetEntity=Room::class, inversedBy="reservations")
* #Groups({"reservation:read", "reservation:write"})
*/
private $room;
/**
* #ORM\Column(type="datetime")
* #Gedmo\Timestampable(on="create")
* #Groups({"reservation:read", "reservation:write", "room:read"})
*/
private $createdAt;
/**
* #ORM\Column(type="datetime")
* #Gedmo\Timestampable(on="update")
* #Groups({"reservation:read", "reservation:write", "room:read"})
*/
private $updatedAt;
public function getId(): ?Uuid
{
return $this->id;
}
public function getDateFrom(): ?\DateTimeInterface
{
return $this->dateFrom;
}
public function setDateFrom(\DateTimeInterface $dateFrom): self
{
$this->dateFrom = $dateFrom;
return $this;
}
public function getDateTo(): ?\DateTimeInterface
{
return $this->dateTo;
}
public function setDateTo(\DateTimeInterface $dateTo): self
{
$this->dateTo = $dateTo;
return $this;
}
public function getMeta(): ?array
{
return $this->meta;
}
public function setMeta(?array $meta): self
{
$this->meta = $meta;
return $this;
}
public function getRoom(): ?Room
{
return $this->room;
}
public function setRoom(?Room $room): self
{
$this->room = $room;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
}
Something like the following method placed in your ReservationRepository will fetch the ids of Rooms having a Reservation overlapping a specified date range:
/**
* #param \DateTimeInterface $start
* #param \DateTimeInterface $end
*
* #return array
*/
public function findRoomIdsReservedInDateRange(\DateTimeInterface $start, \DateTimeInterface $end)
{
$result = $this
->createQueryBuilder('r')
->select('r.room.id')
->andWhere('r.dateFrom < :end')
->andWhere('r.dateTo > :start')
->setParameter('start', $start)
->setParameter('end', $end)
->orderBy('r.room.id', 'ASC')
->getQuery()
->getScalarResult()
; // [['id' => 1], ['id' => 2], ['id' => 2], ['id' => 5]]
return array_unique(array_column($result, 'id')); // [1, 2, 5]
}
Then you could call that method from your RoomRepository
$exclude = $reservationRepository->findRoomIdsReservedInDateRange($start, $end);
return $this
->createQueryBuilder('room')
->andWhere('room.id not in :array')
->setParameter('array', $exclude)
->getQuery()
->getResult()
;

Syntax Error - line 0, col 122: Error: Expected end of string, got 'ON'

I'm trying to use ON in my Query Builder but it returns [Syntax Error] line 0, col 122: Error: Expected end of string, got 'ON'.
Code:
public function filterChamados(Request $request)
{
$em = $this->getDoctrine()->getManager()->getRepository(Chamados::class)
->createQueryBuilder('c')->select('c.id, d.name_fantasy, c.status, c.titulo, c.description')
->join(Clients::class, 'd',Join::ON,'c.id_client = d.id');
if ($request->request->get('status')) {
$em->where('c.status = :status')
->setParameter('status', $request->request->get('status'));
};
if (strtoupper(trim($request->get('client')))) {
$em->andWhere('(d.name_fantasy=:client OR d.razao_social=:client)')
->setParameter('client', strtoupper(trim($request->get('client'))));
};
if ($request->get('open_date')) {
$em->andWhere('c.open_date >=:open_date')
->setParameter('open_date', $request->get('open_date'));
}
if ($request->get('close_date')) {
$em->andWhere('c.close_date <=:close_date')
->setParameter('close_date', $request->get('close_date'));
}
$em->getQuery()->getArrayResult();
return new JsonResponse($em);
}
If I return its DQL, I get:
SELECT c.id, d.name_fantasy, c.status, c.titulo, c.description FROM App\Entity\Chamados c INNER JOIN App\Entity\Clients d ON c.id_client = d.id WHERE (d.name_fantasy=:client OR d.razao_social=:client)
If I run the SQL directly into PGAdmin, it works.
If I change ON to WITH, it does not return errors, but the result comes empty. Plus, I can't run its SQL directly into PGAdmin.
What am I doing wrong?
EDIT:
This is my raw SQL (considering I'm using all fields):
SELECT
c.id, d.name_fantasy, c. status, c.titulo, c.description
FROM
chamados c
JOIN
clients d
ON
c.id_client_id = d.id
WHERE
c.status = 2 --:status
AND
(d.name_fantasy = 'FARMÁCIA ALGUMA COISA' OR d.razao_social = 'FARMÁCIA ALGUMA COISA') -- :client
AND
c.open_date >= '2019-03-03 10:00' --:open_date
AND
c.close_date <= '2019-09-03 18:00' --:close_date
Entity Chamados:
/**
* #ORM\Entity(repositoryClass="App\Repository\ChamadosRepository")
*/
class Chamados
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string")
*/
private $titulo;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Clients", inversedBy="chamados")
* #ORM\JoinColumn(nullable=false)
*/
private $id_client;
/**
* #ORM\Column(type="integer", options={"default" = 0})
*/
private $status;
/**
* #ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* #ORM\ManyToMany(targetEntity="App\Entity\User", inversedBy="chamados")
*/
private $user;
/**
* #ORM\Column(type="datetime")
*/
private $open_date;
/**
* #ORM\Column(type="datetime", nullable=true)
*/
private $update_date;
/**
* #ORM\Column(type="datetime", nullable=true)
*/
private $close_date;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Tramite", mappedBy="chamado")
*/
private $tramites;
public function __construct()
{
$this->user = new ArrayCollection();
$this->tramites = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getIdClient(): ?Clients
{
return $this->id_client;
}
public function setIdClient(?Clients $id_client): self
{
$this->id_client = $id_client;
return $this;
}
public function getStatus(): ?int
{
return $this->status;
}
public function setStatus(int $status): self
{
$this->status = $status;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
/**
* #return Collection|User[]
*/
public function getUser(): Collection
{
return $this->user;
}
public function addUser(User $user): self
{
if (!$this->user->contains($user)) {
$this->user[] = $user;
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->user->contains($user)) {
$this->user->removeElement($user);
}
return $this;
}
public function getOpenDate(): ?DateTimeInterface
{
return $this->open_date;
}
public function setOpenDate(DateTimeInterface $open_date): self
{
$this->open_date = $open_date;
return $this;
}
public function getUpdateDate(): ?DateTimeInterface
{
return $this->update_date;
}
public function setUpdateDate(?DateTimeInterface $update_date): self
{
$this->update_date = $update_date;
return $this;
}
public function getCloseDate(): ?DateTimeInterface
{
return $this->close_date;
}
public function setCloseDate(?DateTimeInterface $close_date): self
{
$this->close_date = $close_date;
return $this;
}
/**
* #return mixed
*/
public function getTitulo()
{
return $this->titulo;
}
/**
* #param mixed $titulo
* #return Chamados
*/
public function setTitulo($titulo)
{
$this->titulo = $titulo;
return $this;
}
/**
* #return Collection|Tramite[]
*/
public function getTramites(): Collection
{
return $this->tramites;
}
public function addTramite(Tramite $tramite): self
{
if (!$this->tramites->contains($tramite)) {
$this->tramites[] = $tramite;
$tramite->setChamado($this);
}
return $this;
}
public function removeTramite(Tramite $tramite): self
{
if ($this->tramites->contains($tramite)) {
$this->tramites->removeElement($tramite);
// set the owning side to null (unless already changed)
if ($tramite->getChamado() === $this) {
$tramite->setChamado(null);
}
}
return $this;
}
}
Entity Clients:
/**
* #ORM\Entity(repositoryClass="App\Repository\ClientsRepository")
*/
class Clients
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $name_fantasy;
/**
* #ORM\Column(type="string", length=255)
*/
private $razao_social;
/**
* #ORM\Column(type="string", length=128, nullable=true)
*/
private $contact_email;
/**
* #ORM\Column(type="string", length=16, nullable=true)
*/
private $contact_telephone;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Chamados", mappedBy="id_client")
*/
private $chamados;
/**
* #ORM\Column(type="boolean", options={"default"="true"})
*/
private $active;
public function __construct()
{
$this->chamados = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getNameFantasy(): ?string
{
return $this->name_fantasy;
}
public function setNameFantasy(string $name_fantasy): self
{
$this->name_fantasy = mb_convert_case($name_fantasy, MB_CASE_UPPER, 'UTF-8');
return $this;
}
public function getRazaoSocial(): ?string
{
return $this->razao_social;
}
public function setRazaoSocial(string $razao_social): self
{
$this->razao_social = mb_convert_case($razao_social, MB_CASE_UPPER, 'UTF-8');
return $this;
}
public function getContactEmail(): ?string
{
return $this->contact_email;
}
public function setContactEmail(?string $contact_email): self
{
$this->contact_email = $contact_email;
return $this;
}
public function getContactTelephone(): ?string
{
return $this->contact_telephone;
}
public function setContactTelephone(?string $contact_telephone): self
{
$this->contact_telephone = $contact_telephone;
return $this;
}
/**
* #return Collection|Chamados[]
*/
public function getChamados(): Collection
{
return $this->chamados;
}
public function addChamado(Chamados $chamado): self
{
if (!$this->chamados->contains($chamado)) {
$this->chamados[] = $chamado;
$chamado->setIdClient($this);
}
return $this;
}
public function removeChamado(Chamados $chamado): self
{
if ($this->chamados->contains($chamado)) {
$this->chamados->removeElement($chamado);
// set the owning side to null (unless already changed)
if ($chamado->getIdClient() === $this) {
$chamado->setIdClient(null);
}
}
return $this;
}
/**
* #return mixed
*/
public function getActive()
{
return $this->active;
}
/**
* #param mixed $active
* #return Clients
*/
public function setActive($active)
{
$this->active = $active;
return $this;
}
}
Edit 2:
The error is gone and the query is being built properly, but the result is empty.
Controller:
public function filterChamados(Request $request)
{
$em = $this->getDoctrine()->getManager()->getRepository(Chamados::class)
->createQueryBuilder('c')->select('c.id, d.name_fantasy, c.status, c.titulo, c.description')
->join('c.id_client', 'd');
if ($request->request->get('status')) {
$em->where('c.status = :status')
->setParameter('status', $request->request->get('status'));
}
if (strtoupper(trim($request->get('client')))) {
$em->andWhere('(d.name_fantasy=:client OR d.razao_social=:client)')
->setParameter('client', strtoupper(trim($request->get('client'))));
}
if ($request->get('open_date')) {
$em->andWhere('c.open_date >=:open_date')
->setParameter('open_date', $request->get('open_date'));
}
if ($request->get('close_date')) {
$em->andWhere('c.close_date <=:close_date')
->setParameter('close_date', $request->get('close_date'));
}
$em->getQuery()->getArrayResult();
return new JsonResponse($em);
}
Generated query:
[2019-08-21 17:22:31] doctrine.DEBUG: SELECT c0_.id AS id_0, c1_.name_fantasy AS name_fantasy_1, c0_.status AS status_2, c0_.titulo AS titulo_3, c0_.description AS description_4 FROM chamados c0_ INNER JOIN clients c1_ ON c0_.id_client_id = c1_.id WHERE (c1_.name_fantasy = ? OR c1_.razao_social = ?) ["PADARIA","PADARIA"] []
PgAdmin3: If I put both values inside the query and replace " by ', it works, otherwise it returns column "PADARIA" does not exist.
Inside AJAX request: it returns an empty JSON.
For some reason you have a semicolon where it should not be, try the following function, not saying it will work though:
function filterChamados(Request $request)
{
$em = $this->getDoctrine()->getManager()->getRepository(Chamados::class)
->createQueryBuilder('c')->select('c.id, d.name_fantasy, c.status, c.titulo, c.description')
->join('c.Clients', 'd', Join::ON, 'c.id_client = d.id');
if ($request->request->get('status')) {
$em->where('c.status = :status')
->setParameter('status', $request->request->get('status'));
}
if (strtoupper(trim($request->get('client')))) {
$em->andWhere('(d.name_fantasy=:client OR d.razao_social=:client)')
->setParameter('client', strtoupper(trim($request->get('client'))));
}
if ($request->get('open_date')) {
$em->andWhere('c.open_date >=:open_date')
->setParameter('open_date', $request->get('open_date'));
}
if ($request->get('close_date')) {
$em->andWhere('c.close_date <=:close_date')
->setParameter('close_date', $request->get('close_date'));
}
$em->getQuery()->getArrayResult();
return new JsonResponse($em);
}
Notice how I have removed the semicolon from the following pieces of code:
if ($request->request->get('status')) {
$em->where('c.status = :status')
->setParameter('status', $request->request->get('status'));
};
if (strtoupper(trim($request->get('client')))) {
$em->andWhere('(d.name_fantasy=:client OR d.razao_social=:client)')
->setParameter('client', strtoupper(trim($request->get('client'))));
};
Also notice how I have change the JOIN:
->join('c.Clients', 'd', Join::ON, 'c.id_client = d.id');
UPDATE:
Change this:
if(strtoupper(trim($request->get('client')))) {
$client = strtoupper(trim($request->get('client')));
$em->andWhere('d.name_fantasy=:client')
->orWhere('d.razao_social=:client')
->setParameter('client', $client);
}
Or:
if (strtoupper(trim($request->get('client')))) {
$em->andWhere('d.name_fantasy=:client OR d.razao_social=:client')
->setParameter('client', strtoupper(trim($request->get('client'))));
};
More info here.

ORM ManyToOne Unidirectional fetchAll Object array does not render in phtml Zend Framework 3

The application has built in ZF3. I have to entity with ManyToOne relationship using ORM. The issue is when i render through controller and and if fetch data via index it gives okay result but when i assign that to view and trying to render at phtml it throws an error/
/**
* Subscriptions
*
* #ORM\Table(name="subscriptions", indexes={#ORM\Index(name="CUST_ID", columns={"customer_id"}),#ORM\Index(name="SUB_TYPE_ID", columns={"subscription_type_id"})})
* #ORM\Entity
*/
class Subscriptions
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \Application\Entity\SubscriptionType
*
* #ORM\ManyToOne(targetEntity="Application\Entity\SubscriptionType")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="subscription_type_id", referencedColumnName="id")
* })
*/
protected $subscriptionType;
//set
public function setSubscriptionType(\Application\Entity\SubscriptionType $subscriptionType = null)
{
$this->subscriptionType = $subscriptionType;
return $this;
}
//get
public function getSubscriptionType(){
return $this->subscriptionType;
}
//other setter and getter....
}
Another Class
/**
* SubscriptionType
*
* #ORM\Table(name="subscription_type")
* #ORM\Entity
*/
class SubscriptionType
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="description", type="text", length=65535, nullable=true)
*/
private $description;
/**
* Get id
*enter code here
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set description
*
* #param string $description
*
* #return SubscriptionType
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
}
Now at controller i have wrote...
//some initiations
class AdminController extends AbstractActionController
{
//other initiations including __construct....
public function usersubscriptionsAction(){
$this->subscriptions = $this->entityManager->getRepository(Subscriptions::class)->findAll();
/*
foreach($this->subscriptions as $subscription){
//if i am checking with this it gives proper output
echo $subscription->getSubscriptionType()->getDescription();
die();
}
*/
return new ViewModel(
array(
"subscriptions" => $this->subscriptions
)
);
}
}
///i have phtml file
<?php foreach ($subscriptions as $subscription): ?>
//below line throwing an error
<?php echo $subscription->getSubscriptionType()->getDescription(); ?>
<?php endforeach; ?>
when i run it throw an erro message
Call to a member function getDescription() on null
You've got the relation mapping incorrect. Corrected the property annotation below:
/**
* #var \Application\Entity\SubscriptionType
*
* #ORM\ManyToOne(targetEntity="Application\Entity\SubscriptionType")
* #ORM\JoinColumn(name="subscription_type_id", referencedColumnName="id")
*/
protected $subscriptionType;
You had
/**
* #var \Application\Entity\SubscriptionType
*
* #ORM\ManyToOne(targetEntity="Application\Entity\SubscriptionType")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="subscription_type_id", referencedColumnName="id")
* })
*/
protected $subscriptionType;
The #ORM\JoinColumns is used for when you have a join with multiple columns, please see the docs. Though, the most common usage of JoinColumns is for a JoinTable.

Type conversion error from LifecycleEventArgs

I have no idea for this. I'm a beginner of symfony3.
code
/**
* Set createdAt
* #ORM\PrePersist()
* #param \DateTime $createdAt
*
* #return Book
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updateAt
*
* #param \DateTime $updateAt
*
* #return Book
*/
public function setUpdateAt($updateAt)
{
$this->updateAt = $updateAt;
return $this;
}
/**
* Get updateAt
*
* #return \DateTime
*/
public function getUpdateAt()
{
return $this->updateAt;
}
/**
* #ORM\PrePersist()
*/
public function PrePersist() {
if ($this->getCreatedAt() == null) {
$this->setCreatedAt(new \DateTime('now'));
}
$this->setUpdateAt(new \DateTime('now'));
}
/**
* #ORM\PreUpdate()
*/
public function PreUpdate() {
$this->setUpdateAt(new \DateTime('now'));
}
it gives an error report as below :
Error
Could not convert PHP value of type 'Doctrine\ORM\Event\LifecycleEventArgs' to type 'datetime'. Expected one of the following types: null, DateTime
Help me to figure it out.

DI container can't find a class

I have an existing application with line below
\Yii::$container->invoke([$this, 'injections']);
and this line couses the error
ReflectionException Class Redis does not exist
I have a file Redis.php where defined class Redis in the /common/components directory.
But yii looks for it at the /common/components/myAPI/
А class that contains a line
\Yii::$container->invoke([$this, 'injections']);
ia located by the path /common/components/myAPI/
The whole class where I try to call the Redis
abstract class AEntity{
const API_BE_SOCCER_COUNTER = 'API_BE_SOCCER_COUNTER';
/**
* #var Fetcher $_fetcher
* #var boolean $_isFromCache
* #var APIConfig $_config
* #var string $_redisKey
* #var array $_params
*/
private $_fetcher,
$_isFromCache = null,
$_params = [],
$_redisKey = null,
$_mainApi;
/**
* #var APIRedisManager $_redis
*/
private $_redis,
$_config;
/**
* #var Array $_dbTable
* #var Array $_dbMap
*/
protected $_dbMap,
$_dbTable;
/**
*
* #return array
*/
protected function setParams(Array $params = []){
$this->_params = $params;
}
protected abstract function keyRelationShip();
protected abstract function getAttributes();
protected abstract function jsonArrayMap();
protected function setRedisKey($redisData){
$this->_redisKey = $redisData;
}
protected function mapArrays(){
$jsonArrayMap = $this->jsonArrayMap();
foreach ($jsonArrayMap as $arrayMap){
$mapPath = $arrayMap['path'] ? explode('.', $arrayMap['path']) : null;
$key = $arrayMap['key'];
$arr = $arrayMap['array'];
$assoc = $arrayMap['assoc'];
$tmpData = $this->_mainApi;
if($mapPath){
foreach($mapPath as $mapper) {
if(!isset($tmpData[$mapper])){
$this->{$key} = null;
return false;
}
$tmpData = $tmpData[$mapper];
}
}
$this->{$key} = $arr ? $this->jsonArrayMapper($tmpData, $assoc, $mapPath) : $tmpData;
}
}
public function injections(APIConfig $config, Fetcher $fetcher, APIRedisManager $redisManager){
$this->_config = $config;
$this->_fetcher = $fetcher;
$this->_redis = $redisManager;
}
protected function initialize(Array $params = []){
$constant = static::keyRelationShip();
$redisKey = $this->paramToRedis($params);
$this->setParams($params);
$this->setRedisKey($redisKey);
\Yii::$container->invoke([$this, 'injections']);
$this->_config->get($constant, $this->_params);
$this->_redis->setConfig($this->_config);
$this->_fetcher->setConfig($this->_config);
$this->_mainApi = $this->getAPIRequest();
$this->mapArrays();
}
/**
* #return array
*/
public function getMainApi(){
return $this->_mainApi;
}
/**
* #return APIRedisManager
*/
public function getRedis(){
return $this->_redis;
}
/**
* #return array
* #throws \Exception
*/
public function loadYiiData(){
$arrModel = [];
if (!$this->_dbTable) new Exception('No Table Specified.');
if (!$this->_dbMap) new Exception('No DB Map Specified.');
foreach ($this->_dbMap as $keyApi => $keyDB){
if(!isset($this->$keyDB)) throw new \Exception("KeyDB: $keyDB, is not Set");
$arrModel[$this->_dbTable][$keyApi] = $this->$keyDB;
}
return $arrModel;
}
/**
* GET API request logic
*
* #return array
*/
public function getAPIRequest(){
$redisKey = $this->formulateRedisKeyLogic();
$storedRequest = $this->_redis->getConfig() ? $this->_redis->get($redisKey) : null;
if(!$storedRequest){
$this->_isFromCache = false;
$apiRequestResult = $this->_fetcher->get()->asArray();
$this->_redis->incrCounter();
if($apiRequestResult && !$storedRequest){
$serializedApiRequest = serialize($apiRequestResult);
$this->_redis->store($serializedApiRequest, $redisKey);
}
}else{
$this->_isFromCache = true;
$apiRequestResult = unserialize($storedRequest);
}
return $apiRequestResult;
}
/** #return boolean */
public function isFromCache(){
return $this->_isFromCache;
}
private function formulateRedisKeyLogic(){
$config = $this->_redis->getConfig();
if(isset($config['key']) && strpos($this->_redisKey,'$.')!==false){
$configKey = $config['key'];
$redisKey = $configKey . $this->_redisKey;
$redisKey = str_replace('$.', '', $redisKey);
}
else{
$redisKey = $this->_redisKey;
}
return $redisKey;
}
protected function paramToRedis($param){
$className = (new \ReflectionClass($this))->getShortName();
$buildRedisKey = '$._'.str_replace('=', '_', http_build_query($param, null, ','));
$paramKey = $buildRedisKey.'_'.$className;
return $paramKey;
}
/**
* GET API request logic
*
* #return array
*/
protected function jsonArrayMapper(Array $entityItems, $assoc = false, $mapPath= true){
$aEntityArray = [];
$attributes = $this->getAttributes();
$Klass = static::class;
if($mapPath){
foreach ($entityItems as $entityItem){
$aEntity = new $Klass(false);
foreach ($attributes as $attribute){
$aEntity->{$attribute} = $entityItem[$attribute];
}
$assoc ? $aEntityArray[$entityItem[$assoc]] = $aEntity : $aEntityArray[] = $aEntity;
}
}else{
$aEntity = new $Klass(false);
foreach ($attributes as $attribute){
$aEntity->{$attribute} = $entityItems[$attribute];
}
$aEntityArray = $aEntity;
}
return $aEntityArray;
}
public function __set($key, $value){
$this->{$key} = $value;
}
public function __get($name) {
return $this->{$key};
}
}
This is a super class for class those have such constructor
public function __construct($fullInitizalization=true, $params = []) {
if($fullInitizalization){
$redisParams = $this->paramToRedis($params);
parent::initialize($params);
}
}
When DI container trying to instaniate APIRedisConnection class, it passes parameter with type Redis:
/** #param Redis $redis */
function __construct(Redis $redis){
$this->_redis = $redis;
}
Class Redis can't be found in the project, but I can see it in IDE and this class was written on PHP 7
Although the whole project written on PHP 5.6