I needed to associate Models on two tables, tickets and users, but the association was not on the primaryKey it was associated using the ticket.name field: users.ticket_name = tickets.name.
This circumstance unfortunately happens to me often these days. I now work on a database with 100's of tables that needed a re-model years ago, and non-CakePHP coders obliterated the MVC standard AND good database normalization practice in many ways. So there I was, stuck in the non-primary key association complex again and needed a more permanent solution. I thought, wouldn't it be nice to define a temporary primary key override when associating models?
My query needed to be as follows:
SELECT * FROM users
LEFT JOIN tickets on users.ticket_name = tickets.name
then the Model associations will be as follows:
User belongsTo Ticket
in the User model we make a new association option like so:
Then in you're AppModel override the __generateAssociation method with the code below:
/** set a temp primary key in the associations in CakePHP 1.2 (and probably CakePHP 1.3 as well)
* @param unknown_type $type
*/
function __generateAssociation($type) {
parent::__generateAssociation($type);
foreach ($this->{$type} as $class => $assocData) {
if(!empty($this->{$type}[$class]['tempPrimaryKey'])){
$this->{$class}->primaryKey = $this->{$type}[$class]['tempPrimaryKey'];
}
}
}
Now you can use the tempPrimaryKey field instead of spending days rewriting the system. To re-build this db proplerly would be a dream, and a long- long, long... process. But at least now I have a great legacy tuck-away patch to easily make Model Associations work with tables that should've been rewritten long ago.
For those who like coding in Flex (now Flash Builder) and like Flex's component-based development, you can have a very similar way of working this code structure in CakePHP using elements and javascript. I will use the JQuery framework with CakePHP for a UI widget, and show side-by-side comparisons with a similar Flex UI widget. This post should be of interest to anyone who creates both Flex and Javascript/HTML front end UI widgets, or who uses flex and want to broaden their skills to use CakePHP and JQuery.
I've made many search forms that query thousands of records and can return as many results, thus requiring pagination. For the first three search forms i've created, it ended up being a re-education every time with many conditionals and general monkeying-around with a side of tom-foolery: passing the search form variables to the pagination helper, checking to see if search parameters have passed, if not then set defaults, etc, etc...
So after the third search form I decided to spend a little extra time to make the process more streamlined, and hope you can find it useful as well.
So there I was, in need of attaching a hamster to a balloon with an elastic cord; I had the balloon and the hamster, but was missing the critical elastic. I googled for a half-hour and stumbled across a great Actionscript class by Maxim Sprey (thank you Maxim!).
I converted Maxim's AS3 class into a Flex mxml rope component where you can attach two objects to each end, an anchored object and a swinging object (which would be the balloon and the hamster respectively). You instantiate the component in mxml like this:
Pretty self expanitory- swingObject is the object attached to the end of the rope that swings, and the anchor object anchors the rope. I've substituted buttons for the hamster and balloon in this example for simplicity, but you can see the hamster and balloon version at tjmillerdoesnothaveawebsite.com, one of my latest sites at the time of this posting.
Drag and release the small button below in the example to see the full elastic effect.
To view this page ensure that Adobe Flash Player version
10.0.0 or greater is installed.
Either scripts and active content are not permitted to run or Adobe Flash Player version
10.0.0 or greater is not installed.
My name is Brandon Plasters and I'm an avid web application developer. To help promote my business I've decided to blog what I've learned along the way, to contribute back to the coding community- of which I can thank for 90% of my coding knowledge thus far.
I'd like to thank Miha Nahtigal for the lilblogs CakePHP plugin I used for this blog.
I also figured it's time to use alpha-channel .png images on my blog design. For the purpose of this blog I don't need backwards compatability for IE6, the reasoning being that if you're visiting a code developer's blog with an out-of-date browser then you've got a thing or two to learn about the web before even thinking about visiting a developer's site. It's all about target audience.