简体   繁体   中英

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';

The error message:

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

Do you have the following in your class?

use Doctrine\ORM\Mapping as ORM;

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

Normally that would be enough for doctrine to resolve that error

  • And as @yceruto says in the comments

It is

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

Instead of

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM