Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CheckIfExists.php
Go to the documentation of this file.
1 <?php
7 
13 
18 {
22  private $resourceConnection;
23 
27  private $metadataPool;
28 
32  private $hydratorPool;
33 
37  private $typeResolver;
38 
45  public function __construct(
46  TypeResolver $typeResolver,
47  MetadataPool $metadataPool,
48  HydratorPool $hydratorPool,
49  ResourceConnection $resourceConnection
50  ) {
51  $this->metadataPool = $metadataPool;
52  $this->hydratorPool = $hydratorPool;
53  $this->typeResolver = $typeResolver;
54  $this->resourceConnection = $resourceConnection;
55  }
56 
64  public function execute($entity, $arguments = [])
65  {
66  $entityType = $this->typeResolver->resolve($entity);
67  $metadata = $this->metadataPool->getMetadata($entityType);
68  $hydrator = $this->hydratorPool->getHydrator($entityType);
69  $connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName());
70  $entityData = $hydrator->extract($entity);
71  if (!isset($entityData[$metadata->getIdentifierField()])) {
72  return false;
73  }
74  return (bool)$connection->fetchOne(
75  $connection->select()
76  ->from($metadata->getEntityTable(), [$metadata->getIdentifierField()])
77  ->where($metadata->getIdentifierField() . ' = ?', $entityData[$metadata->getIdentifierField()])
78  ->limit(1)
79  );
80  }
81 }
__construct(TypeResolver $typeResolver, MetadataPool $metadataPool, HydratorPool $hydratorPool, ResourceConnection $resourceConnection)
$entity
Definition: element.phtml:22
$arguments
$connection
Definition: bulk.php:13