Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

php - Doctrine2 findBy relationship object triggers string conversion error

Say I have two entities in Doctrine2 that are related to each other, ModelsUser and ModelsComment. If I do this in Doctrine 2.0.0...

<?php
// $em instanceof EntityManager, $user instanceof ModelsUser
$comments = $em->getRepository('ModelsComment')
    ->findBy(array('user' => $user, 'public' => true));

...I get a PHP error:

Severity: Notice

Message: Object of class ModelsUser to string conversion

Filename: DBAL/Connection.php

Line Number: 574

This shouldn't happen, right? If I use the QueryBuilder and setParameter('user', $user) it works as expected.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Query by relationship is allowed, but you have to pass the Identifier in there. Query by object is not yet supported and will only make it into 2.1.

<?php
// $em instanceof EntityManager, $user instanceof ModelsUser
$comments = $em->getRepository('ModelsComment')
->findBy(array('user' => $user->getId(), 'public' => true));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...