sql\mapper fatfree 中的 cast() 是做什么的?
What does cast() do in sql\mapper fatfree?
我偶然发现了这个代码片段并且对 cast() 函数感到困惑?它有什么作用?
$userModel = new UserModel();
$record = $userModel->findone(array('id=?', $uid));
$f3->set('SESSION.deleteUser', $uid);
$f3->set('user', $record->cast());
$f3->set('content', 'user/delete.php');
$template = new \View;
echo $template->render('dashboard/layout.php');
它将 Mapper
对象(又名 "casts")转换为关联对象 array
。
投射前:
$userModel->load(['id=?',123]);
echo $userModel->id; // 123
echo $userModel->name; // John Doe
echo $userModel->country; // Botswana
print_r($userModel); // List of object properties (including inherited)
施法后:
print_r($userModel->cast());
/*
Array
(
[id] => 123
[name] => John Doe
[country] => Botswana
)
*/
当您想将映射器的只读副本传递给模板时,转换很有用(又名 "separation of concerns")。它还有助于减少内存使用
加载大量记录时。
我偶然发现了这个代码片段并且对 cast() 函数感到困惑?它有什么作用?
$userModel = new UserModel();
$record = $userModel->findone(array('id=?', $uid));
$f3->set('SESSION.deleteUser', $uid);
$f3->set('user', $record->cast());
$f3->set('content', 'user/delete.php');
$template = new \View;
echo $template->render('dashboard/layout.php');
它将 Mapper
对象(又名 "casts")转换为关联对象 array
。
投射前:
$userModel->load(['id=?',123]);
echo $userModel->id; // 123
echo $userModel->name; // John Doe
echo $userModel->country; // Botswana
print_r($userModel); // List of object properties (including inherited)
施法后:
print_r($userModel->cast());
/*
Array
(
[id] => 123
[name] => John Doe
[country] => Botswana
)
*/
当您想将映射器的只读副本传递给模板时,转换很有用(又名 "separation of concerns")。它还有助于减少内存使用 加载大量记录时。