cviebrock/eloquent-typecast 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cviebrock/eloquent-typecast

Composer 安装命令:

composer require cviebrock/eloquent-typecast

包简介

Trait for Eloquent models to force type-casting on retrieved values

README 文档

README

A trait that allows a Laravel project's Eloquent models to cast attribute values to native PHP variable types.

Latest Stable Version Total Downloads

Background: Why Do I Need This?

For some database drivers, all the attributes you get back from a query are returned as strings, even when the underlaying column-type is INTEGER or FLOAT or BOOLEAN.

Rather than have to use these "integer-looking" strings, etc., and rely on PHP's type-juggling, this trait will cast those attribute values to the proper native PHP variable type automagically for you.

This is also going to be very handy if you are, say, building an API and would like to just return JSON-versions of Eloquent models. Using this trait, all the JSON elements are going to be the right type for consumers of your API -- instead of all strings -- saving them type-juggling on their end.

Note: I believe if you are using the mysqlnd drivers in your PHP installation, then you don't need this trait as mysqlnd handles this type casting for you. Try it out by doing a var_dump($model->getKey()). If it shows that the value is an integer, you don't need this package. If it shows it's a string, read on.

Installation & Requirements

In your project's composer.json file:

"require": {
    "cviebrock/eloquent-typecast": "1.*"
}

In your project's models (or your own base model):

use Cviebrock\EloquentTypecast\EloquentTypecastTrait;

class MyModel {

    use EloquentTypecastTrait;

    // Define the attributes you want typecast here
    protected $cast = array(
        'id'         => 'integer',
        'price'      => 'float',
        'is_awesome' => 'boolean'
    );

    ...

}

That's it. No service providers or facades required. Because it's a trait, however, you will need to be running PHP 5.4 or later.

Usage

Anytime you request an attribute listed in the $cast array, it will be converted from the (usually) string that your database returned into a the native PHP variable type you specified.

The keys of the $cast array are the attribute (i.e. column) names, and the values are the types you want to cast to. Anything supported by PHP's settype() function is valid ... although casting to arrays, objects, or null could be problematic.

If you set the $castOnSet property on your model to true, then setting an attribute that's in the $cast array will typecast that value before setting it. For example:

class MyModel {

    use EloquentTypecastTrait;

    protected $castOnSet = true;

    protected $cast = array(
        'price'      => 'float',
    );

}

$myModel = MyModel::find(1);

$price = Input::get('price');  // this will be a string
$myModel->price = $price;      // the string is cast to a float before setting;

In general, this setting isn't really necessary as Laravel and most databases will handle the string-to-column-type conversion for you on save. However, maybe there are cases where it's useful, so it's added for "feature completion".

Notes

Because of the way the trait works, you should make sure that your $cast array does not include:

  • relations
  • attributes for which you already have a custom mutator
  • attributes using Eloquent's date mutation

$model->toArray() triggers the casting as well. $model->getAttributes(), however, does not. It returns the raw values from the query (not even the date mutation).

Bugs, Suggestions and Contributions

Please use Github for bugs, comments, suggestions.

  1. Fork the project.
  2. Create your bugfix/feature branch and write your (well-commented) code.
  3. Create unit tests for your code:
    • Run composer install --dev in the root directory to install required testing packages.
    • Add your test methods to eloquent-typecast/tests/TypecastTest.php.
    • Run vendor/bin/phpunit to the new (and all previous) tests and make sure everything passes.
  4. Commit your changes (and your tests) and push to your branch.
  5. Create a new pull request against the develop branch.

Please note that you must create your pull request against the develop branch.

Copyright and License

Eloquent-Typecast was written by Colin Viebrock and released under the MIT License. See the LICENSE.md file for details.

Copyright 2014 Colin Viebrock

cviebrock/eloquent-typecast 适用场景与选型建议

cviebrock/eloquent-typecast 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 70.76k 次下载、GitHub Stars 达 24, 最近一次更新时间为 2014 年 07 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「json」 「laravel」 「mutator」 「eloquent」 「integer」 「boolean」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 cviebrock/eloquent-typecast 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cviebrock/eloquent-typecast 我们能提供哪些服务?
定制开发 / 二次开发

基于 cviebrock/eloquent-typecast 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 70.76k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 26
  • 点击次数: 45
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 24
  • Watchers: 4
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-07-12