Symfony Controller findBy() not working with variable

1.9k views Asked by At

I try to use findBy() in my controller and it only works when I use plain text and not a variable - but both are strings, for example:

$repository = $this->getDoctrine()->getRepository('SchlauchBundle:Armatur');

$armatur = $entity->getArmatur();

$armaturen = $repository->findBy(
    array('nameArmatur' => $entity->getArmatur())
);

var_dump($armatur) outputs:
string(21) "Klemmschalen EN 14420"

but var_dump($armaturen) outputs:
NULL.

When I change the findBy()-part to:

$armaturen = $repository->findBy(
    array('nameArmatur' => "Klemmschalen EN 14420")
);

it works - it is so confusing me! What could be the problem that it is not possible for me to use the variable? I don't understand the difference.

In entity file it's defined (nothing special):

/**
* @var string
*/
private $nameArmatur;
3

There are 3 answers

1
didando8a On

Try making a trim(array('nameArmatur' => $entity->getArmatur()) to see what's the behaviour

3
Channaveer Hakari On

I use the following styling which will just work fine -

$marker = $em->getRepository('SkerpGenericBundle:Marker')->findBy(array('userid' => $userObj->getId()));

Thats strange! I didnt face that kind of issue.

0
Andrii Mishchenko On

Seems that $entity->getArmatur() might return an object with implemented __toString method. Maybe try something like:

$armaturen = $repository->findBy(
    array('nameArmatur' => $entity->getArmatur()->__toString())
);