如何将 querybuilder 与 Symfony formbuilder 结合使用来连接两个实体?

How can use querybuilder with Symfony formbuilder to join two entities?

$options['class'] = 'App\Entity\Data';
$options['attr'] = array('class' => 'form-control select2');
$options['query_builder'] = function (EntityRepository $er) use ($fieldId,$documentId) {
    return $er->createQueryBuilder('data')
        ->leftJoin('data.documents', 'dd')
        ->andWhere('dd.pages = :id')
        ->andWhere('dd.uuid = data.document_id')
        ->andWhere('data.field = :field')
        ->setParameter(':id', 16)
        ->setParameter(':field', 35)
     ;
};
$options['choice_label'] = 'content';

错误信息:

[Semantical Error] line 0, col 127 near 'field = :fie': Error: Class App\Entity\Data has no field or association named field

您的 class 中是否包含以下内容?

use Doctrine\ORM\Mapping as ORM;

class Data
{
    /**
     * @ORM\Column(name="field")
     */
    private $field;
}

通常这足以让学说解决该错误

  • 正如@yceruto 在评论中所说

// This is right
->setParameter('field', 22)

而不是

// This is wrong
->setParameter(':field', ...)