f3 / Fat Free Framework:为什么 SQL-Mapper select 函数 return 来自 table 的所有列?
f3 / Fat Free Framework: why does the SQL-Mapper select function return all columns from a table?
想象一个 MySQL table 有两列,col1 和 col2,由 f3 的 SQL-Mapper 查询,如下所示:
$rows = $mapper->find();
$rows = $mapper->select('col1');
当使用 find 查询和 return 两列时,可以像这样访问它们:
... = $rows[0]->col1;
... = $rows[0]->col2;
当使用 select 调用 $rows[0]->col2 时将 return 为空,因为col2 未包含在 select 方法的字符串参数中,这很好。
现在,在执行 var_dump 时,我注意到 select 方法 return 包含所有列!这是为什么?
我想象 select 方法的目的是通过仅查询指定的列来节省数据库服务器上的资源。那么,如果 return 是完整的列集,那么 SQL-Mapper: select 方法的目的是什么?我们有 find 方法,不是吗?
Fat-Free SQL 映射器的目的是自动映射 table 列到PHP 对象属性。这是在 DB\SQL\Mapper::__construct
.
中的实例化时完成的
所以当你调用$mapper->find()
或$mapper->select()
时,实例化已经执行并且table列已经映射到$mapper
对象。
这解释了您的 var_dump
命令的结果。
现在您可以调整实际映射的列列表,但这必须在实例化时完成:
// map all columns
$mapper = new DB\SQL\Mapper($db,'table_name');
// only map col1 & col2 columns
$mapper = new DB\SQL\Mapper($db,'table_name','col1,col2');
关于select()
方法,我想知道为什么要制作这个方法public。它由 find()
在内部使用,但考虑到所有指定的字段都必须在实例化时匹配声明的列,并且计算列应该是别名和声明,因此单独使用不是很方便。参见:
$mapper = new DB\SQL\Mapper($db,'table_name','col1,col2');
// ex.1: column not declared
$results = $mapper->select('*');
echo $results[0]->col3; // undefined field col3
// ex.2a: computed column not aliased
$results = $mapper->select('SUM(col1)');
echo $results[0]->{'SUM(col1)'}; // undefined field SUM(col1)
// ex.2b: computed column aliased but not declared
$results = $mapper->select('SUM(col1) AS sum1');
echo $results[0]->sum1; // undefined field sum1
// ex.2c: computed column declared but not aliased
$mapper->sum1 = 'SUM(col1)';
$results = $mapper->select('SUM(col1)');
echo $results[0]->sum1; // empty
// ex.2d: computed column aliased and declared
$mapper->sum1 = 'SUM(col1)';
$results = $mapper->select('SUM(col1) AS sum1');
echo $results[0]->sum1; // OK!
如您所见,该方法的使用非常严格。我不建议使用它,除非你真的知道你在做什么。请改用 find()
。
想象一个 MySQL table 有两列,col1 和 col2,由 f3 的 SQL-Mapper 查询,如下所示:
$rows = $mapper->find();
$rows = $mapper->select('col1');
当使用 find 查询和 return 两列时,可以像这样访问它们:
... = $rows[0]->col1;
... = $rows[0]->col2;
当使用 select 调用 $rows[0]->col2 时将 return 为空,因为col2 未包含在 select 方法的字符串参数中,这很好。
现在,在执行 var_dump 时,我注意到 select 方法 return 包含所有列!这是为什么?
我想象 select 方法的目的是通过仅查询指定的列来节省数据库服务器上的资源。那么,如果 return 是完整的列集,那么 SQL-Mapper: select 方法的目的是什么?我们有 find 方法,不是吗?
Fat-Free SQL 映射器的目的是自动映射 table 列到PHP 对象属性。这是在 DB\SQL\Mapper::__construct
.
所以当你调用$mapper->find()
或$mapper->select()
时,实例化已经执行并且table列已经映射到$mapper
对象。
这解释了您的 var_dump
命令的结果。
现在您可以调整实际映射的列列表,但这必须在实例化时完成:
// map all columns
$mapper = new DB\SQL\Mapper($db,'table_name');
// only map col1 & col2 columns
$mapper = new DB\SQL\Mapper($db,'table_name','col1,col2');
关于select()
方法,我想知道为什么要制作这个方法public。它由 find()
在内部使用,但考虑到所有指定的字段都必须在实例化时匹配声明的列,并且计算列应该是别名和声明,因此单独使用不是很方便。参见:
$mapper = new DB\SQL\Mapper($db,'table_name','col1,col2');
// ex.1: column not declared
$results = $mapper->select('*');
echo $results[0]->col3; // undefined field col3
// ex.2a: computed column not aliased
$results = $mapper->select('SUM(col1)');
echo $results[0]->{'SUM(col1)'}; // undefined field SUM(col1)
// ex.2b: computed column aliased but not declared
$results = $mapper->select('SUM(col1) AS sum1');
echo $results[0]->sum1; // undefined field sum1
// ex.2c: computed column declared but not aliased
$mapper->sum1 = 'SUM(col1)';
$results = $mapper->select('SUM(col1)');
echo $results[0]->sum1; // empty
// ex.2d: computed column aliased and declared
$mapper->sum1 = 'SUM(col1)';
$results = $mapper->select('SUM(col1) AS sum1');
echo $results[0]->sum1; // OK!
如您所见,该方法的使用非常严格。我不建议使用它,除非你真的知道你在做什么。请改用 find()
。