['Attachment', 'Inline Attachment'], [ 'OR' => [ [ 'parentId' => null, 'parentType!=' => null, 'relatedType=' => null, ], [ 'parentType' => null, 'relatedId' => null, 'relatedType!=' => null, ], ], ], [ 'OR' => [ 'relatedType!=' => 'Settings', 'relatedType=' => null, ], ], 'attachmentChild.id' => null, ]; $this->addLeftJoin(['Attachment', 'attachmentChild', [ 'attachmentChild.sourceId:' => 'attachment.id', 'attachmentChild.deleted' => false ]], $result); $this->setDistinct(true, $result); } }