【发布时间】: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'
【问题讨论】:
-
尝试
$query3->toArray();,然后循环/调试结果。
标签: cakephp cakephp-3.0