[英]Doctrine findOneBy simple_array property isn't returning a valid match
I have an entity LongBeachClickerUploadBallotOption that contains a simple_array property $publicComments: 我有一个实体LongBeachClickerUploadBallotOption,其中包含一个simple_array属性$ publicComments:
/**
* @var array
* @ORM\Column(name="public_comments", type="simple_array", nullable=true)
*/
protected $publicComments;
When I instantiate a new object and give it multiple publicComments, and then try to query for that object, doctrine does not seem to return a match: 当我实例化一个新对象并为其提供多个publicComments,然后尝试查询该对象时,主义似乎没有返回匹配项:
$lbBallotOption = new LongBeachClickerUploadBallotOption('AMPC', array('1', '2', '3'));
$this->em->persist($lbBallotOption);
$this->em->flush();
dump($lbBallotOption->getPublicComments());
$bo = $this->em->getRepository('VoteBundle:LongBeachClickerUploadBallotOption')
->findOneBy(array('motion' => 'AMPC', 'publicComments' => array('1', '2', '3')));
dump($bo);
$bo = $this->em->getRepository('VoteBundle:LongBeachClickerUploadBallotOption')
->findOneBy(array('motion' => 'AMPC', 'publicComments' => array(1, 2, 3)));
dump($bo);
exit;
This code outputs the following: 此代码输出以下内容:
HearingVoteImport.php on line 258:
array:3 [▼
0 => "1"
1 => "2"
2 => "3"
]
HearingVoteImport.php on line 263:
null
HearingVoteImport.php on line 268:
null
What's going on here? 这里发生了什么? I tried querying both with integer "strings" and true ints just out of curiosity. 出于好奇,我尝试使用整数“字符串”和true整数进行查询。
According to Doctrine's SimpleArrayType, it should implode these values with a ',' and then I would assume just do a string match... The rows in the database I can see are storing properly: 根据Doctrine的SimpleArrayType,它应该用','内嵌这些值,然后我假设只是进行字符串匹配...我可以看到的数据库中的行已正确存储:
id motion public_comments
109 AMPC 1,2,3
Edit: I added SQL Logging: 编辑:我添加了SQL日志记录:
$this->em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
and it outputs the following: 它输出以下内容:
SELECT t0.id AS id1,
t0.motion AS motion2,
t0.public_comments AS public_comments3
FROM lb_clicker_upload_ballot_option t0
WHERE t0.motion = ? AND t0.public_comments IN (?) LIMIT 1
array(2) { [0]=> string(4) "AMPC" [1]=> array(3) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" } }
array(2) { [0]=> string(6) "string" [1]=> int(102) }
And of course, as mentioned in the first response, I tried querying like this: 当然,正如在第一个响应中提到的那样,我尝试过这样的查询:
$bo = $this->em->getRepository('VoteBundle:LongBeachClickerUploadBallotOption')
->findOneBy(array('motion' => 'AMPC', 'publicComments' => "1,2,3"));
only for it to complain: 只为它抱怨:
Warning: implode(): Invalid arguments passed 警告:implode():传递了无效的参数
AFAIK, you have to query array fields like a standard text field . AFAIK,您必须查询数组字段,例如标准文本字段 。 So imloding an array is an easiest way to go. 因此,内联数组是最简单的方法。
I know the question is quite old, but since I stumbled across the same issue... The SQL Logging was quite helpful. 我知道这个问题已经很老了,但是由于我偶然发现了同一问题... SQL日志记录非常有用。
The problem is, that in public_comments
a string of comma separated strings is stored in the database. 问题是,在public_comments
,一串用逗号分隔的字符串存储在数据库中。 This string has to be found in the array given to the IN
keyword. 必须在给IN
关键字的数组中找到该字符串。 Since in this array the values are separate and not concatenated to a string as in public_comments
the IN
operator can't find the string. 由于在此数组中,值是分开的,并且不像public_comments
那样连接到字符串,因此IN
运算符找不到该字符串。
I circumvented the issue by preparing the $search
array as 我通过将$search
数组准备为
$public_comments = array(1, 2, 3);
$search = array(implode(',', $public_comments));
$repo = $this->em->getRepository('VoteBundle:LongBeachClickerUploadBallotOption');
$bo = $repo->findOneBy(array('motion' => 'AMPC', 'publicComments' => $search));
The wrapping array()
is needed to avoid the observed complaints about the invalid arguments in implode. 需要使用wrapping array()
来避免观察到的关于内爆无效参数的抱怨。 I'd say it's a bug in handling of simple_array in a search context. 我会说这是在搜索上下文中处理simple_array的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.