我有一個包含simple_array屬性$ PublicComments的實體LongBeachClickerUploadBallotOption:主義findOneBy simple_array屬性不返回有效的匹配
/**
* @var array
* @ORM\Column(name="public_comments", type="simple_array", nullable=true)
*/
protected $publicComments;
當我實例化一個新的對象,並給它多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;
該代碼輸出如下:
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
這是怎麼回事?出於好奇,我嘗試用整數「字符串」和真正的整數查詢。
按照理論的SimpleArrayType,就應該破滅這些值與「」然後我會假設只是做一個字符串匹配......在數據庫中,我可以看到該行存儲正確:
id motion public_comments
109 AMPC 1,2,3
編輯: 我加入SQL日誌記錄:
$this->em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
它輸出以下內容:
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) }
當然,作爲第一個響應所提到的,我試着查詢是這樣的:
$bo = $this->em->getRepository('VoteBundle:LongBeachClickerUploadBallotOption')
->findOneBy(array('motion' => 'AMPC', 'publicComments' => "1,2,3"));
只爲它抱怨:
警告:破滅():通過
我懷疑我只需要使用查詢生成器 –