Я не могу понять, как извлечь конкретный адрес с помощью обратного вызова doctrine-orm.Результат, который я получаю от своей функции getAddressFilter()
, всегда пуст.Вот как мои сущности связаны друг с другом: у меня есть пять классов: Basis, Well, Container, Ticket и TicketPlanningAdmin.
<?php
/**
* Class Basis
*
* @ORM\Entity(repositoryClass="CMS3\CoreBundle\Repository\BasisRepository")
* @ORM\Table(name="basis", options={"charset"="utf8mb4","collate"="utf8mb4_unicode_ci"})
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({"well" = "CMS3\CoreBundle\Entity\Well", "container" = "CMS3\CoreBundle\Entity\Container"})
* @ORM\HasLifecycleCallbacks
*
* @Api\ExclusionPolicy("all")
* @Api\Discriminator(field="discr", disabled=true)
*/
abstract class Basis
{
/**
* Unique identifier for the object
*
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*
* @Api\Expose()
* @Api\Groups({"list", "details"})
* @Api\Since("1.0")
*/
private $id;
/**
* @var Ticket[]|Collection
*
* @ORM\ManyToMany(targetEntity="CMS3\CoreBundle\Entity\Ticket", mappedBy="basi")
*/
private $tickets;
// getters and setters
}
/**
* Class Well
*
* @ORM\Table(name="well", options={"charset"="utf8mb4","collate"="utf8mb4_unicode_ci"})
* @ORM\Entity(repositoryClass="CMS3\CoreBundle\Repository\WellRepository")
*
* @Api\ExclusionPolicy("all")
* @Api\AccessorOrder("custom", custom = {
* "id",
* "idNumber",
* "serialNumber",
* "active",
* "containers",
* "location",
* "owner",
* "createdAt",
* "placingDate",
* "operationalDate",
* "warrantyDate",
* })
*/
class Well extends Basis
{
/**
* The address of the location of the well
*
* @var string
*
* @ORM\Column(name="address", type="string", length=255)
*/
private $address;
/**
* The containers this well holds
*
* (Note: This field is subject to change and might change into an array of
* hyperlinks to the related resources)
*
* @var Container[]|Collection
*
* @ORM\OneToMany(targetEntity="CMS3\CoreBundle\Entity\Container", mappedBy="well")
*
* @Api\Expose()
* @Api\SerializedName("containers")
* @Api\Accessor(getter="getContainerIds")
* @Api\Type("array<integer>")
* @Api\Groups({"details"})
* @Api\Since("1.0")
*/
private $containers;
// getters and setters
}
/**
* Class Container
*
* TODO - Convert Location, Position, Address information to their own entity classes
*
* @package CMS3\CoreBundle\Entity
*
* @Api\ExclusionPolicy("all")
* @Api\AccessorOrder("custom", custom = {
* "id",
* "idNumber",
* "serialNumber",
* "active",
* "containerType",
* "emptyFrequency",
* "owner",
* "fractie",
* "well",
* "createdAt",
* "placingDate",
* "operationalDate",
* "warrantyDate",
* })
*
* @ORM\Table(name="container", options={"charset"="utf8mb4","collate"="utf8mb4_unicode_ci"})
* @ORM\Entity(repositoryClass="CMS3\CoreBundle\Repository\ContainerRepository")
*/
class Container extends Basis
{
/**
* The well in which this container is placed
*
* (Note: This field is subject to change, and might change into hyperlinks to
* the related resource)
*
* @var Well
*
* @ORM\ManyToOne(targetEntity="CMS3\CoreBundle\Entity\Well", inversedBy="containers")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="well_id", referencedColumnName="id")
* })
*
* @Api\Expose()
* @Api\Accessor(getter="getWellId")
* @Api\Type("integer")
* @Api\Groups({"details"})
* @Api\Since("1.0")
*/
private $well;
// getters and seters
}
/**
* Ticket
*
* @ORM\Table(name="ticket", options={"charset"="utf8mb4","collate"="utf8mb4_unicode_ci"}, indexes={@ORM\Index(name="archived_idx", columns={"archived"})})
* @ORM\Entity(repositoryClass="CMS3\CoreBundle\Repository\TicketRepository")
* @ORM\HasLifecycleCallbacks
*/
class Ticket
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var Basis[]|Collection
*
* @ORM\ManyToMany(targetEntity="CMS3\CoreBundle\Entity\Basis", inversedBy="tickets")
* @ORM\JoinTable(name="ticket_basis",
* joinColumns={
* @ORM\JoinColumn(name="ticket_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="basis_id", referencedColumnName="id")
* }
* )
*/
private $basi;
/**
* @var TicketPlanning
*
* @ORM\OneToOne(targetEntity="CMS3\CoreBundle\Entity\TicketPlanning", mappedBy="ticket", cascade={"persist", "remove"})
*/
private $ticketPlanning;
// getters and setters
}
/**
* Class TicketPlanningAdmin
*
*/
class TicketPlanningAdmin extends AbstractAdmin
{
/**
* @param \Sonata\AdminBundle\Datagrid\DatagridMapper $datagridMapper
*/
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('ticket.id')
->add(
'address',
'doctrine_orm_callback',
[
//'callback' => [$this, 'getAddressFilter1'],
'callback' => [$this, 'getAddressFilter1'],
'field_type' => TextType::class,
]
)
->add('ticket.ticketType')
->add('serviceMan')
->add(
'assignation',
'doctrine_orm_date_range',
['field_type' => DateRangePickerType::class]
);
}
public function getAddressFilter($queryBuilder, $alias, $field, $value): bool
{
if (!isset($value['value']) || !$value['value']) {
return false;
}
// Use `andWhere` instead of `where` to prevent overriding existing `where` conditions
$queryBuilder->leftJoin(Basis::class, 'basis', 'WITH', 't.id = basis.id');
$queryBuilder->leftJoin(Container::class, 'c', 'WITH', 'basis.id = c.id');
$queryBuilder->leftJoin(Well::class, 'wc', 'WITH', 'wc.id = c.well');
$queryBuilder->leftJoin(Well::class, 'w', 'WITH', 'basis.id = w.id');
$queryBuilder->andWhere('w.address LIKE :address OR wc.address LIKE :address');
$queryBuilder->setParameter('address', '%' . $value['value'] . '%');
return true;
}
}
Ссылка на таблицы здесь: http://pastebin.com/0UZZC0bf.