protected function synColumns($entities) { $this->info('---------------------开始同步表结构----------------------------'); foreach ($entities as $entity_name => $re) { $this->info('同步Entity:<keyword>' . $entity_name . '</keyword>'); $table_name = $re->getMethod('getTableName')->invoke(null); $table = new Table($table_name); $attrs = $re->getMethod('getAttributes')->invoke(null); foreach ($attrs as $field => $info) { $attr = $info; $attr['type'] = Type::get($info['type'])->setExtra($info['extra'] ?? [])->toDbType(); $table->setField($field, $attr); } $table->setPrimaryKey($re->getStaticPropertyValue('primary')); $unique = $re->getMethod('getUnique')->invoke(null); if (is_array($unique)) { $the_unique = []; foreach ($unique as $key => $config) { $the_unique[$key . '_' . $table_name . '_uk'] = $config; } $table->setUniqueKeys($the_unique); } $table->save(); } }
public function validate($val, $rules) { $type = Type::get($rules['type'])->setExtra($rules['extra'] ?? []); if (isset($rules['required'])) { $type->setRequired($rules['required']); } if (isset($rules['allow_empty'])) { $type->setAllowEmpty($rules['allow_empty']); } return $type->check($val); }
public function toArray() { $ret = []; foreach ($this->data as $field => $data) { $attr = $this->config[$field]; $type = Type::get($attr['type']); if ($attr['sensitive'] ?? false) { continue; } $ret[$field] = $type->toDB($data['value']); } return $ret; }
/** * 获取从前端传上来的参数 * @param array rules = [ * 'hello', // 不验证可用性 * 'world', // 不验证可用性 * 'hell' => ['type' => 'string'], // 通过规则检查其可用性 * 'password'=> ['type' => 'password'] // 通过规则检查其可用性 * ]; */ protected function inputs($rules = null) { $source = $this->getInputSource(); if ($rules === null) { return $source; } $ret = []; foreach ($rules as $k => $rule) { if (is_int($k)) { $ret[$rule] = $source[$rule] ?? null; continue; } $type = Type::get($rule['type'])->setValueName($k)->setExtra($rule['extra'] ?? []); if (isset($rule['required'])) { $type->setRequired($rule['required']); } if (isset($rule['allow_empty'])) { $type->setAllowEmpty($rule['allow_empty']); } try { $type->check($source[$k]); } catch (\Exception $e) { $message = $rule['message'] ?? $e->getMessage(); throw new \Leno\Http\Exception(400, $message); } $ret[$k] = $source[$k] ?? null; } return $ret; }
/** * 从数组中获取值,生成Entity, 该方法会假设row是从数据库查询 * 的结果,请不要直接使用该方法,通过Entity::selector()->find() * 的方式,它会自动将row包装称Entity * * 有时候我们需要关联其他表获取某些字段,我们允许将这些字段附带到这个entity上面 * 这样我们能够获取到更多的灵活性 * * ###example * * ArticleEntity 拥有属性 * title content * * PublishedArticleEntity 拥有属性 * atl_id like_num * *```php * $article = PublishedArticleEntity::find('xx')->getArticle(function($selector) { * $published_selector = PublishedArticleEntity::Selector() * ->feildLikeNum('like_number') // 通过left join抓取like_num字段的值 * ->onAtlId($selector->getFeildExpr('atl_id')); * return $selector->join($published_selector); * }); * * $total_like_number = $article->getLikeNumber(); // 获取PublishedArticleEntity的属性值 *``` * * 这样我们可以通过标准的get方法取值,我们把这种通过left join抓取的值称之为伪属性 * * @param array row 从数据库中查询出来的row信息 * * @return Entity */ public static function newFromDB(array $row) { $self = get_called_class(); $entity = new $self(false); $attrs = $self::getAttributes(); foreach ($row as $field => $value) { if ($attrs[$field] ?? false) { $value = Type::get($attrs[$field]['type'])->toPHP($value); } $entity->setForcely($field, $value, false); } $entity_pool = EntityPool::instance(); $key = $entity_pool->getKey($self::getTableName(), $entity->id()); $entity_pool->set($key, $entity); return $entity; }