使用模型Crud中的变量和具有Closure的高级用法

时间:2012-11-06 16:01:20

标签: php fuelphp

echo $slot->id;
$slot->players = (Model_Players::find(function ($query)
{
    return $query->join('users')
        ->on('players.player', '=', 'users.id')
        ->where('players.slot', $slot->id);
})) ?: array();

返回:

4

ErrorException [ Notice ]: Undefined variable: slot  
APPPATH/classes/controller/booking.php @ line 98:  
98: ->where('players.slot', $slot->id);

如何将$slot对象发送到函数中?

2 个答案:

答案 0 :(得分:2)

您必须在查询中“使用”$ slot变量

echo $slot->id;
$slot->players = (Model_Players::find(function ($query) use ($slot)
{
    return $query->join('users')
        ->on('players.player', '=', 'users.id')
        ->where('players.slot', $slot->id);
})) ?: array();

答案 1 :(得分:1)

需要使用use

Model_Players::find(function ($query) use ($slot) 

干杯。