2017 © Pedro Peláez
 

library model-manager

a slim model manager

image

liz/model-manager

a slim model manager

  • Tuesday, January 23, 2018
  • by l396635210
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Model Manager

after make model by model-console, now use the model-manager and model-finder develop., (*1)

  • use in slim framework
    dependencies.php
...
//injection the container
$container['model_manager'] = function ($c){
    $orm = \Symfony\Component\Yaml\Yaml::parse(file_get_contents(__DIR__."/orm.yml"));
    $db = $orm['db'];
    $pdo = new PDO("mysql:host=".$db['host'] . ";dbname=".$db['dbname'].
        ";charset=".$db['charset']. ";collate=".$db['collate'],
        $db['user'], $db['pass'], ['port'=>$db['port']]);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    $modelManager=\Liz\ModelManager\ModelManager::getInstance($pdo,$orm['relations']);
    return $modelManager;
};
...

BaseController, (*2)

//for a shortcut
...
    /**
     * @var ModelManager
     */
    protected $modelManager;
...    
    public function __construct(Container $container)
    {
        ...
        $this->modelManager = $container->get('model_manager');
        ...
    }
...
  • example: shortcut for find, (*3)

    • find list
    $this->modelManager->getFinder(ClassNameToString)->findBy($condition);
    $this->modelManager->getFinder(ClassNameToString)->findAll();
    
    • find one
    $this->modelManager->getFinder(ClassNameToString)->findOneBy($condition);
    $this->modelManager->getFinder(ClassNameToString)->findOne($id);
    
  • example: insert or update
    if the model's id is a number will update else insert, (*4)

    $this->modelManager->persist($model)
            ->flush();
    
  • example: custom finder in ModelFinder.php file can custom find methods, (*5)

    // pdo for find
    public function findForIndex(Request $request, $withArr=false){
        $params = [];
        $sql = "SELECT * FROM {$this->table} AS u WHERE 1=1 ";
        if($request->getQueryParam('k')){
            $sql .= " AND (u.mobile LIKE :k OR u.realname LIKE :k)";
            $params['k'] = "%{$request->getQueryParam('k')}%";
        }
    
        $sth = $this->pdo->prepare($sql);
        if(!$withArr){
            $sth->setFetchMode(\PDO::FETCH_CLASS, $this->modelName);
        }
        $sth->execute($params);
        return $sth->fetchAll();
    }
    
    // base find class for find
    public function findOneForAPI($adCode, $createDate, $hour){
        $res = $this->findBy([
            'ad_code' => $adCode,
            'create_date' => $createDate->format('Y-m-d'),
            'hour'  => $hour,
        ]);
        return $res ? $res[0] : $res;
    }
    

The Versions

23/01 2018

dev-master

9999999-dev

a slim model manager

  Sources   Download

MIT

The Requires

  • php >=5.6

 

by liz

slim model console