【问题标题】:display complex find results in a cakephp3 debug在 cakephp3 调试中显示复杂的查找结果
【发布时间】:2016-06-04 08:02:20
【问题描述】:

我对相关数据进行了查询。它确实工作并显示所有数据(如下所示)。

我的调试输出在 [] 括号中包含很多额外的字段,那么如何将其限制为仅来自选择的所需数据并且在 [] 括号中不显示额外字段?

这也是在 cakephp3 中获取此类关联数据的最佳方式吗?因为我习惯于 cakephp2。cakephp3 中用于查找许多表的代码要少得多,但我确实更喜欢 cakephp2 参数。我习惯于使用连接进行查找和条件,但 cakephp3 不需要查找中的连接代码。

$query3 = $this->Bookmarks->find()
          ->contain(['Users'])
          ->select(['bookmarks.id','bookmarks.title','users.id'])      
          ->where(['bookmarks.id' => 1])  ;    

        $query3->matching('Tags', function ($q) {

             return $q
                ->select(['tags.id']) 
                ->where(['Tags.title like' => '%tes%']);
            });


            foreach ( $query3 as $row) {
                  debug($row);
              }

//bookmarks model
public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('bookmarks');


        $this->belongsTo('Users', [
            'foreignKey' => 'user_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsToMany('Tags', [
            'foreignKey' => 'bookmark_id',
            'targetForeignKey' => 'tag_id',
            'joinTable' => 'bookmarks_tags'
        ]);
    }


'bookmarks' => [
    'id' => '1',
    'title' => 'kmkl'
],
'users' => [
    'id' => '1'
],
'tags' => [
    'id' => '1'
],
'[new]' => false,
'[accessible]' => [
    '*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Bookmarks'

http://book.cakephp.org/3.0/en/orm/query-builder.html

【问题讨论】:

  • 尝试$query3->toArray();,然后循环/调试结果。

标签: cakephp cakephp-3.0


【解决方案1】:

要从查询对象中获取数组,请使用toArray

改变

$query3 = $this->Bookmarks->find()
          ->contain(['Users'])
          ->select(['bookmarks.id','bookmarks.title','users.id'])      
          ->where(['bookmarks.id' => 1]); 

$query3 = $this->Bookmarks->find()
          ->contain(['Users'])
          ->select(['bookmarks.id','bookmarks.title','users.id'])      
          ->where(['bookmarks.id' => 1])
          ->toArray();  // <--

【讨论】:

    猜你喜欢
    • 2012-11-03
    • 2015-01-26
    • 2011-02-20
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 2019-09-06
    相关资源
    最近更新 更多