Sugarcrm支持产品指南糖开发人员糖开发人员指南10.0数据框架数据库DBManager.

DBManager.

概述

The DBManager. Object provides an interface for working with the database. As of Sugar 7.9, there are some deprecated methods that have been removed from the system that are outlined in the Release Notes.

实例化DBManager对象

The DBManager.Factory class, located in ./include/database/DBManagerFactory.php, can help instantiate a DBManager. object using the getInstance() method.

$db = \DBManagerFactory::getInstance();

For best practices, we recommend using the global DBManager. Object:

$GLOBALS['db']

3d彩吧论坛数据库

含糖7.9,有支持准备的陈述。以下部分概述了遗留用法和新的准备好的声明使用情况。

选择3d彩吧论坛

For select queries that do not have a dynamic portion of the where clause, you can use the query() method on the DBManager object. For queries that are accepting data passed into the system in the where clause, the following examples demonstrate how best utilize the new Prepared Statement functionality.

遗产:

$id = '1234-abcde-fgh45-6789';
$query = 'SELECT * FROM accounts WHERE id = ' . $GLOBALS['db']->quoted($id);
$results = $GLOBALS['db']->query($query);

最佳实践:

Use the getConnection() method to retrieve a Doctrine Connection Object which handles prepared statements.

$id = '1234-abcde-fgh45-6789';
$query = 'SELECT * FROM accounts WHERE id = ?';
$conn = $GLOBALS['db']->getConnection(); 
$stmt = $conn->executeQuery($query, array($id));

在3d彩吧论坛逻辑变量或有条件构建的情况下,直接使用Doctrine QueryBuilder有意义。

遗产:

$query = 'SELECT * FROM accounts'; 
if ($status !== null) {
	$query .= ' WHERE status = ' . $GLOBALS['db']->quoted($status);
}
$results = $GLOBALS['db']->query($query);

最佳实践:

Use the getConnection() method to retrieve the Doctrine Connection Object, and then use the createQueryBuilder() method on the Connection Object to retrieve the QueryBuilder Object.

$builder = $GLOBALS['db']->getConnection()->createQueryBuilder();
$builder->select('*')->from('accounts');
if ($status !== null) {
	$builder->where('status = ' . $builder->createPositionalParameter($status)));
}
$stmt = $builder->execute();

检索结果

遗产:

After using the query() method, such as in the Legacy code examples above, you can use the fetchByAssoc() method to retrieve results. The query() method will submit the query and retrieve the results while the fetchByAssoc() method will iterate through the results:

$sql = "SELECT id FROM accounts WHERE deleted = 0";
$result = $GLOBALS['db']->query($sql);

while($row = $GLOBALS['db']->fetchByAssoc($result) )
{
    //Use $row['id'] to grab the id fields value
    $id = $row['id'];
}

最佳实践:

使用准备好语句时,Doctrine3d彩吧论坛Builder和Doctrine Connection对象都将返回一个 Doctrine\DBAL\Portability\Statement Object to allow iterating through the results of the query. You can use the fetch() or fetchAll() methods to retrieve results.

fetchall()示例

The fetchAll() method will return the entire result set as an array, with each index containing a row of data.

$id = '1234-abcde-fgh45-6789';
$query = 'SELECT * FROM accounts WHERE id = ?';
$conn = $GLOBALS['db']->getConnection();
$stmt = $conn->executeQuery($query, array($id));
foreach($stmt->fetchAll() as $row){
	$id = $row['id']
	//do other stuff...
}
fetch()示例

The fetch() method will return the next index in the result set.

$id = '1234-abcde-fgh45-6789';
$query = 'SELECT * FROM accounts WHERE id = ?';
$conn = $GLOBALS['db']->getConnection();
$stmt = $conn->executeQuery($query, array($id));
while($row = $stmt->fetch()){
	$id = $row['id']
	//do other stuff...
}

检索单一结果

To retrieve a single result from the database, such as a specific record field, you can use the getOne() method for Legacy query usage.

$sql = "SELECT name FROM accounts WHERE id = '{$id}'";
$name = $GLOBALS['db']->getOne($sql);

限制结果

To limit the results of a query, you can add a limit to the SQL string or for legacy query usage you can use the limitQuery() method on the DBManager Object:

遗产:
$sql = "SELECT id FROM accounts WHERE deleted = 0";
$offset = 0;
$limit = 1;

$result = $GLOBALS['db']->limitQuery($sql, $offset, $limit);

while($row = $GLOBALS['db']->fetchByAssoc($result) )
{
    //Use $row['id'] to grab the id fields value
    $id = $row['id'];
}
准备的陈述:

When using the Doctrine Query Builder, you can limit the results of the query by using the setMaxResults() method.

$builder = $GLOBALS['db']->getConnection()->createQueryBuilder();
$builder->select('*')->from('accounts');
if ($status !== null) {
	$builder->where('status = ' . $builder->createPositionalParameter($status))); 
}
$builder->setMaxResults(2);
$stmt = $builder->execute();

插入3d彩吧论坛

可以使用DBManager类轻松执行INSERT3d彩吧论坛。

遗产:

$query = 'INSERT INTO table (foo, bar) VALUES ("foo", "bar")';
$GLOBALS['db']->query($query);

最佳实践:

$fieldDefs = $GLOBALS['dictionary']['table']['fields'];
$GLOBALS['db']->insertParams('table', $fieldDefs, array('foo' => 'foo','bar' => 'bar'));

更新3d彩吧论坛

在使用已知ID的记录或具有简单过滤条件的一组记录时,可以使用DBManager:

遗产:

$query = 'UPDATE table SET foo = "bar" WHERE id = ' . $GLOBALS['db']->quoted($id);
$GLOBALS['db']->query($query);

最佳实践:

$fieldDefs = $GLOBALS['dictionary']['table']['fields'];
$GLOBALS['db']->updateParams('table', $fieldDefs, array('foo' => 'bar',), array('id' => $id) );

对于更复杂的标准或列值包含对表中其他字段的表达式或引用然后可以使用Doctrine QueryBuilder。

遗产:

$query = 'UPDATE table SET foo = "bar" WHERE foo = "foo" OR foo IS NULL';
$GLOBALS['db']->execute($query);

最佳实践:

$query = 'UPDATE table SET foo = ? WHERE foo = ? OR foo IS NULL';
$conn = $GLOBALS['db']->getConnection();
$stmt = $conn->executeQuery($query, array('bar', 'foo'));

从SugarBean生成SQL3d彩吧论坛

要使Suggor自动生成SQL3d彩吧论坛,可以使用Bean类中的以下方法。

选择3d彩吧论坛

To create a select query you can use the create_new_list_query() method:

$bean = BeanFactory::newBean($module);

$order_by = '';
$where = '';
$fields = array(
    'id',
    'name',
);

$sql = $bean->create_new_list_query($order_by, $where, $fields);

计算3d彩吧论坛

You can also run the generated SQL through the create_list_count_query() method to generate a count query:

$bean = BeanFactory::newBean('Accounts');

$sql = "SELECT * FROM accounts WHERE deleted = 0";

$count_sql = $bean->create_list_count_query($sql);

最后修改:2020-03-16 17:00:15