//发起调用的
public function findLearnedCoursesByCourseIdAndUserId($courseId, $userId)
{
$that = $this; return $this->fetchCached("courseId:{$courseId}:userId:{$userId}:isLearned:1", $courseId, $userId, function ($courseId, $userId) use ($that) {
$sql = "SELECT * FROM {$that->getTable()} WHERE courseId = ? AND userId = ? AND isLearned = 1";
return $that->getConnection()->fetchAll($sql, array($courseId, $userId));
} );
}//被调用的
protected function fetchCached()
{
// ..... //
}
首先发起调用的那一段匿名函数没太看懂执行过程
然后被调用的fetchCached()并没有接受参数
请问下大神这个的执行顺序是怎样的呢?
{
$that = $this; return $this->fetchCached("courseId:{$courseId}:userId:{$userId}:isLearned:1"
, $courseId, $userId
, function ($courseId, $userId) use ($that) {
$sql = "SELECT * FROM {$that->getTable()} WHERE courseId = ? AND userId = ? AND isLearned = 1";
return $that->getConnection()->fetchAll($sql, array($courseId, $userId));
}
);
}那一部分看不懂呢?
$this->fetchCached("courseId:{$courseId}:userId:{$userId}:isLearned:1"
, $courseId, $userId
, function ($courseId, $userId) use ($that) {
$sql = "SELECT * FROM {$that->getTable()} WHERE courseId = ? AND userId = ? AND isLearned = 1";
return $that->getConnection()->fetchAll($sql, array($courseId, $userId));
}
);
1.首先fetchCached()函数并没有实参
2.$sql = "SELECT * FROM {$that->getTable()} WHERE courseId = ? AND userId = ? AND isLearned = 1"; 这一段的?参数值是对应的前面的courseId和userId吗?没有看到过这种调用方法,跟之前见到的闭包函数的使用也不太一样
3。请问有没有其他的示例类似这种调用方法的呢?
, $courseId, $userId
, function ($courseId, $userId) use ($that) {
$sql = "SELECT * FROM {$that->getTable()} WHERE courseId = ? AND userId = ? AND isLearned = 1";
return $that->getConnection()->fetchAll($sql, array($courseId, $userId));
}
);
[/code]1.首先fetchCached()函数并没有实参
2.$sql = "SELECT * FROM {$that->getTable()} WHERE courseId = ? AND userId = ? AND isLearned = 1"; 这一段的?参数值是对应的前面的courseId和userId吗?没有看到过这种调用方法,跟之前见到的闭包函数的使用也不太一样
3。请问有没有其他的示例类似这种调用方法的呢?
2、? 用于数据库的预处理
$that->getConnection()->fetchAll($sql, array($courseId, $userId));
对 $sql 做预处理后,在实施查询时带入 $courseId, $userId 两个参数