1

Could anyone tell me how to use 'findby' with input as an array of objects?? i got code like this:

public function getIpOnline($acc)
{
    try {                    
        $rs = $this->em
            ->getRepository($this->target)
            ->findBy(array('login' => $acc))
        ;
    } catch (Exception $e) {          
        echo "ERROR ".$this->target." DAO: ".$e;
    }               
    var_dump($rs);exit();
    return $rs;     
}

and i got error :

 Catchable fatal error: Object of class Character could not be converted to string in /var/www/xxx.com/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php on line 67

Thanks in advance.

0

3 Answers 3

2

$this->target is probably an object but you must returns the name of the class of an object.

Have you try to use get_class?

Or more simply Character::class :)

Like you asked, this is your example with get_class:

public function getIpOnline($acc)
{
    $nameClass = get_class($this->target);

    try {
        $rs = $this->em
            ->getRepository($nameClass)
            ->findBy(['login' => $acc]);
    } catch (Exception $e) {
        echo 'ERROR ' . $nameClass . ' DAO: ' . $e;
    }
    die(var_dump($rs));

    return $rs;
}
Sign up to request clarification or add additional context in comments.

2 Comments

could u please tell me in detail?? i don't understand.
@Hanata In function getRepository($repoName); you have to pass class name of Entity. for example you can type there 'Product\Entity\Phone', or use function get_class($object), or just use Product::class. All these soultions will return full class name with namespace of class/object.
0

You can do it only in case when your instance $name have magic method __toString.

Fox example:

class Name
{
    public function __toString()
    {
        return 'ipad';
    }
}

$name = new Name();
$product = $entityManager->getRepository('Product')->findBy(['name' => $name]);

Comments

0
public function getIpOnline($acc)
{
 try {                    
    $rs = $this->em->getRepository($this->target)-findBy(['login' => $acc));

 /* or u can also use findOneBy if expecting result is a single record and find corresponding data only based in $acc
  $rs = $this->em->getRepository($this->target)-findOneByLogin($acc);
 */

 } catch (Exception $e) {          
    echo "ERROR ".$this->target." DAO: ".$e;
}               
var_dump($rs);exit();
return $rs;     

}

also see http://symfony.com/doc/current/doctrine.html#fetching-objects-from-the-database

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.