search

Home  >  Q&A  >  body text

Output problem

Why can’t you dump ($result->getData()) directly after $result = Staff::select($where) instead of traversing the array foreach ($result as $value)

Imagine BreakerImagine Breaker2275 days ago1642

reply all(6)I'll reply

  • jesse

    jesse2018-09-07 22:40:57

    Let’s take an official screenshot first to confirm the correctness of my underlying source code analysis:

    QQ截图20180907224207.png

    reply
    0
  • Imagine Breaker

    How did you send the screenshot and how did you operate it? Also, doesn’t it say in the video that select and all return an array of model objects?

    Imagine Breaker · 2018-09-08 09:35:52
    Imagine Breaker

    Okay, I understand, sorry for the reverse, thank you! But how did you insert the picture? I didn’t see the option to insert pictures.

    Imagine Breaker · 2018-09-08 09:40:13
  • jesse

    jesse2018-09-07 14:49:33

    There are still several aspects to be discussed about this issue:

    1. Since the data exists in the data attribute, how to determine the data attribute and remove the data set when traversing the object

    2. This is just a Model object. If it is a Collection object, how is it implemented?

    ArrayAccess should appear when these two problems arise. I will update it when I go back in the evening, so that you can truly understand the underlying operating mechanism of object traversal.

    reply
    0
  • jesse

    jesse2018-09-07 14:27:18

    First read the official manual instructions:

    QQ截图20180907141025.jpg

    Then look at the underlying source code:

    QQ截图20180907141312.jpg

    QQ截图20180907141700.jpg

    QQ图片20180907141926.png

    QQ截图20180907142024.jpg

    QQ截图20180907142220.jpg

    ##The official manual blocks many details and directly explains the results

    QQ截图20180907142316.jpg

    The last question, why can the returned results be traversed?

    QQ截图20180907142514.jpg

    QQ截图20180907142446.jpg

    Both the Model class and the Collection class implement the same ArrayAccess interface, so that they can be accessed like an array properties in the object.

    reply
    0
  • jesse

    Because the returned result set is an object, the values ​​of the internal attributes must be obtained through array traversal. The data attribute is the result set of the database query.

    jesse · 2018-09-07 14:31:41
  • Cancelreply