Kohana 3 Authモジュール、「スタッフ」または「マネージャー」の役割を持つユーザーを獲得する
-
30-09-2019 - |
質問
私はフレームワークを学んでおり、現在それを使用してアプリケーションを構築しています。
「ユーザー」または「スタッフ」の役割を持つすべてのユーザーを取得する必要がありますが、ドキュメントでそれについて見つけることができませんでした。
誰かを助けますか? (私はそれがよりormの問題だと思いますauthモジュール)
解決
ORMを使用してこれを行う簡単な方法は見つかりませんでしたが、回避策があります。
これは、私と同じ問題に遭遇する可能性のある人のための私のコードです。
// One for each role
$staffs = ORM::factory('role', array('name' => 'staff'))->users->find_all()->as_array();
$managers = ORM::factory('role', array('name' => 'manager'))->users->find_all()->as_array();
// Merge the results
$results = array_merge($staffs, $managers);
他のヒント
そのために別のORMメソッドを作成する必要がありますか?このコードのようなもの:
public function get_users(array $roles)
{
$users = DB::select(array($this->_has_many['roles']['foreign_key'], 'id'))
->distinct(TRUE)
->from($this->_has_many['roles']['through'])
->where($this->_has_many['roles']['far_key'], 'IN', DB::expr('('.implode(',', $roles).')'))
->execute($this->_db);
if (count($users) == 0)
{
// return empty list
return array();
}
// now we need only IDs from result
$ids = array();
foreach($users as $columns)
{
$ids[] = $columns['id'];
}
// load users by id
return $this->where($this->_primary_key, 'IN', DB::expr('('.implode(',', $ids).')'))->find_all();
}
$の役割はロール_id配列です(名前ではありません!)。詩「Where in」をクエリする方法を覚えていないので、DB式を使用します。
所属していません StackOverflow