[英]how to use “findby” doctrine with input as array?
Could anyone tell me how to use 'findby' with input as an array of objects?? 谁能告诉我如何将'findby'与输入作为对象数组一起使用? 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. $this->target
可能是一个对象,但是您必须返回对象的类的名称。
Have you try to use get_class
? 您尝试使用
get_class
吗?
Or more simply Character::class
:) 或更简单的是
Character::class
:)
Like you asked, this is your example with get_class
: 就像您问的那样,这是您使用
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
. 仅当实例
$name
具有魔术方法__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 另请参见http://symfony.com/doc/current/doctrine.html#fetching-objects-from-the-database
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.