简体   繁体   中英

how to use “findby” doctrine with input as array?

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.

$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;
}

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]);
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM