Yii:更改活动记录字段名称
我是Yii的新手,我有一个表Yii:更改活动记录字段名称,yii,Yii,我是Yii的新手,我有一个表'Student',其中包含'stdStudentId','stdName'等字段。 我正在制作API,所以这些数据应该以JSON格式返回。现在,由于我希望JSON中的字段名与'id','name'类似,并且不希望返回所有字段,因此我在模型中创建了一个方法: public function APIfindByPk($id){ $student = $this->findByPk($id); return array( 'id'=>$student->
'Student'
,其中包含'stdStudentId'
,'stdName'
等字段。
我正在制作API,所以这些数据应该以JSON格式返回。现在,由于我希望JSON中的字段名与'id'
,'name'
类似,并且不希望返回所有字段,因此我在模型中创建了一个方法:
public function APIfindByPk($id){
$student = $this->findByPk($id);
return array(
'id'=>$student->stdStudentId,
'name'=>$student->stdName,
'school'=>$student->stdSchool
);
}
问题是,
stdSchool
是一种关系,在这种情况下,$student->stdSchool
返回带有schSchoolId
,schName
等字段的数组。我不希望字段的命名与JSON中的命名相同,另外,我不想返回School
中的所有字段,我想添加一些我自己的字段。有没有办法在Yii中实现这一点,或者我必须通过编写这样的方法手动完成这一点?我一直在寻找同样的方法。有一个很棒的php库,名为Fractal,可以让您实现它:
为了简单地解释lib,您为每个模型创建了一个转换器,该转换器将在您的模型属性和需要使用api公开的属性之间进行映射
class BookTransformer extends Fractal\TransformerAbstract
{
public function transform(Book $book)
{
return [
'id' => (int) $book->id,
'title' => $book->title,
'year' => $book->yr,
];
}
}
在transformer中,您还可以设置此模型具有的关系:
class BookTransformer extends TransformerAbstract
{
/**
* List of resources relations that can be used
*
* @var array
*/
protected $availableEmbeds = [
'author'
];
/**
* Turn this item object into a generic array
*
* @return array
*/
public function transform(Book $book)
{
return [
'id' => (int) $book->id,
'title' => $book->title,
'year' => $book->yr,
];
}
/**
* Here we are embeding the author of the book
* using it's own transformer
*/
public function embedAuthor(Book $book)
{
$author = $book->author;
return $this->item($author, new AuthorTransformer);
}
}
所以最后你会打电话给我
$fractal = new Fractal\Manager();
$resource = new Fractal\Resource\Collection($books, new BookTransformer);
$json = $fractal->createData($resource)->toJson();
用一个答案来描述分形的所有潜力并不容易,但你真的应该试一试。
我正在和Yii一起使用它,所以如果你有什么问题,不要犹豫 因为您是使用Yii active record从数据库获取值,所以请数据库使用列别名 普通SQL将类似于以下内容:
SELECT id AS Student_Number, name AS Student_Name, school AS School_Attending FROM student;
在Yii中,可以将条件应用于findByPK()函数。请参见此处以供参考:
请注意,为了使用这样的列别名,您必须在学生{}模型中定义一个虚拟属性Student_Number。重写ActiveRecord的populateRecord()函数可以实现这一点 我的DishType有5个属性,它覆盖了populateRecord函数Yii,当从数据库获取记录时,它将调用该函数 我的密码在这里
class DishType extends ActiveRecord
{
public $id;
public $name;
public $sort;
public $createTime;
public $updateTime;
public static function populateRecord($record, $row)
{
$pattern = ['id' => 'id', 'name' => 'name', 'sort' => 'sort', 'created_at' => 'createTime', 'updated_at' => 'updateTime'];
$columns = static::getTableSchema()->columns;
foreach ($row as $name => $value) {
$propertyName = $pattern[$name];
if (isset($pattern[$name]) && isset($columns[$name])) {
$record[$propertyName] = $columns[$name]->phpTypecast($value);
}
}
parent::populateRecord($record, $row);
}
}
class DishType extends ActiveRecord
{
public $id;
public $name;
public $sort;
public $createTime;
public $updateTime;
public static function populateRecord($record, $row)
{
$pattern = ['id' => 'id', 'name' => 'name', 'sort' => 'sort', 'created_at' => 'createTime', 'updated_at' => 'updateTime'];
$columns = static::getTableSchema()->columns;
foreach ($row as $name => $value) {
$propertyName = $pattern[$name];
if (isset($pattern[$name]) && isset($columns[$name])) {
$record[$propertyName] = $columns[$name]->phpTypecast($value);
}
}
parent::populateRecord($record, $row);
}