# SugarCRM6.5数据查询方法研究

> 作者：James Zhu (<fatindeed@hotmail.com>)
>
> 创建日期：2018-09-05

## 单条数据查询

1. 根据`accounts.id`查询数据

   ```php
   // Method 1
   $account = BeanFactory::getBean('Accounts', $account_id);
   // Method 2
   $account = BeanFactory::getBean('Accounts');
   $account->retrieve($account_id);
   ```
2. 根据属性字段`accounts.name`查询数据

   ```php
   $account = BeanFactory::getBean('Accounts');
   $account->retrieve_by_string_fields(array('name' => $account_name));
   ```

## 列表数据查询

1. 分页数据列表

   ```php
   $account_list = BeanFactory::getBean('Accounts')->get_list('', 'accounts.name like \'%Inc%\'');
   ```

   返回数据示例：

   ```php
   Array
   (
       [list] => Array
           (
               [0] => Account Object
               [1] => Account Object
               ...
           )
       [row_count] => 8
       [next_offset] => 20
       [previous_offset] => -20
       [current_offset] => 0
   )
   ```
2. 所有数据列表

   ```php
   $account_list = BeanFactory::getBean('Accounts')->get_full_list('', 'accounts.name like \'%Inc%\'');
   ```

   返回数据示例：

   ```php
   Array
   (
       [0] => Account Object
       [1] => Account Object
       ...
   )
   ```

## 关联数据查询

```php
$account = BeanFactory::getBean('Accounts', $account_id);
if($account->load_relationship('contacts')) {
    // Method 1
    $contacts = $account->contacts->getBeans();
    // Method 2
    $contacts = $account->contacts->beans;
}
```
