实际上,我正在使用Doctrine的setParame进行查询,但如果我尝试获取查询,则会看到参数未设置。
代码是这样的:
$query = $this->createQueryBuilder('l')
->where('l.project = :projectId')
->andWhere('l.state.value != :deletedState')
->setParameters([
'projectId' => $project->getId(),
'deletedState' => LandingState::STATE_DELETED,
]);
如果我制作一个var_dump
,这就是我得到的查询'SELECT l0_.lan_id AS lan_id_0, l0_.lan_title AS lan_title_1,
l0_.lan_url AS lan_url_2, l0_.lan_testimony AS lan_testimony_3,
l0_.lan_testimony_content AS lan_testimony_content_4,
l0_.lan_final_claim AS lan_final_claim_5, l0_.lan_service_ids AS
lan_service_ids_6, l0_.lan_state AS lan_state_7, l0_.lan_index AS
lan_index_8, l0_.lan_follow AS lan_follow_9, l0_.lan_script AS
lan_script_10, l0_.lan_date_insert AS lan_date_insert_11,
l0_.lan_date_update AS lan_date_update_12, l0_.pro_id AS pro_id_13,
l0_.lan_image AS lan_image_14, l0_.lan_testimony_image AS
lan_testimony_image_15 FROM landing l0_ WHERE l0_.pro_id = ? AND
l0_.lan_state <> ?'
但是如果我试图获得$ project-&gt; getId()或带有echo的LandingState :: STATE_DELETED的值,我得到的值。为什么查询中没有设置的值?
答案 0 :(得分:0)
我看到的方式没有错, 看看这个简单的例子来理解:
控制器中的代码:
$search = 'test';
$em = $this->getDoctrine()->getManager();
$queryBuilder = $em->createQueryBuilder();
$queryBuilder->select('d')
->from('BackBundle\Entity\disponibility', 'd')
->where('d.name like :search')
->setParameter('search', '%' .$search. '%');
$query = $queryBuilder->getQuery();
dump($query->getSQL());
dump($query);
转储的内容:
$查询 - &GT; getSQL();
"SELECT d0_.id AS id0, d0_.name AS name1 FROM disponibility d0_ WHERE d0_.name LIKE ?"
$查询;
Query {#789 ▼
-_state: 1
-_dql: "SELECT d FROM BackBundle\Entity\disponibility d WHERE d.name like :search"
-_parserResult: ParserResult {#793 ▼
-_sqlExecutor: SingleSelectExecutor {#821 ▼
#_sqlStatements: "SELECT d0_.id AS id0, d0_.name AS name1 FROM disponibility d0_ WHERE d0_.name LIKE ?"
#queryCacheProfile: null
}
-_resultSetMapping: ResultSetMapping {#788 ▼
+isMixed: false
+aliasMap: array:1 [▼
"d" => "BackBundle\Entity\disponibility"
]
+relationMap: []
+parentAliasMap: []
+fieldMappings: array:2 [▼
"id0" => "id"
"name1" => "name"
]
+scalarMappings: []
+typeMappings: []
+entityMappings: array:1 [▼
"d" => null
]
+metaMappings: []
+columnOwnerMap: array:2 [▼
"id0" => "d"
"name1" => "d"
]
+discriminatorColumns: []
+indexByMap: []
+declaringClasses: array:2 [▼
"id0" => "BackBundle\Entity\disponibility"
"name1" => "BackBundle\Entity\disponibility"
]
+isIdentifierColumn: []
+newObjectMappings: []
+metadataParameterMapping: []
}
-_parameterMappings: array:1 [▼
"search" => array:1 [▼
0 => 0
]
]
}
-_firstResult: null
-_maxResults: null
-_queryCache: null
-_expireQueryCache: false
-_queryCacheTTL: null
-_useQueryCache: true
#parameters: ArrayCollection {#790 ▼
-elements: array:1 [▼
0 => Parameter {#791 ▼
-name: "search"
-value: "%test%"
-type: 2
}
]
}
#_resultSetMapping: null
#_em: EntityManager {#348 …10}
#_hints: []
#_hydrationMode: 1
#_queryCacheProfile: null
#_expireResultCache: false
#_hydrationCacheProfile: null
}
最后来自symfony profiler的查询:
SELECT count(DISTINCT d0_.id) AS sclr0 FROM disponibility d0_ WHERE d0_.name LIKE ?
Parameters: ['%test%']
正如您所看到的,Doctrine的SetParameter工作正常,但dump函数不会显示参数。如果要查看参数,请在分析器中查看它们