laravel问题与mutator [英] laravel problems with mutators
问题描述
我有一个文章模型,访问一个名为文章的表格。
我已经设置了以下变量的模型:
class Article extends Enloquent {
public function getArticleDateAttribute($ value)
{
返回日期('d / m / Y',strtotime($ value));
}
public function getValidUntilAttribute($ value)
{
返回日期('d / m / Y',strtotime($ value));
}
}
现在,当我查询数据库时以下和删除mutator一切正常工作,我得到我预期的数据:
public function getTest(){
$ data =文章:: select(array(
'articles.id',
'articles.article_date',
'articles.image_link',
'article.headline',
'articles.category'
)) - > get()
- > toArray();
var_dump($ data);
// return View :: make('_ layouts.master');
}
在我的测试中,我得到如下样本所期望的结果:
array(size = 5)
'id'=> int 3
'article_date'=> string'2008-06-03 00:00:00'(length = 19)
'image_link'=> string''(length = 0)
'headline'=>字符串'Sussex业余课程关闭'(长度= 29)
'category'=> int 6
现在,当我添加了这些mutators,具体的查询我得到以下数据:
array(size = 6)
'article_date'=> string '03 / 06/2008'(length = 10)
'valid_until'=> string '01 / 01/1970'(length = 10)
'id'=> int 3
'image_link'=> string''(length = 0)
'headline'=>字符串'Sussex业余课程关闭'(长度= 29)
'category'=> int 6
列顺序已更改,它包含一个我最初没有请求的列。我应该如何正确地实现mutator,为什么列改变?
我误解了吗?
谢谢
Ray
mutator将被调用,因为代码是建立这样。请参阅Eloquent Model类中的这个函数的实现(由 toArray()调用)
):
/ **
*将模型的属性转换为数组。
*
* @return array
* /
public function attributesToArray()
{
$ attributes = $ this-> getAccessibleAttributes();
//我们要旋转这个模型的所有突变属性,并调用
//属性的mutator。我们缓存每个突变的属性,所以
//我们不必经常检查实际上改变的属性。
foreach($ this-> getMutatedAttributes()as $ key)
{
if(!array_key_exists($ key,$ attributes))继续;
$ attributes [$ key] = $ this-> mutateAttribute($ key,$ attributes [$ key]);
}
返回$属性;
}
https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Eloquent/Model.php
My journey into laravel 4 (from laravel 3) continues....
I have an Article model, accessing a table called articles.
I have set up the model with the following mutators:
class Article extends Eloquent {
public function getArticleDateAttribute($value)
{
return date('d/m/Y', strtotime($value));
}
public function getValidUntilAttribute($value)
{
return date('d/m/Y', strtotime($value));
}
}
Now when I query the database with the following AND Delete the mutators everything works as expected and I get the data I expect:
public function getTest() {
$data = Article::select(array(
'articles.id',
'articles.article_date',
'articles.image_link',
'articles.headline',
'articles.category'
)) ->get()
->toArray();
var_dump($data);
//return View::make('_layouts.master');
}
In my test I get the results as expected as this sample:
array (size=5)
'id' => int 3
'article_date' => string '2008-06-03 00:00:00' (length=19)
'image_link' => string '' (length=0)
'headline' => string 'Sussex Amateur Course Closure' (length=29)
'category' => int 6
Now, when I add back the mutators, with the exact query I get the following data:
array (size=6)
'article_date' => string '03/06/2008' (length=10)
'valid_until' => string '01/01/1970' (length=10)
'id' => int 3
'image_link' => string '' (length=0)
'headline' => string 'Sussex Amateur Course Closure' (length=29)
'category' => int 6
the column order is changed and it's included a column I didn't originally request. How should I correctly implement mutators and why do the columns change?
Have I misunderstood this?
Thanks
Ray
The mutators will be called, because the code is built that way. See the implementation of this function in the Eloquent Model class (which is called by toArray()
):
/**
* Convert the model's attributes to an array.
*
* @return array
*/
public function attributesToArray()
{
$attributes = $this->getAccessibleAttributes();
// We want to spin through all the mutated attributes for this model and call
// the mutator for the attribute. We cache off every mutated attributes so
// we don't have to constantly check on attributes that actually change.
foreach ($this->getMutatedAttributes() as $key)
{
if ( ! array_key_exists($key, $attributes)) continue;
$attributes[$key] = $this->mutateAttribute($key, $attributes[$key]);
}
return $attributes;
}
https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Eloquent/Model.php
这篇关于laravel问题与mutator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!