其实到这里已经说明问题,你的Yii2框架,为什么findOne()函数出来的,为什么id是string,但是
created_at status是int呢?
我也搞不明白,不过我可以继续说一下,下面是代码追踪
AR: yii/db/ActiveRecord.php 继承于 yii/db/BaseActiveRecord
在这个文件可以看到:findOne函数
/**
* {@inheritdoc}
* @return static|null ActiveRecord instance matching the condition, or `null` if nothing matches.
*/
public static function findOne($condition)
{
return static::findByCondition($condition)->one();
}
static::findByCondition 函数在 yii/db/ActiveRecord.php
/**
* Finds ActiveRecord instance(s) by the given condition.
* This method is internally called by [[findOne()]] and [[findAll()]].
* @param mixed $condition please refer to [[findOne()]] for the explanation of this parameter
* @return ActiveQueryInterface the newly created [[ActiveQueryInterface|ActiveQuery]] instance.
* @throws InvalidConfigException if there is no primary key defined
* @internal
*/
protected static function findByCondition($condition)
{
$query = static::find();
if (!ArrayHelper::isAssociative($condition)) {
// query by primary key
$primaryKey = static::primaryKey();
if (isset($primaryKey[0])) {
$pk = $primaryKey[0];
if (!empty($query->join) || !empty($query->joinWith)) {
$pk = static::tableName() . '.' . $pk;
}
$condition = [$pk => $condition];
} else {
throw new InvalidConfigException('"' . get_called_class() . '" must have a primary key.');
}
}
return $query->andWhere($condition);
}
后面您自己慢慢玩吧
不过,你可以通过下面的方法来处理一下int字段,进来解决这个问题
你更改一下fecshop的代码,将@fecshop/models/mysqldb/Customer.php 的方法
public static function findIdentity($id)
{
return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
}
更改为:
public static function findIdentity($id)
{
$result = static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
$result['id'] = (int)$result['id'];
return $result;
}
这样应该就可以了。进行int转化一下,如果这样没有问题,可以看到产品收藏,那说明修改好了。
当然这种直接修改源码是不好的,最好用重写的方式,下面详细和你说一下(看在你这么钻研的态度上)
1.打开@appfront/config/fecshop_local.php , return数组中加入配置
return [
'modules' => $modules,
'services' => $services,
'components' => [
'user' => [
'identityClass' => 'appfront\local\local_models\mysqldb\Customer',
],
],
];
然后新建文件 @appfront\local\local_models\mysqldb\Customer.php
这个class继承于 fecshop\models\mysqldb\Customer
, 然后覆盖方法 findIdentity()
,也就是将上面的函数赋值进去
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace appfront\local\local_models\mysqldb;
class Customer extends \fecshop\models\mysqldb\Customer
{
public static function findIdentity($id)
{
$result = static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
$result['id'] = (int)$result['id'];
return $result;
}
}
这样重写就完成了