-2

I like to check if there is data in the repository:

$dataCollection = $this->em->getRepository(Data::class)->DataConnector($fieldId,$id);

if(isset($dataCollection)){
    echo "this contains data";
} else {
   echo "this does not contain data";
}

But even if no data is in the repository, I always get the message "this contains data".

yivi
  • 42,438
  • 18
  • 116
  • 138
peace_love
  • 6,229
  • 11
  • 69
  • 157
  • 1
    [`isset`](https://www.php.net/manual/en/function.isset.php): '*Determine if a variable is declared and is different than NULL*', are you sure you need `isset`? What is the dumped value of `$dataCollection`? – Script47 Mar 25 '19 at 14:52
  • 1
    What's the definition for `DataConnector`? I think that's not a standard doctrine method. – yivi Mar 25 '19 at 14:57
  • @Script47 this is the dump value of dataCollection: `[]` – peace_love Mar 25 '19 at 15:01
  • won't this always result in a valid isset() statement? You basically always have a repository item anyways – Liam roels Mar 25 '19 at 15:02
  • Why not check using `!empty($dataCollection)`, then? – yivi Mar 25 '19 at 15:02
  • @yivi yes "empty" is working. I think I have difficulties unerstanding "isset" – peace_love Mar 25 '19 at 15:05
  • Dupe of: https://stackoverflow.com/questions/2216052/how-to-check-whether-an-array-is-empty-using-php – yivi Mar 25 '19 at 15:05

2 Answers2

0

The output ([]) is an empty array. An empty array IS set:

$x = [];

print_r(isset($x)); // 1
print_r(empty($x)); // 1
print_r(count($x)); // 0

You need either count or empty.

Script47
  • 14,230
  • 4
  • 45
  • 66
-2
$dataCollection = $this->em->getRepository(Data::class)->DataConnector($fieldId,$id);

if(dataCollection != NULL){
    echo "this contains data";
} else {
   echo "this does not contain data";
}
Onur KAYA
  • 234
  • 1
  • 9