PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 18, 2022

[FIXED] remove fields from select generated by matching

 January 18, 2022     cakephp-3.0, orm     No comments   

Issue

I have two custom find methods and I would like to make an union of them.

public function findInGroup(Query $query, array $options)
{
    return $query
      ->matching(
          'Groups',
          function ($q) {
                  return $q->where(['Groups.id' => 1]);
              }
      );
}

public function findAsUser(Query $query, array $options)
{
    return $query
      ->matching(
          'Users',
          function ($q) {
                  return $q->where(['Users.id' => 1]);
              }
      );
}

The two methods generates different select list, because of the matching call. So I can not make union of them...

I do not need Groups__id, etc fields to be selected.

Is there any way to tell to matching that it should not add the matching data fields to the created select field list?


Solution

You have two options"

Select all the fields from the main query

That will make the query builder omit the fields from associations:

$query
    ->find('iGroup', $options)
    ->find('asUser', $options)
    ->select($table->schema()->columns());

By specifically selecting the columns you need, you will leave out the columns form associations that can be joined, i.e. the columns from matching.

Use CakePHP 3.1

CakePHP 3.1 introduces a new function called innerJoinWith(), it does exactly the same as matching() but it will not select any columns from the association:

public function findInGroup(Query $query, array $options)
{
    return $query
      ->innerJoinWith(
          'Groups',
          function ($q) {
                  return $q->where(['Groups.id' => 1]);
              }
      );
}

public function findAsUser(Query $query, array $options)
{
    return $query
      ->innerJoinWith(
          'Users',
          function ($q) {
                  return $q->where(['Users.id' => 1]);
              }
      );
}


Answered By - José Lorenzo Rodríguez
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing