Skip Navigation

paginating ad-hoc joins in CakePHP

March 28th, 2009

Couple months ago, Nate published in the bakery a quick tip to do what he called ad-hoc joins in cakephp’s find()
Basically the trick was to use the joins parameter of the method find() to define the associations.
I have noticed a lot of people asking about how to paginate the results.
So This post is exactly for doing that..

I’ll be using the modified version found in the comments by Bambou

Basically His version allows to define the type of find() using a parameter called operation

just copy it to your AppModel, or the model that you want to do ad-hoc joins on:

Now for pagination to work with the new ‘matches’ type, you’ll have to write a custom paginateCount()
copy this to your AppModel or the model in question:

  1.  
  2. function paginateCount($conditions = array(), $recursive = 0, $extra = array()) {
  3.  
  4.         $parameters = compact(‘conditions’);
  5.         if ($recursive != $this->recursive) {
  6.                 $parameters[‘recursive’] = $recursive;
  7.         }
  8.         if(isset($extra[‘type’]) && ($extra[‘type’] == ‘matches’)) {
  9.                 $extra[‘operation’] = ‘count’;
  10.                 return $this->find(‘matches’, array_merge($parameters, $extra));
  11.         } else {
  12.                 return $this->find(‘count’, array_merge($parameters, $extra));
  13.         }       
  14. }

Usage is nearly identical to the normal paginate() except that instead of doing:

  1.  $markers = $this->Marker->find(‘matches’, array(
  2.        ‘operation’ => ‘all’,
  3.         ‘model’ => ‘Tag’,
  4.         ’scope’ => array(‘Tag.tag’ => $tags)
  5.    )
  6. );

You’ll do:

  1.  array_unshift($this->paginate, ‘matches’);
  2. $this->paginate[‘model’] = ‘Tag’;
  3. $this->paginate[‘operation’] = ‘all’;
  4. $this->paginate[’scope’] = array(‘Tag.tag’=>$tags);
  5. $result = $this->paginate();

The trick indeed is in array_unshift($this->paginate, ‘matches’);
and has to do with how paginate() works internally ( makes the assumption/convention that the first element in the paginate array is the type )

So that’s it, hopefully this will help some misguided cakephp souls ;)

w00t! I’ve got wordpress spam-hacked!

March 27th, 2009

It’s been a while ( like really really a while, since I looked here )
I won’t get on the details of what-why, mainly family, sickness and a lot of other work experiments.
Anyhow, I apologize for not being here, answering comments and posting new stuff.
But no worries I’m back on the rails and hope to be able to post some new exciting stuff

Today I’ve got an email from google telling me that this blog is scheduled for removal from their indexes, whaaat!
Here is the complete email:

Dear site owner or webmaster of devmoz.com,

While we were indexing your webpages, we detected that some of your pages were using techniques that are outside our quality guidelines, which can be found here: http://www.google.com/support/webmasters/bin/answer.py?answer=35769&hl=en. This appears to be because your site has been modified by a third party. Typically, the offending party gains access to an insecure directory that has open permissions. Many times, they will upload files or modify existing ones, which then show up as spam in our index.

The following is some example hidden text we found at http://www.devmoz.com/blog/category/cakephp/:

[bunch of spam words here]

In order to preserve the quality of our search engine, pages from devmoz.com are scheduled to be removed temporarily from our search results for at least 30 days.

We would prefer to keep your pages in Google’s index. If you wish to be reconsidered, please correct or remove all pages (may not be limited to the examples provided) that are outside our quality guidelines. One potential remedy is to contact your web host technical support for assistance. For more information about security for webmasters, see http://googlewebmastercentral.blogspot.com/2008/04/my-sites-been-hacked-now-what.html. When such changes have been made, please visit https://www.google.com/webmasters/tools/reconsideration?hl=en to learn more and submit your site for reconsideration.

Sincerely, Google Search Quality Team
Note: if you have an account in Google’s Webmaster Tools, you can verify the authenticity of this message by logging into https://www.google.com/webmasters/tools/siteoverview?hl=en and going to the Message Center.

I’ve got really shocked ! I do HATE spam so how can this be possible.
I’ve looked at the page and found indeed a hidden div containing tons of links pointing to some spam websites. It was clear I’ve got SPAM HACKED.
I quickly ssh-ed to the wordpress files, run through the web logs, some greps.. and found out the links in footer.php, I removed the huge list of links, chmod-ed the file to readonly.
I was running wordpress version 2.2.1 or something, haven’t had any plugins installed other than akismet, openid and dean’s code highlighter. so must have been a vulnerability in the version of wordpress I was running.
Well since I haven’t been here in a while I couldn’t upgrade, duh!

