我在symfony存储库中有功能
public function getAllSites($criteria = null)
{
$qb = $this->createQueryBuilder('s')
->select('s')
->orderBy('s.root', 'ASC')
->addOrderBy('s.lft', 'ASC');
if($criteria !== null):
foreach ($criteria as $field => $value):
if (!$this->getClassMetadata()->hasField($field)):
continue; // Make sure we only use existing fields (avoid any injection)
endif;
$qb ->andWhere($qb->expr()->eq('s.'.$field, ':s_'.$field))
->setParameter('s_'.$field, $value);
endforeach;
endif;
return $qb->getQuery()->getResult();
}
我想检查字段的类型,然后使用搜索LIKE%word%,如果字段类型是字符串(或文本),则使用其他字段。
您知道symfony存储库中哪个函数返回fieldType吗?
答案 0 :(得分:3)
我现在找到了答案。该函数是$ this-> getClassMetadata() - > getTypeOfField($ field)。
示例:
public function getAllSites($criteria = null)
{
$qb = $this->createQueryBuilder('s')
->select('s')
->orderBy('s.root', 'ASC')
->addOrderBy('s.lft', 'ASC');
if($criteria !== null):
foreach ($criteria as $field => $value):
if (!$this->getClassMetadata()->hasField($field)):
continue; // Make sure we only use existing fields (avoid any injection)
endif;
if ($this->getClassMetadata()->getTypeOfField($field) === 'string'
|| $this->getClassMetadata()->getTypeOfField($field) === 'text'):
$qb ->andWhere($qb->expr()->like('s.'.$field, ':s_'.$field))
->setParameter('s_'.$field, '%'.$value.'%'); // search LIKE %value%
else:
$qb ->andWhere($qb->expr()->eq('s.'.$field, ':s_'.$field))
->setParameter('s_'.$field, $value);
endif;
endforeach;
endif;
return $qb->getQuery()->getResult();
}
您可以找到有关ClassMetadata的更多功能 here.
答案 1 :(得分:-1)
我认为您正在寻找PHP的getType()
功能。有关如何使用它的详细信息,请参阅PHP docs。
$var = "abc";
$type = getType($var)
// returns 'string'