Upon model retrieval the model's query object forgets the selected columns #863
Description
- Laravel Version: 6
- Adldap2-Laravel Version: 6.0.9
- PHP Version: 7.3
- LDAP Type: OpenLDAP
Description:
Hi.
We are using OpenLDAP which means we need to explicitly select the entryuuid on users when running an LDAP query. It works fine as the UserResolver automatically inserts it to the queries, but the resulting Adldap\Model model's "query" object loses this information so upon calling $model->fresh(), the entryuuid is missing from the fresh model.
Steps To Reproduce:
$resolver->query()->where('uid', 'grant')->first()
The returned model has the "entryuuid" attribute, but the model's "query" object has only "*" as the selected columns.
Calling fresh() right after the query:
$resolver->query()->where('uid', 'grant')->first()->fresh()
The returned model does not have the "entryuuid" attribute and same as above the "query" object is missing the entryuuid column.
Proposed solution
I traced back the source of this behaviour to Adldap\Query\Builder@newInstance
method which creates a new Builder object from the previous one, but does not care at all about the previous selects.
Original code:
/**
* Returns a new Query Builder instance.
*
* @param string $baseDn
*
* @return Builder
*/
public function newInstance($baseDn = null)
{
// We'll set the base DN of the new Builder so
// developers don't need to do this manually.
$dn = is_null($baseDn) ? $this->getDn() : $baseDn;
return (new static($this->connection, $this->grammar, $this->schema))
->setDn($dn);
}
Proposed solution:
/**
* Returns a new Query Builder instance.
*
* @param string $baseDn
*
* @return Builder
*/
public function newInstance($baseDn = null)
{
// We'll set the base DN of the new Builder so
// developers don't need to do this manually.
$dn = is_null($baseDn) ? $this->getDn() : $baseDn;
return (new static($this->connection, $this->grammar, $this->schema))
->setDn($dn)->select($this->getSelects());
}