1

I am logging changes in my database to a table called audit_field. For a given model I would like to retrieve all the audit_fields for this model as well as some of the related models.

For example:

<?php
class Job extends ActiveRecord
{
    public function getAuditFields()
    {

        $link = []; // what do I put here to get "1=1" ?

        return $this->hasMany(AuditField::className(), $link)
            ->orOnCondition([
                'audit_field.model_id' => $this->job_id, 
                'audit_field.model_name' => get_class($this),
            ])
            ->orOnCondition([
                'audit_field.model_id' => ArrayHelper::map($this->getJobTeches()->all(), 'id', 'id'),
                'audit_field.model_name' => 'app\models\JobTech',
            ]);
    }
    public function getJobTeches()
    {
        return $this->hasMany(JobTech::className(), ['job_id' => 'job_id']);
    }
}

What I want:

SELECT * FROM audit_field WHERE (...my or conditions...);
// or
SELECT * FROM audit_field WHERE (1=1) AND (...my or conditions...);

What I get:

SELECT * FROM audit_field WHERE (1=0) AND (...my or conditions...)
cornernote
  • 1,055
  • 1
  • 12
  • 20

1 Answers1

-1

Found the answer, don't use hasMany, instead just return an ActiveQuery:

<?php
class Job extends ActiveRecord
{
    public function getAuditFields()
    {
        return AuditField::find()
            ->orOnCondition([
                'audit_field.model_id' => $this->job_id, 
                'audit_field.model_name' => get_class($this),
            ])
            ->orOnCondition([
                'audit_field.model_id' => ArrayHelper::map($this->getJobTeches()->all(), 'id', 'id'),
                'audit_field.model_name' => 'app\models\JobTech',
            ]);
    }
}
cornernote
  • 1,055
  • 1
  • 12
  • 20