Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
huangzhhui committed Jun 13, 2018
1 parent 2d1c652 commit 60f9e56
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/db/src/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Swoft\Db\Exception\MysqlException;
use Swoft\Db\Validator\ValidatorInterface;
use Swoft\Exception\ValidatorException;
use Swoft\Helper\StringHelper;

/**
* Executor
Expand All @@ -36,7 +37,13 @@ public static function save($entity): ResultInterface

$fields = $fields ?? [];
$query = Query::table($table)->selectInstance($instance);

// Set Primary Id to Entity
$query->addDecorator(function ($primaryId) use ($entity, $className) {
list(, , $idColumn) = self::getTable($className);
$setter = 'set' . StringHelper::camel($idColumn, false);
method_exists($entity, $setter) && $entity->$setter($primaryId);
return $primaryId;
});
return $query->insert($fields);
}

Expand Down
5 changes: 3 additions & 2 deletions src/framework/src/Helper/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,16 @@ public static function ascii($value)
* Convert a value to camel case.
*
* @param string $value
* @param bool $lcfirst
* @return string
*/
public static function camel($value)
public static function camel($value, $lcfirst = true)
{
if (isset(static::$camelCache[$value])) {
return static::$camelCache[$value];
}

return static::$camelCache[$value] = lcfirst(static::studly($value));
return static::$camelCache[$value] = ($lcfirst ? lcfirst(static::studly($value)) : static::studly($value));
}

/**
Expand Down

0 comments on commit 60f9e56

Please sign in to comment.