我有一个包含以下列的表: id,externalId,firstname,lastname,email
我的网域:
//............................
if ($resultado === null) {
} else {
//get mail info from your local database (mysql) using current one
//if(you get null then send email otherwise you already have sent email)
$enviaremail = mail($destino, $assunto, $arquivo, $headers);
//save this mail info to DB
}
//...............................
我的服务:
class Employee {
int id
String externalId
static mapping = {
table "Employee"
id column: "Id"
externalId column: "externalId"
version false
}
}
日志中的错误消息说:
class EmployeeService {
def getEmployee(externalId) {
def employee = new Employee();
employee.findAllByExternalId("1234");
return employee
}
我的目标是加载一个Employee实例,其中externalId与我给它的匹配。我做错了什么?
谢谢!
答案 0 :(得分:1)
你知道所有这些都有很多错误
//Create an instance of Employee which currently holds nothing
def employee = new Employee();
//With this empty object of Employ now do findAllByExternalId
employee.findAllByExternalId("1234");
你应该抬头
//This will provide you with the entire employee
// domain class any it finds with that externalId.
// since it is declared as findAll
// the return will naturally be a list even though there may only be 1
def employees = Employee?.findAllByExternalId("1234")
但这是非常漫长的啰嗦你想要做什么验证它存在或返回所有具有该ID的员工的列表?你把它称为getEmployee,所以我假设你正在尝试找到一个,但你正在通过findAll寻找迭代
// If you wanted just to check it exists return a boolean like this
//This will return result as a boolean
boolean getEmployee(externalIdd) {
return Employee.where { externalId == externalIdd }.exists()
}
//if you wanted purely employee entire object bound to first records
Employee getEmployee(externalIdd) {
return Employee.where { externalId == externalIdd }?.find()
}
//if you wanted a list of entire employees like the findAll above
List<Employee> getEmployee(externalIdd) {
return Employee.where { externalId == externalIdd }?.findAll()
}
是一些替代方法,并且可能不那么强烈的数据库取决于您正在尝试实现的特定布尔值与您当前的方法
我也打电话给externalIdd
,因为它.where
有时候变量名称与被调用的名称相同会导致问题因此发生变化
E2A
根据你的评论,如果你只需要id,那么id通常是Long因此Long
的严格定义你可以使用def
- def更通用并且将返回任何内容。我已经改进了只包含id
属性的位置,所以如果它找到记录,它将返回id然后?:0L
如果没有找到返回0 L
如此长,所以返回零长你可以替换使用?:null
或根本不声明它。
//if you only want to get the id as per comment
Long getEmployeeId(String externalIdd) {
return (Employee.where{externalId == externalIdd}.property('id')?\
.find()?:0L)
// Another way close to your findBy but manually declared
// and only returning id field.
// return Employee.find{externalId==externalIdd}?.id
}
答案 1 :(得分:0)
您应该在类上调用findAllBy_()
,而不是实例。此外,由于您只返回一个Employee
而不是列表,因此您应该使用findBy_()
。鉴于此,您的服务方法应如下所示:
def getEmployee(String externalId){
return Employee.findByExternalId(exteranlId)
}
答案 2 :(得分:0)
您不能以Id结尾的财产。
myInstance.propertyId means get the property "Property" and its Id.
我想在动态查找器生成过程中出现了问题 -