0

如何在同一个控制器和不同控制器中的 cakephp 3.0 中编写查询。

在用户控制器 $data = $this->Users->find('all'); 公关($数据);

输出:

Cake\ORM\Query Object
(
    [_hasFields:protected] => 
    [_autoFields:protected] => 
    [_hydrate:protected] => 1
    [_counter:protected] => 
    [_eagerLoader:protected] => 
    [_beforeFindFired:protected] => 
    [_connection:protected] => Cake\Database\Connection Object
        (
            [_config:protected] => Array
                (
                    [driver] => Cake\Database\Driver\Mysql
                    [persistent] => 
                    [host] => localhost
                    [username] => root
                    [password] => 
                    [database] => lenders
                    [encoding] => utf8
                    [timezone] => UTC
                    [cacheMetadata] => 1
                    [log] => 
                    [quoteIdentifiers] => 
                    [name] => default
                )

            [_driver:protected] => Cake\Database\Driver\Mysql Object
                (
                    [_baseConfig:protected] => Array
                        (
                            [persistent] => 1
                            [host] => localhost
                            [username] => root
                            [password] => 
                            [database] => cake
                            [port] => 3306
                            [flags] => Array
                                (
                                )

                            [encoding] => utf8
                            [timezone] => 
                            [init] => Array
                                (
                                )

                        )

                    [_config:protected] => Array
                        (
                            [driver] => Cake\Database\Driver\Mysql
                            [persistent] => 
                            [host] => localhost
                            [username] => root
                            [password] => 
                            [database] => lenders
                            [encoding] => utf8
                            [timezone] => UTC
                            [cacheMetadata] => 1
                            [log] => 
                            [quoteIdentifiers] => 
                            [name] => default
                            [port] => 3306
                            [flags] => Array
                                (
                                )

                            [init] => Array
                                (
                                )

                        )

                    [_autoQuoting:protected] => 
                    [_startQuote:protected] => `
                    [_endQuote:protected] => `
                    [_schemaDialect:protected] => Cake\Database\Schema\MysqlSchema Object
                        (
                            [_driver:protected] => Cake\Database\Driver\Mysql Object
 *RECURSION*
                        )

                    [_connection:protected] => PDO Object
                        (
                        )

                )

            [_transactionLevel:protected] => 0
            [_transactionStarted:protected] => 
            [_useSavePoints:protected] => 
            [_logQueries:protected] => 1
            [_logger:protected] => DebugKit\Database\Log\DebugLog Object
                (
                    [_queries:protected] => Array
                        (
                        )

                    [_logger:protected] => 
                    [_connectionName:protected] => default
                    [_totalTime:protected] => 0
                    [_totalRows:protected] => 0
                )

            [_schemaCollection:protected] => Cake\Database\Schema\CachedCollection Object
                (
                    [_cache:protected] => _cake_model_
                    [_connection:protected] => Cake\Database\Connection Object
 *RECURSION*
                    [_dialect:protected] => Cake\Database\Schema\MysqlSchema Object
                        (
                            [_driver:protected] => Cake\Database\Driver\Mysql Object
                                (
                                    [_baseConfig:protected] => Array
                                        (
                                            [persistent] => 1
                                            [host] => localhost
                                            [username] => root
                                            [password] => 
                                            [database] => cake
                                            [port] => 3306
                                            [flags] => Array
                                                (
                                                )

                                            [encoding] => utf8
                                            [timezone] => 
                                            [init] => Array
                                                (
                                                )

                                        )

                                    [_config:protected] => Array
                                        (
                                            [driver] => Cake\Database\Driver\Mysql
                                            [persistent] => 
                                            [host] => localhost
                                            [username] => root
                                            [password] => 
                                            [database] => lenders
                                            [encoding] => utf8
                                            [timezone] => UTC
                                            [cacheMetadata] => 1
                                            [log] => 
                                            [quoteIdentifiers] => 
                                            [name] => default
                                            [port] => 3306
                                            [flags] => Array
                                                (
                                                )

                                            [init] => Array
                                                (
                                                )

                                        )

                                    [_autoQuoting:protected] => 
                                    [_startQuote:protected] => `
                                    [_endQuote:protected] => `
                                    [_schemaDialect:protected] => Cake\Database\Schema\MysqlSchema Object
 *RECURSION*
                                    [_connection:protected] => PDO Object
                                        (
                                        )

                                )

                        )

                )

        )

    [_type:protected] => select
    [_parts:protected] => Array
        (
            [delete] => 1
            [update] => Array
                (
                )

            [set] => Array
                (
                )

            [insert] => Array
                (
                )

            [values] => Array
                (
                )

            [select] => Array
                (
                )

            [distinct] => 
            [modifier] => Array
                (
                )

            [from] => Array
                (
                )

            [join] => Array
                (
                )

            [where] => 
            [group] => Array
                (
                )

            [having] => 
            [order] => 
            [limit] => 
            [offset] => 
            [union] => Array
                (
                )

            [epilog] => 
        )

    [_dirty:protected] => 1
    [_resultDecorators:protected] => Array
        (
        )

    [_iterator:protected] => 
    [_valueBinder:protected] => 
    [_functionsBuilder:protected] => 
    [_useBufferedResults:protected] => 1
    [_typeMap:protected] => Cake\Database\TypeMap Object
        (
            [_defaults:protected] => Array
                (
                    [Users.id] => integer
                    [id] => integer
                    [Users.username] => string
                    [username] => string
                    [Users.password] => string
                    [password] => string
                    [Users.first_name] => string
                    [first_name] => string
                    [Users.last_name] => string
                    [last_name] => string
                    [Users.email] => string
                    [email] => string
                    [Users.status] => integer
                    [status] => integer
                    [Users.group_id] => integer
                    [group_id] => integer
                    [Users.created_by] => integer
                    [created_by] => integer
                    [Users.created] => datetime
                    [created] => datetime
                    [Users.modifiedmodified] => datetime
                    [modifiedmodified] => datetime
                    [Users.modified_by] => integer
                    [modified_by] => integer
                    [Users.last_login] => datetime
                    [last_login] => datetime
                    [Users.login_attempts] => integer
                    [login_attempts] => integer
                    [Users.unlocktime] => datetime
                    [unlocktime] => datetime
                )

            [_types:protected] => Array
                (
                )

        )

    [_repository:protected] => App\Model\Table\UsersTable Object
        (
            [_table:protected] => users
            [_alias:protected] => Users
            [_connection:protected] => Cake\Database\Connection Object
                (
                    [_config:protected] => Array
                        (
                            [driver] => Cake\Database\Driver\Mysql
                            [persistent] => 
                            [host] => localhost
                            [username] => root
                            [password] => 
                            [database] => lenders
                            [encoding] => utf8
                            [timezone] => UTC
                            [cacheMetadata] => 1
                            [log] => 
                            [quoteIdentifiers] => 
                            [name] => default
                        )

                    [_driver:protected] => Cake\Database\Driver\Mysql Object
                        (
                            [_baseConfig:protected] => Array
                                (
                                    [persistent] => 1
                                    [host] => localhost
                                    [username] => root
                                    [password] => 
                                    [database] => cake
                                    [port] => 3306
                                    [flags] => Array
                                        (
                                        )

                                    [encoding] => utf8
                                    [timezone] => 
                                    [init] => Array
                                        (
                                        )

                                )

                            [_config:protected] => Array
                                (
                                    [driver] => Cake\Database\Driver\Mysql
                                    [persistent] => 
                                    [host] => localhost
                                    [username] => root
                                    [password] => 
                                    [database] => lenders
                                    [encoding] => utf8
                                    [timezone] => UTC
                                    [cacheMetadata] => 1
                                    [log] => 
                                    [quoteIdentifiers] => 
                                    [name] => default
                                    [port] => 3306
                                    [flags] => Array
                                        (
                                        )

                                    [init] => Array
                                        (
                                        )

                                )

                            [_autoQuoting:protected] => 
                            [_startQuote:protected] => `
                            [_endQuote:protected] => `
                            [_schemaDialect:protected] => Cake\Database\Schema\MysqlSchema Object
                                (
                                    [_driver:protected] => Cake\Database\Driver\Mysql Object
 *RECURSION*
                                )

                            [_connection:protected] => PDO Object
                                (
                                )

                        )

                    [_transactionLevel:protected] => 0
                    [_transactionStarted:protected] => 
                    [_useSavePoints:protected] => 
                    [_logQueries:protected] => 1
                    [_logger:protected] => DebugKit\Database\Log\DebugLog Object
                        (
                            [_queries:protected] => Array
                                (
                                )

                            [_logger:protected] => 
                            [_connectionName:protected] => default
                            [_totalTime:protected] => 0
                            [_totalRows:protected] => 0
                        )

                    [_schemaCollection:protected] => Cake\Database\Schema\CachedCollection Object
                        (
                            [_cache:protected] => _cake_model_
                            [_connection:protected] => Cake\Database\Connection Object
 *RECURSION*
                            [_dialect:protected] => Cake\Database\Schema\MysqlSchema Object
                                (
                                    [_driver:protected] => Cake\Database\Driver\Mysql Object
                                        (
                                            [_baseConfig:protected] => Array
                                                (
                                                    [persistent] => 1
                                                    [host] => localhost
                                                    [username] => root
                                                    [password] => 
                                                    [database] => cake
                                                    [port] => 3306
                                                    [flags] => Array
                                                        (
                                                        )

                                                    [encoding] => utf8
                                                    [timezone] => 
                                                    [init] => Array
                                                        (
                                                        )

                                                )

                                            [_config:protected] => Array
                                                (
                                                    [driver] => Cake\Database\Driver\Mysql
                                                    [persistent] => 
                                                    [host] => localhost
                                                    [username] => root
                                                    [password] => 
                                                    [database] => lenders
                                                    [encoding] => utf8
                                                    [timezone] => UTC
                                                    [cacheMetadata] => 1
                                                    [log] => 
                                                    [quoteIdentifiers] => 
                                                    [name] => default
                                                    [port] => 3306
                                                    [flags] => Array
                                                        (
                                                        )

                                                    [init] => Array
                                                        (
                                                        )

                                                )

                                            [_autoQuoting:protected] => 
                                            [_startQuote:protected] => `
                                            [_endQuote:protected] => `
                                            [_schemaDialect:protected] => Cake\Database\Schema\MysqlSchema Object
 *RECURSION*
                                            [_connection:protected] => PDO Object
                                                (
                                                )

                                        )

                                )

                        )

                )

            .........

请帮助我在 cakephp 3.x 中编写 mysql 查询以及如何动态加载模型和选择数据。

4

1 回答 1

0

要访问不同控制器中的表数据,您可以使用提供的 TableRegistry 类:

class OtherController extends AppController
{
    public function yourEndpoint()
    {
        $users_table = TableRegistry::get('Users');
        $user_collection = $users_table->find()->all();
        $this->set('users_collection', $users_collection);
    }
}

此外,您当前的查询 $this->Users->find('all'); 只返回一个查询对象本身。要返回结果集本身,您需要迭代结果集或将其转换为数组。以下是一些可用于从查询对象返回结果集的方法:

// The ->find() function returns a query object!
$base_query = $users_table->find();

// 1. Using the foreach() iterator
foreach($base_query as $user) {
    echo $user->name;
}

// 2. Using the native Collections Class each iterator
$base_query->each(function ($user) {
    echo $user->name;
}

// 3. Getting all results of a set using ->all()
$result_set = $base_query->all();

// 4. Casting to an array using ->toArray()
$result_array = $base_query->toArray();

查看关于表对象的CakePHP 3 文档。

祝你好运!

于 2015-12-29T14:34:19.803 回答