-1

I'm getting this error while trying to compare an object to a string I believe. But I can't find a way to get the data from the object to compare.

I'm getting

Call to a member function getStatus() on array

Which makes sense as I'm trying to compare the object to "1" but how do I pull out the object content in the foreach and compare it to 1?

$this->data['document_collection'] = $this->em->getRepository('Entities\Documents')->findAll();
$this->data['onboarding_data'] = $this->em->getRepository('Entities\Onboarding')->findBy(['user' => $this->session->user_id]);

<?php
foreach($document_collection as $onboarding):
?>
<tr>
    <td>
    <?php
    echo $onboarding->getDocument();
    ?>
    </td>
    <td align="center">
    <?php
    echo anchor($onboarding->getUrl(), "Download");
    ?>
    </td>
    <td class="">
    <label class="option block mn">
    <input type="checkbox"
    name="status" value="1" 
    <?php if($onboarding_data->getStatus() == "1" && $onboarding->getId() == $onboarding_data->getDocument_Id()->getId()):?>
     checked="checked"
     <?php endif;?>
     >
     </td>
</tr>
<?php endforeach;?>
tereško
  • 58,060
  • 25
  • 98
  • 150
Gabriel Dube
  • 77
  • 12
  • 1
    did you mean `$onboarding->getStatus()`? – Nick Dec 02 '19 at 22:22
  • 1
    `$onboarding_data` is undefined – jibsteroos Dec 02 '19 at 22:22
  • @jibsteroos it is defined with $this->data[‘onboarding_data’]. If it was undefined it would give me the undefined index error. It’s giving me an array error – Gabriel Dube Dec 02 '19 at 23:35
  • @Nick I actually meant that variable. I’m using codeigniter and that’s how my controller is defined, it recognizes the variable from $this->data->[‘onboarding_data’] as $onboarding_data – Gabriel Dube Dec 02 '19 at 23:37
  • `findBy()` search for all elements that match, so it will return an array of object even when there is only one match, I think what you want is `findOneBy()` – catcon Dec 02 '19 at 23:52
  • @catcon I want an array of elements as I'm doing a loop to display them. But how do I condition it if any of those elements matches "1"? – Gabriel Dube Dec 03 '19 at 00:01
  • So `$onboarding_data` is an array of objects with a `getStatus()` method? – Nick Dec 03 '19 at 05:47
  • @Nick yes, it’s an array of objects with a getStatus() method – Gabriel Dube Dec 03 '19 at 12:21
  • You probably want to use `array_reduce` then to check each of the results of the `getStatus` method calls for each element in the array and return a single boolean result you can use in your `if` – Nick Dec 03 '19 at 20:17

1 Answers1

0

Changing my line from

 <?php if($onboarding_data->getStatus() == "1" && $onboarding->getId() == $onboarding_data->getDocument_Id()->getId()):?>

To

 <?php if(in_array("1", $onboarding_data->getStatus())) && $onboarding->getId() == $onboarding_data->getDocument_Id()->getId()):?>

Worked for me

Gabriel Dube
  • 77
  • 12