Php 通过循环所有行来显示所有用户
我对PHP非常陌生,在创建一个API时遇到了一些问题,该API本质上是接受GET请求并返回存储在表中的所有用户。此应用程序使用Zend Framework 2和条令 控制器:Php 通过循环所有行来显示所有用户,php,rest,api,doctrine-orm,zend-framework2,Php,Rest,Api,Doctrine Orm,Zend Framework2,我对PHP非常陌生,在创建一个API时遇到了一些问题,该API本质上是接受GET请求并返回存储在表中的所有用户。此应用程序使用Zend Framework 2和条令 控制器: public function viewAllAction(){ $restService = $this->getServiceLocator()->get("Rest"); $restService->auth(); $business = $restService->
public function viewAllAction(){
$restService = $this->getServiceLocator()->get("Rest");
$restService->auth();
$business = $restService->getIdentity();
$bizID = $business->getId();
$clientModel = $this->getServiceLocator()->get('clientModel');
$clients = $clientModel->findAllByBusinessID($bizID);
$params['client'] = array(
"name" => $clients->getName(),
"email" => $clients->getEmail(),
"phone" => $clients->getPhone(),
"address" => $clients->getAddress(),
"suburb" => $clients->getSuburb(),
"postcode" => $clients->getPostcode(),
"state" => $clients->getState(),
"country" => $clients->getCountry(),
);
return $restService->send($params);
}
public function findAllByBusinessID( $business_id ){
$clientRepository = $this->getMapper()->getRepository( self::ENTITY_CLASS );
$clients= $clientRepository->findBy(array("bid" => $business_id));
foreach($clients as $client) {
return $client;
}
}
型号:
public function viewAllAction(){
$restService = $this->getServiceLocator()->get("Rest");
$restService->auth();
$business = $restService->getIdentity();
$bizID = $business->getId();
$clientModel = $this->getServiceLocator()->get('clientModel');
$clients = $clientModel->findAllByBusinessID($bizID);
$params['client'] = array(
"name" => $clients->getName(),
"email" => $clients->getEmail(),
"phone" => $clients->getPhone(),
"address" => $clients->getAddress(),
"suburb" => $clients->getSuburb(),
"postcode" => $clients->getPostcode(),
"state" => $clients->getState(),
"country" => $clients->getCountry(),
);
return $restService->send($params);
}
public function findAllByBusinessID( $business_id ){
$clientRepository = $this->getMapper()->getRepository( self::ENTITY_CLASS );
$clients= $clientRepository->findBy(array("bid" => $business_id));
foreach($clients as $client) {
return $client;
}
}
目前,我可以成功地检索数据并通过rest返回,但只能返回第一组(行)数据。表中有更多具有相同业务ID的项目
如何返回具有相同业务ID的所有行,而不是仅返回第一行?谢谢大家! 问题在
findAllByBusinessID
方法的循环中return
中断循环,因此只返回第一行。您想要的可能是这样的:
public function findAllByBusinessID( $business_id ){
$clientRepository = $this->getMapper()->getRepository( self::ENTITY_CLASS );
$clients= $clientRepository->findBy(array("bid" => $business_id));
$results = [];
foreach($clients as $client) {
$results['client'][] = [
"name" => $client->getName(),
"email" => $client->getEmail(),
"phone" => $client->getPhone(),
"address" => $client->getAddress(),
"suburb" => $client->getSuburb(),
"postcode" => $client->getPostcode(),
"state" => $client->getState(),
"country" => $client->getCountry(),
];
}
return $results;
}
但是您应该将此方法拆分为两个单独的函数。一个用于从数据库检索数据的函数,一个用于按您想要的方式格式化数据的函数
另一种解决方案是以数组集的形式使用和返回数据(getArrayResult()
)