简体   繁体   English

如何将“ findby”学说与输入作为数组一起使用?

[英]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.

相关问题 如何将数组传递给 doctrine 查询 findBy? - How to pass array into the doctrine query findBy? 如何使用findBY函数在教义中使用OrderBy - How to use OrderBy in Doctrine with findBY function Doctrine findBy *方法和获取数组 - Doctrine findBy* methods and fetch array 如何使用查询/结果缓存(Doctrine 1.2)使Doctrine查找(如Doctrine_table-> find,-> findby *) - How to make Doctrine lookups like Doctrine_table->find, ->findby* use the query/results cache (doctrine 1.2) 如何在Doctrine中使用findBy()来订购结果 - How to order results with findBy() in Doctrine 为 findBy 准则存储库函数返回具有特定键的关联数组 - Return an associative array with certain key for a findBy doctrine repository function 在 Symfony2 Doctrine findBy() 中保留数组的初始顺序 - Preserve the initial order of array in Symfony2 Doctrine findBy() 从数组动态设置方法参数(使用Doctrine中的FindBy方法) - setting method parameters dynamically from array (working with FindBy method in Doctrine) Zend Framework&Doctrine 2 findBy使用实体返回空数组 - Zend Framework & Doctrine 2 findBy using entity returning empty array 使用Doctrine魔术“findby”方法进行制作是否安全? 或者它们仅用于原型设计? - Is it safe to use Doctrine magic “findby” methods for production? Or are they only for prototyping?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM