laravel地图模型属性

时间:2015-01-15 08:17:15

标签: php laravel query-builder

使用$queryBuilder->get()方法时,laravel是否可以检索模型属性?

我的数据库有奇怪的命名约定(旧应用),我为每个字段编写了一个get*Attribute()方法,因此我可以通过$model->prettyName而不是$model->weird_Convention_Name来访问它,但是当我使用$queryBuilder->get()时,我得到数据库字段,而不是模型属性。

有没有办法做到这一点?

修改

这是一段代码示例:

模型

<?php

namespace Model\Day;
use Illuminate\Database\Eloquent\Builder;

/**
 * Class Incomplete
 * @package Model
 * @subpackage Day
 * @property string $alarm
 * @property string $status
 * @property string $mark1
 * @property int $dayRate
 * @property bool $isHalfDay
 * @property bool $noPause
 * @property bool $isTracked
 * @property bool $noTime
 * @property string $dayType
 * @property string $shortDay
 * @property \DateTime $date
 * @property \DateTime $startDay
 * @property \DateTime $startBreak
 * @property \DateTime $endBreak
 * @property \DateTime $startBreak2
 * @property \DateTime $endBreak2
 * @property \DateTime $endDay
 * @property string $statusDescription
 * @property string $subtype
 * @property string $subcode
 * @property string $description
 * @property int $locationId
 * @property string $locationDes
 *
 * @method \Eloquent currentUser()
 */
class Incomplete extends \Eloquent
{
    /**
     * The database table used by the model.
     * @var string
     */
    protected $table = 'vlogger_pending';

    /**
     * The primary key for the model.
     * @var string
     */
    protected $primaryKey = null;

    /**
     * The sequence name used for incrementing
     * @var string
     */
    protected $sequence = null;

    /**
     * Indicates if the IDs are auto-incrementing.
     * @var bool
     */
    public $incrementing = false;

    /**
     * Indicates if the model should be timestamped.
     * @var bool
     */
    public $timestamps = false;

    /***********************************************************************
     * SCOPES
     ***********************************************************************/

    /**
     * Add a where statement for current logged in user
     * @param Builder $query
     * @return Builder
     */
    public function scopeCurrentUser(Builder $query)
    {
        return $query->where('usr_id', '=', \Auth::user()->id);
    }

    /***********************************************************************
     * ACCESSORS
     ***********************************************************************/

    public function getAlarmAttribute()
    {
        return $this->attributes['alarm'];
    }

    public function getStatusAttribute()
    {
        return $this->attributes['status'];
    }

    public function getMark1Attribute()
    {
        return $this->attributes['mark1'];
    }

    public function getDayRateAttribute()
    {
        return (int)$this->attributes['day_rate'];
    }

    public function getIsHalfDayAttribute()
    {
        return !!$this->attributes['half_day'];
    }

    public function getNoPauseAttribute()
    {
        return !!$this->attributes['no_pause'];
    }

    public function getIsTrackedAttribute()
    {
        return !!$this->attributes['tracked'];
    }

    public function getNoTimeAttribute()
    {
        return !!$this->attributes['no_hours'];
    }

    public function getDayTypeAttribute()
    {
        return $this->attributes['day_type'];
    }

    public function getShortDayAttribute()
    {
        return $this->attributes['dd'];
    }

    public function getDateAttribute()
    {
        $date = $this->attributes['day'];
        if (null !== $date) {
            $date = \DateTime::createFromFormat('U', strtotime($date));
        }
        return $date;
    }

    public function getStartDayAttribute()
    {
        $date = $this->attributes['dstart'];
        if (null !== $date) {
            $date = \DateTime::createFromFormat('U', strtotime($date));
        }
        return $date;
    }

    public function getStartBreakAttribute()
    {
        $date = $this->attributes['pstart'];
        if (null !== $date) {
            $date = \DateTime::createFromFormat('U', strtotime($date));
        }
        return $date;
    }

    public function getEndBreakAttribute()
    {
        $date = $this->attributes['pend'];
        if (null !== $date) {
            $date = \DateTime::createFromFormat('U', strtotime($date));
        }
        return $date;
    }

    public function getStartBreak2Attribute()
    {
        $date = $this->attributes['s_pstart2'];
        if (null !== $date) {
            $date = \DateTime::createFromFormat('U', strtotime($date));
        }
        return $date;
    }

    public function getEndBreak2Attribute()
    {
        $date = $this->attributes['s_pend2'];
        if (null !== $date) {
            $date = \DateTime::createFromFormat('U', strtotime($date));
        }
        return $date;
    }

    public function getEndDayAttribute()
    {
        $date = $this->attributes['dend'];
        if (null !== $date) {
            $date = \DateTime::createFromFormat('U', strtotime($date));
        }
        return $date;
    }

    public function getStatusDescriptionAttribute()
    {
        return $this->attributes['status_des'];
    }

    public function getSubtypeAttribute()
    {
        return $this->attributes['sub_type'];
    }

    public function getSubcodeAttribute()
    {
        return $this->attributes['sub_code'];
    }

    public function getDescriptionAttribute()
    {
        return $this->attributes['des'];
    }

    public function getLocationIdAttribute()
    {
        return (int)$this->attributes['location'];
    }

    public function getLocationDesAttribute()
    {
        return $this->attributes['location_des'];
    }

    /***********************************************************************
     * MUTATORS
     ***********************************************************************/

    public function setAlarmAttribute($value)
    {
        $this->attributes['alarm'] = $value;
        return $this;
    }

    public function setStatusAttribute($value)
    {
        $this->attributes['status'] = $value;
        return $this;
    }

    public function setMark1Attribute($value)
    {
        $this->attributes['mark1'] = $value;
        return $this;
    }

    public function setDayRateAttribute($value)
    {
        $this->attributes['day_rate'] = $value;
        return $this;
    }

    public function setIsHalfDayAttribute($value)
    {
        $this->attributes['half_day'] = $value;
        return $this;
    }

    public function setNoPauseAttribute($value)
    {
        $this->attributes['no_pause'] = $value;
        return $this;
    }

    public function setIsTrackedAttribute($value)
    {
        $this->attributes['tracked'] = $value;
        return $this;
    }

    public function setNoTimeAttribute($value)
    {
        $this->attributes['no_hours'] = $value;
        return $this;
    }

    public function setDayTypeAttribute($value)
    {
        $this->attributes['day_type'] = $value;
        return $this;
    }

    public function setShortDayAttribute($value)
    {
        $this->attributes['dd'] = $value;
        return $this;
    }

    public function setDateAttribute($value)
    {
        $this->attributes['day'] = $value;
        return $this;
    }

    public function setStartDayAttribute($value)
    {
        $this->attributes['dstart'] = $value;
        return $this;
    }

    public function setStartBreakAttribute($value)
    {
        $this->attributes['pstart'] = $value;
        return $this;
    }

    public function setEndBreakAttribute($value)
    {
        $this->attributes['pend'] = $value;
        return $this;
    }

    public function setStartBreak2Attribute($value)
    {
        $this->attributes['s_pstart2'] = $value;
        return $this;
    }

    public function setEndBreak2Attribute($value)
    {
        $this->attributes['s_pend2'] = $value;
        return $this;
    }

    public function setEndDayAttribute($value)
    {
        $this->attributes['dend'] = $value;
        return $this;
    }

    public function setStatusDescriptionAttribute($value)
    {
        $this->attributes['status_des'] = $value;
        return $this;
    }

    public function setSubtypeAttribute($value)
    {
        $this->attributes['sub_type'] = $value;
        return $this;
    }

    public function setSubcodeAttribute($value)
    {
        $this->attributes['sub_code'] = $value;
        return $this;
    }

    public function setDescriptionAttribute($value)
    {
        $this->attributes['des'] = $value;
        return $this;
    }

    public function setLocationIdAttribute($value)
    {
        $this->attributes['location'] = $value;
        return $this;
    }

    public function setLocationDesAttribute($value)
    {
        $this->attributes['location_des'] = $value;
        return $this;
    }
}

控制器

   /**
     * Get pending report for current logged user
     * @return \Illuminate\Http\JsonResponse
     */
    public function pendingReport()
    {
        return Response::json(Day\Incomplete::currentUser()->get());
    }

2 个答案:

答案 0 :(得分:0)

将模型中的$ snakeAttributes设置为false以使用prettyName。 并且在将数据转换为json时使用$ appends添加格式化属性。

class MyModel extends Eloquent {
    public static $snakeAttributes = false;
    protected $hidden = array('oldAttribute1', 'oldAttribute2');
    protected $appends = array('attribute1', 'attribute2');

    public function getAttribute1Attribute()
    {
        return 'Attribute1 Value';
    }

    public function getAttribute2Attribute()
    {
        return 'Attribute2 Value';
    }
}

答案 1 :(得分:0)

你可以使用&#34; magick&#34; __get():

private $aliases = array('
    'prettyName' => 'weird_Convention_Name',
    //...
');

public function __get($key)
{
    if (!empty($this->aliases[$key]))
        return parent::__get($this->aliases[$key]);
    else
        return parent::__get($key);
}

并始终在您使用$model->prettyName时返回$model->weird_Convention_Name