Yii Active Record Query result conversion array

Source: Internet
Author: User
Tags yii

When using Yii's active Record to get query results, the returned result set is an object type, and sometimes it is desirable to be able to return to the array for data processing convenience. For example, the following method:

Finds the first row in the result that meets the specified criteria $post=post::model ()->find ($condition, $params);//finds the row with the specified primary key value $post=post::model (), FINDBYPK ($postID, $condition, $params);//Finds the row $post=post::model ()->findbyattributes with the specified attribute value ($attributes, $ condition, $params);

When returning a result, use the $post directly->attributes; You can do it.

Post::model ()->find ()->attributes If there are multiple results returned, there are 2 ways to return an array of objects:
The first directly loops the result out of the foreach ($myReceivedCode as $model) {                        $result [] = $model->attributes;                } The second uses array_map                $result = Array_map (function ($record) {                                return $record->attributes;                        }, Post::model () ->->findallbyattributes ($attributes));

Yii Active Record Query result conversion array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.