空字符串而不是空值 Eloquent
我正在尝试使用批量分配 Eloquent 功能创建实体...
I'm trying to create entities using mass-assignment Eloquent feature...
$new = new Contact(Input::all());
$new->save();
问题在于,这样一来,每个字段都填充了一个空字符串,而不是我预期的 null
值.
The problem's that this way, every field's filled out with an empty string instead of null
values as I expected.
我目前正在开发系统,但仍有一些表列没有定义,这就是为什么使用这种方法,以避免将每个新字段添加到 $fillable
数组和 new联系人(array(...));
...
I'm currently developing the system and still some table columns're not defined, that's why using this method, to avoid adding every new field to $fillable
array and to a new Contact(array(...));
...
此外,我在这个表中有大约 20 个字段,所以有一个这样的数组有点难看
Also I've around 20 fields in this table, so It'd be a bit ugly to have an array such as
$new = new Contact(array(
'salutation' => Input::get('salutation'),
'first_name' => Input::get('first_name'),
'last_name' => Input::get('last_name'),
'company_id' => Input::get('company_id'),
'city' => ...
...
));
有关如何执行此操作或修复的任何提示?
Any tips of how to do this or fix?
更新到目前为止,我已经在 App::before()
过滤器中解决了这个问题.
Update By now I've sorted out this doing the array_filter in the App::before()
filter.
更新过滤器有点乱.我最终做了:
Update In filter was a bit mess. I end up doing:
public static function allEmptyIdsToNull()
{
$input = Input::all();
$result = preg_grep_keys ( '/_id$/' , $input );
$nulledResults = array_map(function($item) {
if (empty($item))
return null;
return $item;
}, $result);
return array_merge($input, $nulledResults);
}
在我的functions.php中.
And in my functions.php.
if ( ! function_exists('preg_grep_keys'))
{
/**
* This function gets does the same as preg_grep but applies the regex
* to the array keys instead to the array values as this last does.
* Returns an array containing only the keys that match the exp.
*
* @author Daniel Klein
*
* @param string $pattern
* @param array $input
* @param integer $flags
* @return array
*/
function preg_grep_keys($pattern, array $input, $flags = 0) {
return array_intersect_key($input, array_flip(preg_grep($pattern, array_keys($input), $flags)));
}
}
现在只使用以_id"结尾的字段.这是我最大的问题,如果关系不是NULL
,数据库会因为找不到外键"而抛出错误.
By now only working with fields that ends with "_id". This is my biggest problem as if a relationship is not NULL
, the database will throw an error as the foreign key "" cannot be found.
完美运行.有什么意见吗?
Works perfect. Any comment?
推荐答案
使用模型保存"事件查找空模型并将其显式设置为 null.在此示例中,项目"是我的模型的名称.把它放在一个辅助函数中并将它连接到你的所有模型.
Use the model 'saving' event to look for empty models and explicitly set them to null. 'Project' is the name of my model in this example. Put this in a helper function and wire it up to all your models.
Project::saving(function($model) {
foreach ($model->toArray() as $name => $value) {
if (empty($value)) {
$model->{$name} = null;
}
}
return true;
});
LARAVEL 5 更新(2016 年 4 月 11 日)
UPDATE FOR LARAVEL 5 (April 11 2016)
我最后还创建了一个 Http 中间件来清理来自 http 请求的输入数据,除了在数据发送到数据库之前清理数据.
I also ended up creating an Http Middleware to cleanup the input data from the http request, in addition to cleaning up the data before it is sent to the database.
class InputCleanup
{
/**
* Handle an incoming request.
*
* @param IlluminateHttpRequest $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$input = $request->input();
array_walk_recursive($input, function(&$value) {
if (is_string($value)) {
$value = StringHelper::trimNull($value);
}
});
$request->replace($input);
return $next($request);
}
}
相关文章