{"id":245,"date":"2012-07-19T16:49:18","date_gmt":"2012-07-19T20:49:18","guid":{"rendered":"http:\/\/blogs.law.harvard.edu\/acts\/?p=245"},"modified":"2012-07-22T17:17:40","modified_gmt":"2012-07-22T21:17:40","slug":"yii-default-order-by","status":"publish","type":"post","link":"https:\/\/archive.blogs.harvard.edu\/acts\/2012\/07\/19\/yii-default-order-by\/","title":{"rendered":"Yii default ORDER BY"},"content":{"rendered":"<p>I ran into an issue with a minor difference between Oracle and MySQL.  Apparently MySQL is better about returning rows in the order they were inserted than Oracle. Now if you want to let me know it&#8217;s wrong to assume results are returned in any specific order, I know! Neither gives any guarantee on the order of results without an ORDER BY, but Oracle is semi-random.<\/p>\n<p>It took me a little while to find the right way to add default items to queries.  Yii uses <a href=\"http:\/\/www.yiiframework.com\/doc\/guide\/1.1\/en\/database.ar\/#defining-ar-class\">CActiveRecords<\/a> for model queries.  I already had an overridden CActiveRecord class from <a href=\"http:\/\/blogs.law.harvard.edu\/acts\/2012\/04\/05\/yii-handling-getlastinsertid-with-oracle\/\" title=\"Yii handling \u201cgetLastInsertId\u201d with Oracle\">Yii handling \u201cgetLastInsertId\u201d with Oracle<\/a>. So I knew I would be able to use that somehow.<\/p>\n<p>I finally discovered <a href=\"http:\/\/www.yiiframework.com\/doc\/guide\/1.1\/en\/database.ar\/#named-scopes\">scopes<\/a>.  It allows me to define a scope:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\npublic function scopes()\r\n{\r\n    return array(\r\n        &#039;sort_order&#039;=&gt;array(\r\n            &#039;order&#039;=&gt;&#039;SORT_ORDER ASC&#039;,\r\n        ),\r\n        &#039;id_order&#039;=&gt;array(\r\n            &#039;order&#039;=&gt;&#039;ID ASC&#039;,\r\n        ),\r\n    );\r\n}\r\n<\/pre>\n<p>and then use it as such:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nMyModel::model()-&gt;id_order()-&gt;FindAllByAttributes(array(&#039;SOMECOLUMN&#039;=&gt;1))\r\n<\/pre>\n<p>But still I didn&#8217;t find anything on default scopes.  On a whim I did a grep -i &#8220;defaultscope&#8221; on the code and discovered it exists in the Yii framework.  So I was able to piece together the following:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\npublic function defaultScope()\r\n{\r\n    return array(\r\n \t&#039;order&#039;=&gt;&#039;ID ASC&#039;\r\n    );\r\n}\r\n<\/pre>\n<p>and bam.  Add that to my <a href=\"https:\/\/github.com\/jazahn\/Quizmo\/blob\/master\/quizmo\/protected\/models\/QActiveRecord.php\">QActiveRecord<\/a>, and it&#8217;s golden.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I ran into an issue with a minor difference between Oracle and MySQL. Apparently MySQL is better about returning rows in the order they were inserted than Oracle. Now if you want to let me know it&#8217;s wrong to assume results are returned in any specific order, I know! Neither gives any guarantee on the [&hellip;]<\/p>\n","protected":false},"author":4571,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[63924,1841,63941,63942,3163,63928,63937],"tags":[63965,3159,4034,63943],"class_list":["post-245","post","type-post","status-publish","format-standard","hentry","category-atg","category-databases","category-mysql-databases","category-oracle-databases","category-php","category-quizmo-atg","category-yii","tag-abstraction","tag-mysql","tag-oracle","tag-yii-2"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/posts\/245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/users\/4571"}],"replies":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/comments?post=245"}],"version-history":[{"count":6,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/posts\/245\/revisions"}],"predecessor-version":[{"id":250,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/posts\/245\/revisions\/250"}],"wp:attachment":[{"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/media?parent=245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/categories?post=245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/tags?post=245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}