Cakephp3多个数据库

时间:2015-07-09 08:38:18

标签: cakephp cakephp-3.0

我想问一下,如果可以使用raw sql在cakephp3中使用2个数据库吗?

我有这样的查询:

select * from shop.brochures b, upload.documents up where b.doc_id = up.id;

问题是,这是2个数据库。

我不知道,如何设置连接,我认为这不起作用(doc示例)

$conn = ConnectionManager::get('default');

感谢您的任何建议。

1 个答案:

答案 0 :(得分:1)

在/config/app.php中,您可以根据需要设置多个数据库;我有2个。

public class PlaceAutocomplete: BaseFragment, IGoogleApiClientOnConnectionFailedListener {
    IGoogleApiClient client;
    public override void OnCreate( Bundle savedInstanceState ) {
        base.OnCreate( savedInstanceState );
        client = new GoogleApiClientBuilder( Activity )
                                            .EnableAutoManage( Activity as BaseFragmentActivity, 0, this )
                                            .AddOnConnectionFailedListener( OnConnectionFailed )
                                            .AddApi( Places.GEO_DATA_API )
                                            .Build();
    }

    public override View OnCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
        Places.GeoDataApi.GetAutocompletePredictions( client, "cul",
        new LatLngBounds( new LatLng( 33.515071, -118.796427 ), new LatLng( 34.435985, -117.127371 ) ), null )
        .SetResultCallback<AutocompletePredictionBuffer>( AutocompleteResult );

        return base.OnCreateView( inflater, container, savedInstanceState );
    }

    public void AutocompleteResult( AutocompletePredictionBuffer buffer ) {

        if( !buffer.Status.IsSuccess ) {
            Toast.MakeText( Activity, buffer.Status.StatusMessage, ToastLength.Long ).Show();
            return;
        }

        var a = new List<IAutocompletePrediction>();
        for( var i = 0; i < buffer.Count; i++ ) {
            var item = buffer.Get( i );
            if( item is IAutocompletePrediction ) {
                a.Add( (IAutocompletePrediction) item );
            } else {
                 //all the results go in here
            }
        }

        Toast.MakeText( Activity, a.Count.ToString(), ToastLength.Long ).Show();
    }

    public void OnConnectionFailed( ConnectionResult result ) {

    }
}

然后在控制器中你可以设置

'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'username' => 'root',
        'password' => '',
        'database' => 'sisarticulos',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,
    ],
'gente' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'username' => 'root',
        'password' => '',
        'database' => 'sggeneral',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,´

首先使用默认值然后使用gente