Commit 76254653 by feixiang

Dao Data生成

parent 965d4f1c
<?php
/**
* Created by PhpStorm.
* User: Argericy
* Date: 2018/7/23
* Time: 15:54
*/
namespace App\Models\Dao;
use App\Models\Entity\{entityName};
use Swoft\Bean\Annotation\Bean;
use Hdll\Services\Common\Exception\CommonException;
/**
* @Bean()
* Class {className}
* @package App\Models\Dao
*/
class {className}
{
public function create(array $info) : {entityName}
{
${entityName} = new {entityName}($info);
$res = ${entityName}->save()->getResult();
if ( $res === false ) {
throw new CommonException(["msg" => "数据库操作错误", "code" => 500]);
}
return ${entityName};
}
public function selectInfoByCondition($condition, $field)
{
return {entityName}::findAll($condition, ['fields' => $field])->getResult();
}
public function getInfoByCondition($condition, $field)
{
return {entityName}::findOne($condition, ['fields' => $field])->getResult();
}
/**
* 根据条件更新信息
* @param $condition
* @param $updateInfo
* @return mixed
*/
public function updateByCondition($condition, $updateInfo)
{
return {entityName}::updateAll($updateInfo, $condition)->getResult();
}
/**
* 更新一条记录
*
* @param $condition
* @param $updateInfo
* @return mixed
*/
public function updateOne($condition, $updateInfo)
{
return {entityName}::updateOne($updateInfo, $condition)->getResult();
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: Argericy
* Date: 2018/9/29
* Time: 11:35
*/
namespace App\Models\Data;
use App\Models\Dao\{daoName};
use Swoft\Bean\Annotation\Bean;
use Swoft\Bean\Annotation\Inject;
/**
* @Bean()
*
* Class {dataName}
* @package App\Models\Data
*/
class {dataName}
{
/**
* @Inject()
* @var {daoName}
*/
private ${daoName};
}
\ No newline at end of file
<?php
/**
* This file is part of Swoft.
*
* @link https://swoft.org
* @document https://doc.swoft.org
* @contact group@swoft.org
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/
namespace App\Commands\Common;
use Swoft\Console\Bean\Annotation\Command;
use Swoft\Console\Bean\Annotation\Mapping;
use Swoft\Console\Input\Input;
use Swoft\Console\Output\Output;
use Swoft\Task\Task;
/**
* Test command
*
* @Command(coroutine=false)
*/
class GenerateCommand
{
/**
* this generate command
*
* @Usage
* generate:dao [options]
*
* @Options
* -n,--n the dao name
* -e, --e the entity name
*
* @Example
* php swoft generate:dao -e entityName [-n daoName]
*
* @param Input $input
* @param Output $output
*
* @Mapping("dao")
*/
public function Dao(Input $input, Output $output)
{
var_dump(alias('@commonCommands'));
$className = $input->getOpt('n');
$entityName = $input->getOpt('e');
$this->generateDao($output, $className, $entityName);
}
/**
* this generate command
*
* @Usage
* generate:all [options]
*
* @Options
* -n,--n the dao name
* -e, --e the entity name
*
* @Example
* php swoft generate:dao -e entityName [-n dataName] [--dao-name daoName]
*
* @param Input $input
* @param Output $output
*
* @Mapping("all")
*/
public function all(Input $input, Output $output)
{
$dataName = $input->getOpt('n');
$entityName = $input->getOpt('e');
$daoName = $input->getOpt('dao-name');
$appPath = alias('@app');
$dataName = empty($dataName)?$entityName.'Data':$dataName;
//generateDao
$daoName = empty($daoName)?$entityName.'Dao':$daoName;
$this->generateDao($output, $daoName, $entityName);
//generateData
$dataPath = $appPath.'/Models/Data/'.$dataName.'.php';
$templatePath = $appPath.'/Commands/Common/ClassTemplate/Data/ClassTemplate';
$content = file_get_contents($templatePath);
$content = str_replace('{dataName}', $dataName, $content);
$content = str_replace("{daoName}", $daoName, $content);
file_put_contents($dataPath, $content);
}
private function generateDao($output,$daoName, $entityName)
{
if ( empty($entityName) ) {
$output->writeln("the en option is required", true, true);
}
$daoName = empty($daoName)?$entityName.'Dao':$daoName;
$daoPath = alias("@app").'/Models/Dao/'.$daoName.'.php';
$templatePath = alias("@app").'/Commands/Common/ClassTemplate/Dao/ClassTemplate';
$content = file_get_contents($templatePath);
$content = str_replace("{className}", $daoName,$content);
$content = str_replace("{entityName}", $entityName, $content);
file_put_contents($daoPath,$content);
}
/**
* this task command
*
* @Usage
* test:{command} [arguments] [options]
*
* @Options
* -o,--o this is command option
*
* @Arguments
* arg this is argument
*
* @Example
* php swoft test:task
*
* @Mapping()
*/
public function task()
{
$result = Task::deliver('sync', 'console', ['console']);
var_dump($result);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment