简体   繁体   中英

select specific Columns with createQueryBuilder in Doctrine ORM using Parameters

$qb = $this->em->createQueryBuilder()
    ->select("p.$input")
    ->from('AppBundle:Profile', 'p')
    ->where("p.$input LIKE :value")
    ->setParameter('value', '%'.$value.'%')
    ->setMaxResults(5);

$results = $qb->getQuery()->execute();
$response['results'] = $results;

is there a way to even set the $input using setParameter ?

I am doing this in that way:

class EntityHelper{
    /**
     * @param Object $entity
     * 
     * @return array
     */
    public static function entityToArray($entity) {
        $className = "\0" . get_class($entity) . "\0";
        $entityAsArray = (array)$entity;
        $retArray = array();
        foreach ($entityAsArray as $key => $val) {
            $retArray[str_replace($className, '', $key)] = $val;
        }
        return $retArray;
    }

    /**
     * @param Object $entity
     * @param string $parameter
     *
     * @return boolean
     */
    public static function checkEntityHasParameter($entity, $parameter) {
        return array_key_exists($parameter, self::entityToArray($entity));
    }
}

Use checkEntityHasParameter static method to check that field will occur in your table.

WARNING! It works only with standard type fields, and sometimes when mapping were used.

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