{"id":2784,"date":"2022-08-30T15:27:45","date_gmt":"2022-08-30T15:27:45","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/16\/cake-php-paginate-multiple-tables-of-one-model-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:27:45","modified_gmt":"2022-08-30T15:27:45","slug":"cake-php-paginate-multiple-tables-of-one-model-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/cake-php-paginate-multiple-tables-of-one-model-collection-of-common-programming-errors\/","title":{"rendered":"Cake PHP paginate multiple tables of one model-Collection of common programming errors"},"content":{"rendered":"<p>community, there is a problem with multiple tables which receive their entries of the same model &#8220;Post&#8221; on the same page. When clicking a paginator-number the app will change the page for both tables.<\/p>\n<p>I tried it with dummy classes in the AppModel which should work but I get an error saying an internal error has occurred (error 500). I found out that there is a problem with the corresponding SQL-statement with a not found row.<\/p>\n<p>In the AppModel:<\/p>\n<pre><code>class PostHood extends Post {\n    public $useTable = 'posts';\n};\n<\/code><\/pre>\n<p>That code uses the table &#8220;cake_posts&#8221; of &#8220;class Post extends AppModel&#8221;. In the controller I tried to get the PostHood like following:<\/p>\n<pre><code>$this-&gt;paginate['PostHood'] = array(\n    'conditions' =&gt; array('OR' =&gt; array(stuff)),\n    'limit' =&gt; 5\n);\n$this-&gt;set('postsHood', $this-&gt;paginate('PostHood'));\n<\/code><\/pre>\n<p>In the view there is a foreach-loop using the $postsHood as $post. Maybe you have an idea, thanks in advance \ud83d\ude42<\/p>\n<p>EDIT 1: I got some error notices after changing the code. May be you have an idea what to do. Change of the AppModel:<\/p>\n<pre><code>class PostHood extends AppModel {\n    var $name = 'Post';\n    public $useTable = 'posts';\n};\n<\/code><\/pre>\n<p>The Controller:<\/p>\n<pre><code>$this-&gt;loadModel('PostHood');\n$this-&gt;paginate = array(\n    'conditions' =&gt; array('OR' =&gt;\n    array(\n        array('AND' =&gt; array(   \n        array('PostHood.ZIPCODE LIKE' =&gt; $userArea . '%'),\n            array('PostHood.ALTDATE &gt;' =&gt; date(\"Y-m-d\")),\n        array('PostHood.AGENT' =&gt; '0'),\n        array('PostHood.OWNER ' =&gt; $this-&gt;UserAuth-&gt;getUserId()),\n        array('PostHood.PARENTID' =&gt; '0'),\n        array('PostHood.ACCEPTED' =&gt; '0')\n    )), [MORE AND ARRAYS]\n$this-&gt;set('postsHood', $this-&gt;paginate('PostHood'));\n<\/code><\/pre>\n<p>The corresponding error in the view:<\/p>\n<pre><code>Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Post.DATE' in 'order clause'\n<\/code><\/pre>\n<p>SQL Query: SELECT <code>PostHood<\/code>.<code>id<\/code>, <code>PostHood<\/code>.<code>B\/S<\/code>, <code>PostHood<\/code>.<code>H<\/code>, <code>PostHood<\/code>.<code>CITY<\/code>, <code>PostHood<\/code>.<code>MARKET<\/code>, <code>PostHood<\/code>.<code>DATE<\/code>, <code>PostHood<\/code>.<code>ALTDATE<\/code>, <code>PostHood<\/code>.<code>TIME<\/code>, <code>PostHood<\/code>.<code>INCOME<\/code>, <code>PostHood<\/code>.<code>ZIPCODE<\/code>, <code>PostHood<\/code>.<code>ALIAS<\/code>, <code>PostHood<\/code>.<code>VEHICLE<\/code>, <code>PostHood<\/code>.<code>DELIVERYAREA<\/code>, <code>PostHood<\/code>.<code>SPACE<\/code>, <code>PostHood<\/code>.<code>STREET<\/code>, <code>PostHood<\/code>.<code>HOUSENUMBER<\/code>, <code>PostHood<\/code>.<code>NAME<\/code>, <code>PostHood<\/code>.<code>CART<\/code>, <code>PostHood<\/code>.<code>TEL<\/code>, <code>PostHood<\/code>.<code>OWNER<\/code>, <code>PostHood<\/code>.<code>created<\/code>, <code>PostHood<\/code>.<code>modified<\/code>, <code>PostHood<\/code>.<code>AGENT<\/code>, <code>PostHood<\/code>.<code>EXTRADATA<\/code>, <code>PostHood<\/code>.<code>PARENTID<\/code>, <code>PostHood<\/code>.<code>BRATED<\/code>, <code>PostHood<\/code>.<code>SRATED<\/code>, <code>PostHood<\/code>.<code>REQUESTED<\/code>, <code>PostHood<\/code>.<code>ACCEPTED<\/code> FROM <code>usr_web126986_4<\/code>.<code>cake_posts<\/code> AS <code>PostHood<\/code> WHERE 1 = 1 ORDER BY <code>Post<\/code>.<code>DATE<\/code> asc LIMIT 5<\/p>\n<p>Obviously Cake tries to fetch data with &#8220;PostHood&#8221; but the table &#8220;posts&#8221; which I actually want to use is listening to &#8220;Post.field&#8221;. How can I fix that? Thanks \ud83d\ude42<\/p>\n<ol>\n<li>\n<p>You should try something like this:<\/p>\n<pre><code>$this-&gt;paginate = array(\n    'conditions' =&gt; array('OR' =&gt; array(stuff)),\n    'limit' =&gt; 5\n);\n$this-&gt;set('postsHood', $this-&gt;paginate('PostHood'));\n<\/code><\/pre>\n<p>I am just curious as to why you want to structure your model extending another model.<\/p>\n<\/li>\n<li>\n<p>Got it working with the great plugin DataTables. That overrides the cake-pagination and uses JSON to display the results.<\/p>\n<p>In order to implement it into cake you need<\/p>\n<p>1.) a cake component for interpreting sql-statements: https:\/\/github.com\/cnizzdotcom\/cakephp-datatable<\/p>\n<p>2.) the plugin: http:\/\/www.datatables.net\/index<\/p>\n<p>3.) implementation:<\/p>\n<pre><code>$this-&gt;paginate = array(\n    'fields' =&gt; array('Model.field1', 'Model.field2'),\n    'conditions' =&gt; array( stuff )\n);\n$response = $this-&gt;DataTable-&gt;getResponse();\n$this-&gt;set('response', $response);\n$this-&gt;set('_serialize','response');\n$encode = json_encode($response);\n$this-&gt;set('dataTablesData', $encode);\n<\/code><\/pre>\n<p>Then you can grab the data in the view.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-02-16 13:13:06. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>community, there is a problem with multiple tables which receive their entries of the same model &#8220;Post&#8221; on the same page. When clicking a paginator-number the app will change the page for both tables. I tried it with dummy classes in the AppModel which should work but I get an error saying an internal error [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2784","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2784","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2784"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2784\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}