Model Association with other keys

It’s hard to do an association with other keys than ids if you don’t know. But there is an easy way to do this:

var $belongsTo = array(

    'Inventory' => array(
        'className'    => 'Inventory',
        'foreignKey' => false,
        'conditions' => array(' `RentalLineitem`.`i_num` = `Inventory`.`i_num`'),
        'dependent' => false ) );

But watch out, this doesn’t work (see line with conditions):

var $belongsTo = array(

    'Inventory' => array(
        'className'    => 'Inventory',
        'foreignKey' => false,
        'conditions' => array('RentalLineitem.i_num' => 'Inventory.i_num'),
        'dependent'    => false
    )

);

I don’t know why exactly it hast to go with the ` characters, but my understanding is that it has to do with the db or sql respectively itself.

I have found my solution here: http://stackoverflow.com/questions/6267804/cakephp-mapping-belongsto-association-to-a-non-primary-key

Authentication with CakePHP 2.4

Set up an authentication environment with CakePHP 2.4 is not as easy as it could/should be, because the examples in the book are unclear for my (I don’t have the guts to say the book is wrong, because the people behind are smarter then me).

I wanted to have a login page which works with the new SimplePasswordHasher (from CakePHP 2.4). After a few line of code, it worked. BUT, as I noticed, it worked only with sha1, which is the default setting. I tried it with sha256 and md5, but there was no login. After a lot of searching and trying (more than a week) I have a solution which works now, but I think this is not the proper solution. I had to change the code in the User Model in the beforeSave function. See my new code below. Continue reading

Use “pw” instead of “password” as col name in the db

Don’t use the word password in the db for the col name. CakePHP uses this name inside. If you post a form with input fields name, email and password, then you can save the data more or less by itself. Then, the password will be saved as plaintext. Use as col name in the db for example the name “pw”. Then, saving is not as easy as bevore. You have to bring the input field “password” to “pw” (while hashing). That prevents plain text saving of passwords.

sitemap.xml in cakePHP 2.x

Creating a sitemap.xml is always a good choice. Information how to create a such file is described here: http://www.sitemaps.org/.

So, create a such file in cakePHP. At the end it will be here: http://www.yourdomain.com/sitemap.xml.

First, set the routing in app/Config/routes.

Router::parseExtensions('rss', 'xml');

Then create a controller file called SitemapController.php, a model file called Sitemap.php and a view file called index.ctp in View/Sitemap/xml.
Then, here is the code for the files: Continue reading

Passed, named or query string params?

Lately, I read an article form DerEuroMark. Read it here too! It’s very enlightening.

For the fast tracker, here is a short summary (got from DerEuroMark’s post):

Passed params

You use passed params where there is a definite order in your URL regarding those params.

/**
 * url: /posts/index/year/month/day/
 */
public function index($year = null, $month = null, $day = null) {}

Continue reading

bake with osx

This is not an entire tutorial, only a little helper. Create a profile-file under your user directory in osx. It has a name like “.profile”. See here how you can display all hidden files here.

I use MAMP, so in the file you need to have something like that:

alias cake="/applications/MAMP/bin/php/<strong>php5.3.6</strong>/bin/php /applications/MAMP/htdocs/projects/<strong>currentproject.com</strong>/app/Console/cake.php"

When you open the terminal and type in cake, then it takes your php version with your project.

For me, it works like a charm.