I looked through http://codex.wordpress.org/Upgrading_WordPress_Extended to get me an idea on how to upgrade to the latest version of wordpress, and to patch my custom made theme. I was anxious but I could upgrade in less than 15min.
I logged into the google webmaster tool’s thingy and sent google a nice message, I just hope they’ll reconsider the removal :(

So people moral of the story is, upgrade your open source software often!

MailingBaker: a cakePHP newsletter manager plugin

June 28th, 2007

Not news for those who track new projects at cakeforge but I’ve started a new plugin for cakePHP, it’s called MailingBaker and as the name says, it’s a newsletter manager plugin.
MailingBaker has multi-lists, WYSIWYG newsletter editing (html/plain), layouts, opt-in, opt-out and more. Mailing Baker doesn’t aim to be a standalone application, rather, be hooked in your applications..well a plugin duh.
there is already working code in the svn repo
Currently MB is not setup as a plugin in code, it’s just a normal app, when the functionality is stable we’ll package it as a plugin and make a release.

MailingBaker uses a new version of my SwiftMailer Component (compatible with version 3 yes).

If you are interested in joining the project, please don’t hesitate, as it’s for the benefit of everyone. Whenever you will need a newsletter/mailing list functionality in your cake applications you can plug-in MB and get done.

Cake!

Introducing TohDoh Yet Another CakePHP AJAX Todo-List Demo

April 14th, 2007

TohDoh is YET ANOTHER CAKEPHP AJAX TODO LIST DEMO or YACATLD, a 100% ajax threaded todo list. It’s both a showcase and an opportunity for new bakers to learn from.
TohDoh has some features like:

  • Threaded, so you have tasks and sub tasks.
  • You can add, edit in-place and delete tasks
  • Drag & Drop to sort tasks
  • And of course you can done & and undone tasks :)

You can see the demo running here. The code is available at cakeforge

Read the rest of this entry »

Update to the Conf Component

April 5th, 2007

Not so long ago I’ve created a component to handle DB based configuration and settings. It was pretty clean and slim. Some features however were needed ( requested ), so the component got some updates.
Read the rest of this entry »

CakePHP: Update a select box using ajax

April 4th, 2007

I noticed a lot of new comers asking this question: I want a select box to be updated using ajax when I change the current item in another select box .

So this is a quick tutorial on how to do exactly that.

Read the rest of this entry »

a tinymce element for cakePHP

March 29th, 2007

To demonstrate the power of Elements in cakePHP, I’m going to use as an example TinyMCE. TinyMCE is basically a LGPLed web based Javascript HTML WYSIWYG editor. It has many features but it’s not the aim of this post to introduce it, rather to demonstrate how we can use it as an element. TinyMCE can be used on textareas ( and probably some other tags ) .
Read the rest of this entry »

othAuth 0.5.4

March 29th, 2007

This is mainly a bug fix release.

  • fixed a bug related to Y-m-d H:i:s
  • Fixed a bug in loginAttempts reported by PatDaMilla
  • Added support for parameters sent via url in a traditional way, mainly for redirects, thanks to Ritesh.

What else I’m not sure but we’re going towards a more stable system.
the nao mode is broken at the moment, I’ll fix it when I’ll have time to write docs for it.

Updated Component
Docs

Hello devmoz !

March 6th, 2007

Hey,
so this is going to be the new othy home, how is the look ?
I imported all the previous posts..the adventure continues here..
stay tuned !

ConfComponent db Based configuration

February 16th, 2007

Hello, it has been some time now. My apologies for that, I’ve been kinda very busy lately ( whine whine..). But anyway I’ve found sometime to release something, and oh Ma! it is something! heh well, not really. This time I have a configuration component for you. I hear you saying, but cake already has a configuration system. Yes it has, and it’s very nice. really neat. But it’s not db based, meaning the configuration files are stored in files in app/config/. it’s no big deal but in some cases you have to use the db.

So anyway the component is in bakery along with a tutorial. check it out.

PS: If the links say the article doesn’t exist, you gotta wait until a moderator validates them..or write a comment with your email so I can send it to you.