0

I have below mysql tables:

Table1: emp

Fields: id, document_ids

Table2: documents

Fields: id, document_name

Example data:

Table1

emp
<table border='1pt'>
  <tr>
    <td>id</td>
    <td>document_ids</td>
  </tr>
  <tr>
    <td>1</td>
    <td>[1,2]</td>
  </tr>
  <tr>
    <td>2</td>
    <td>[2,3]</td>
  </tr>  
</table>

documents
<table border='1pt'>
  <tr>
    <td>id</td>
    <td>document_name</td>
  </tr>
  <tr>
    <td>1</td>
    <td>Matser Degree</td>
  </tr>
  <tr>
    <td>2</td>
    <td>HSC</td>
  </tr>
    <tr>
    <td>3</td>
    <td>SSC</td>
  </tr>
</table>

Result should be:

<table border='1pt'>
  <tr>
    <td>id</td>
    <td>documents</td>
  </tr>
  <tr>
    <td>1</td>
    <td>Master Degree, HSC</td>
  </tr>
  <tr>
    <td>2</td>
    <td>HSC,SSC</td>
  </tr>
</table>

I want the result that I printed in above third table as a result. Relation should be on first model 'emp' only. How do I make query to get such result in yii2 model relation?

Rahul Mankar
  • 910
  • 9
  • 17

1 Answers1

0

It depends which database u use. Yii2 ActiveRelationTrait enhancement - here guys added support to get relation by array, use it normally like other hasMany() relations.

And if youre using database which doesnt support this you can do something like this, if document_ids is real PHP array:

public function getDocuments() {
   return Document()->find()->andWhere(['id' => $this->document_ids]);
}
Yupik
  • 4,932
  • 1
  • 12
  • 26