php - TYPO3 QueryRepository returns empty and can not use it as condition

704

I have created a query on my repository file which searches for objects with specific values. For example:

/**
     * @param int $uid
     * @param string $value
     * @param int $someIntValue
     *
     * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
     */
    public function findByFields($uid, $value, $someIntValue)
    {
        $query = $this->createQuery();
        $query->getQuerySettings()->setRespectStoragePage(FALSE);
        $query->matching(
            $query->logicalAnd(
                [
                    $query->equals('some_field', $uid),
                    $query->equals('value', $value),
                    $query->equals('some_other_field', $someIntValue)
                ])
        );
        return $query->execute();

Now this gives me an empty object which is fine because i don't have anyhting in the database which meets the requirements.

But if i want to add a condition which evaluates if the object is empty it does not work.

Code in my Task

$getObjects = $someRepository->findByFields($oldObject->getUid(), $oldObject->getValue, $oldObject->getExternalValue);

if(empty($getObjects))
{
    // do something
}
else 
{
    // do something else
}

Here fails to evaluate the object as empty because it is not actually empty. If i typecast it to an array: (Just to see what is inside)

$typecasted = (array)$getObjects;

It gives me back what exists in the "empty" object. (with var_dump)

*dataMapper => TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper singleton object filtered
*persistenceManager => TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager singleton object filtered
*numberOfResults => NULL
*query => TYPO3\CMS\Extbase\Persistence\Generic\Query prototype object
*queryResult => NULL

The question:

How do i get the condition to work?

788

Answer

Solution:

I found the solution.

If nothing found TYPO3 returns the query. All i had to do is the following:

if($query->execute()->count())
{
   return $query->execute();
}
else 
{
   return NULL;
}

What this does, it to evaluate if there are any objects available. If yes then it gives back the objects. If not, it sets the result to NULL and with that the condition can be used.

EDIT :

The above works but after @Mathias Brodala 's recommendation, i tried something like that as well.

if (count($getObjects) > 0)
{
    \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump('exists');
} else {
    \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump('does not exist');
} 

Both ways work. Take your pick.

Thanks @Mathias Brodala

People are also looking for solutions to the problem: php - Unable to record call using twilio proxy

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.