我对相关数据进行了查询。它确实有效并显示所有数据(如下所示)。
我的调试输出在[]括号中包含了很多额外的字段,所以我如何将其限制为只选择所需的数据,并且在[]括号中没有多余字段显示?
这也是在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'
答案 0 :(得分:2)
要从查询对象中获取数组,请使用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(); // <--