gpt4 book ai didi

php - 自动将 MySQL 位字段映射到 bool 属性

转载 作者:行者123 更新时间:2023-11-29 00:17:57 25 4
gpt4 key购买 nike

假设我有几个模型使用 MySQL 的位字段存储一些标志。我知道可以通过执行以下操作轻松地将其转换为 bool 值:

$myBoolFlag = (ord($model->myFlag) == 1) ? true : false;

我正在寻找某种方法使其自动映射到 bool 属性。

请有人告诉我,有比为我的项目中的每个位字段创建 setter 和 getter 更好的方法。我敢打赌 Phalcon 在数据库服务或类似的东西中有一些神奇的配置......

最佳答案

我处理类似问题的方法是在每个模型中创建 map 数组并使用它们。更具体地说:

<?php
/**
* Model.php
*
* Model
*
* @author Nikos Dimopoulos <nikos@niden.net>
* @since 2012-12-12
* @category Library
*
*/

namespace NDN;

use \Phalcon\DI\FactoryDefault as PhDi;
use \Phalcon\Mvc\Model as PhModel;

class Model extends PhModel
{
private $meta = [];

/**
* Some init stuff
*/
public function initialize()
{

// Disable literals
$this->setup(['phqlLiterals' => false]);
}

/**
* Universal method caller. This checks the available methods based on
* the fields in the meta array and returns the relevant results
*
* @author Nikos Dimopoulos <nikos@niden.net>
* @since 2015-02-15
*
* @param string $function
* @param array|null $arguments
*
* @return mixed|void
* @throws \Exception
*/
public function __call($function, $arguments = null)
{
// $function is something like getId, setId, getName etc.
$metaFunction = substr($function, 3);
$field = $this->getMetaFunctionToField($metaFunction);

if ($field) {

$prefix = substr($function, 0, 3);
$fieldName = $field['field'];

switch ($prefix) {
case 'get':

/**
* Data manipulation here if needed
*/
$value = $this->getField($fieldName);
$value = $this->metaFieldValidate($field, $value);

return $value;
break;

case 'set':

/**
* Data manipulation here
*/
$value = $this->metaFieldValidate($field, $arguments);

$this->setField($field, $value);
break;
}

} else {
throw new \Exception('Function does not exist');
}
}

/**
* -------------------------------------------------------------------------
* PROTECTED METHODS
* -------------------------------------------------------------------------
*/

/**
* Gets a field from the model with the correct prefix
*
* @param $name
*
* @return mixed
*/
protected function getField($name)
{
return $this->$name;

}

/**
* Sets a field in the model
*
* @author Nikos Dimopoulos <nikos@niden.net>
* @since 2014-02-15
*
* @param string $field
* @param mixed $value
*/
protected function setField($field, $value)
{
$this->$field = $value;
}

/**
* Returns the DI container
*
* @author Nikos Dimopoulos <nikos@niden.net>
* @since 2014-02-22
*
* @return mixed
*/
public function getDI()
{
return PhDi::getDefault();
}

/**
* Accesses the internal array map to provide the field name from a function
*
* @author Nikos Dimopoulos <nikos@niden.net>
* @since 2014-02-27
*
* @param string $prefix The prefix of the table
* @param string $function The aliased function
*
* @return string The field name (i.e. tnt_id)
* bool False if not found
*/
public function getMetaFunctionToField($function)
{
if (array_key_exists($function, $this->meta)) {
return $this->meta[$function];
}

return false;
}

/**
* Validates a setter value based on each field's type
*
* @author Nikos Dimopoulos <nikos@niden.net>
* @since 2014-02-17
*
* @param string $field The field to check
* @param mixed $value The value of the field
*
* @return bool|int|string
*/
protected function metaFieldValidate($field, $value)
{
// Find the validator
$validator = $field['validator'];

switch ($validator)
{
case 'int':
$return = intval($value);
break;
case 'bit':
$return = (ord($value) == 1) ? true : false;
break;
case 'bool':
$return = (bool) $value;
break;
case 'decimal':
$return = (float) $value;
break;
case 'string':
$return = (string) $value;
break;
case 'datetime':
/**
* @todo check datetime validator
*/
$return = (string) $value;
break;
default:
$return = $value;
break;
}

return $return;
}

}

示例 User 模型如下所示

<?php
/**
* User.php
*
* User
*
* @author Nikos Dimopoulos <nikos@niden.net>
* @since 2014-03-08
* @category Models
*
*/

namespace NDN;

use \NDN\Model as NDNModel;

class Model extends NDNModel
{
public function initialize()
{
/**
* This is where I will set the field map
*
* The key of the array is the function name without
* the prefix. So for instance if you want getName()
* to return the user.name you use Name as the key
*/
$this->data = [
'Id' => [
'field' => 'user_id',
'validator' => 'int',
],
'Name' => [
'field' => 'user_name',
'validator' => 'int',
],
'IsMarried' => [
'field' => 'user_is_married',
'validator' => 'bit',
]
];

parent::initialize();
}
}

关于php - 自动将 MySQL 位字段映射到 bool 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22260410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com