Tag Archives: CakePHP

Web based CakePHP Project generation tool

Yesterday I found a tool which could be handy for rookie CakePHP developers. Atlas is a web code generator based in CakePHP. As a glance it’s a web alternative of CakePHP bake console which is a nice idea indeed.

My first impression was that tool is like diamond in the mud and I was wondering how I didn’t hear about this tool till now, but after installing I realized that it’s not so good as it looked like.

Why?

The first problem occurs in the generation of my first project – I’ve got a error message in Spanish which I couldn’t understand. I had to use Google translate for it, but after the translation, the message still doesn’t say much – “Failed to generate project skeleton”.

After debugging the code figuring out why this error means, I found that the application uses backslash as directory separator (\) ?!? and of course this lead to directories and files with strange names like /var/www/ctest\app\controllers directories where ctest\app\controllers was the full name of the directory :). I am on Ubuntu Desktop machine where backslash is not a directory separator.

Pros and Cons about this tool:

Pros:

  • Very nice interface – really big point for Atlas
  • It should work properly on windows environment and based on the features list and screenshots it quite intuitive.
  • I like web based development everything is in your web browser, so another big plus.

Cons

  • Not working as expected – the backslash problem described above
  • If you are not Spanish speaking person – you are in the middle of nowhere when you see error like this: Error: no se ha podido generar el esqueleto del proyecto
  • No documentation at all. I had to look in the code in order to get idea why I cannot create a project – Not clear labels (or at least no hints). Probably a hint like: Directory (enter a full path to the empty directory under htdocs) or similar could help a lot.

The conclusion:
Atlas looking like promising tool, still it’s far from the final version, but it’s worth to try it in Windows environment. πŸ™‚ I definitely will follow that project in the future, so hopefully I could use it one day πŸ™‚

How to use aggregation SQL functions with CakePHP

Have you ever wondering how to find MAX, MIN or AVG of a database table column in CakePHP way? Well I haven’t since yesterday. The solution is quite simple and obvious.

Lets say you want to show the average views per post (in the example we have the views field in every post).
The SQL should be something like this:

SELECT AVG(views) FROM posts

Instead of using query() function you can use the CakePHP way:

$this->Post->find('first', array('fields'=>array('AVG(views) as avg_views')));

Hope this will help someone.

JSON and CakePHP

Last few articles in this blog are only for stupid problems and unfortunately this one will be the same.

I had to use JSON to update some chained select boxes. I am using one jQuery plugin for this. The problem is that if you code, most likely your debug setting in the core.php file is set to 1 (or 2). This way the developer can see errors from CakePHP.

When the level is set to 1, at the end of the page, there is an comment, displaying the processing time for the page, but when you use JSON, this comment causes troubles (probably, because JSON doesn’t accept this type of comments) and the JS fail to process the response.

In order to check JSON, I needed to disable set debug level to 0 and after this to reset it again to 1 (for debugging purposes in other parts of the application).

So, the conclusion: bear in mind, that JSON doesn’t like HTML comments.

Hope this helps.

linxspy.com – track your links

Today I want to present you a small service which I wrote for a week /including the design/. πŸ™‚

I’ve wrote it, because few times I’ve exchanged some links with strangers and few weeks after this, my link was disappeared from their sites while their was on mine. It is a bit unfair isn’t it? I know that link exchange is not promoted from Google, but people still do it. Do you? πŸ™‚

The service is called Linxspy.com and in general it is a simple interface, where you can define your links and partner’s websites. On a regular basis /at this moment every week/, the service will check the partner site and will look for your link. If everything is ok and link is there, no action will be taken, but if the link is missing, you will be notified by mail.

It should be very usefull for web masters which exchanging or buying links with unknown or not well known partners.

Basically if everything is cool with your partners, you should forget about Linxspy, because it will bother you only for troubles. πŸ™‚

A little bit for implementation: As I said it was build for a week and as you probably guess it’s CakePHP app. On this project I’ve used Console Shell interface for triggering the weekly check.

Here you could find more information on about the service page

Here are some screenshots from Linxspy:

Hope it would be useful for you.

CakePHP ajaxed pagination and sort

Here I will describe how I did an Ajaxed pagination for one of my projects using jQuery and Livequery Plugin. Currently I am working on a huge project which, hopefully, will feed me with some ideas for blog posts and this is one of them :). So:

1. Loading the javascript libs

Loading the jQuery and Livequery was done by Autoloader helper, but if you don’t use it just include jQuery and Livequery in your layout with following code

<?php
echo $javascript->link('jquery.min');
echo $javascript->link('plugins/jquery.livequery');
?>

Continue reading

Passing variables to all Models from the Controller – strict MVC way

It’s really easy to find inspiration when you developing on CakePHP. For the current project I need to create global (for all models) behavior which stores creator and modifier as well as deleter and deleted (for soft-delete).

What are the requirements

1. For every table (or at least the most of them) there are fields 6 fields:

  • creator (int) – user who create the the record
  • created (datetime) – date when the record was created
  • modifier (int) – user who modified the the record
  • modified (datetime) – date when the record was modified
  • deleter (int) – user who delete the the record
  • deleted (datetime) – date when the record was modified

2. Some tables doesn’t have such fields (like aros, acos, settings etc), so it should update tables which has such fields.

I’ve seen Soft Deletable Behavior as well as recently added WhoDidIt behavior, but I don’t like the approach of using _SESSION. I’ve also decided to use datetime field to determine if the record is active (field deleted is NULL) or deleted (if the field is set with date).

So the result was – I’ve write my own behavior who serves this.
Continue reading

Strange issue with captcha field

Captcha caseWell, it’s more an observation than a real problem.

For my current CakePHP project I had to set a captcha field to a user registration form. I decided to be “creative” and I set the captcha image as background of the input field. In fact the graphical representation wasn’t bad at all. Unfortunately when it’s used, the captcha word shows wrong image and of course the result was wrong. To be correct the first attempt was always ok, but the second one (let’s say if the user decided to register again) the captcha was always wrong.

I checked everything especially after the image in Javascript library case and I couldn’t find anything suspicious.

The solution came when I removed the image from the background of the field and call it with an <img> tag. This time everything went just fine. This make me think that it’s a cache problem. I didn’t dig it deeply, because for now it’s working and as you can see it’s looking better.

So, an advise from me – never put your captcha image as background of your field, or call through css. Just use good old <img> tag instead.

Book review: CakePHP Application Development

CakePHP Application Development

In the beginning of December I’ve been asked to write a review of a book called CakePHP Application Development. The interesting thing was that I received a copy of the book exactly on my birthday – 29-th of December (probably consequence, but it was really nice). In this article I will write about what this book covers and my opinion of the content. Hope it will be a good guide for these of you who will decide to buy a copy of it from Amazon.com.

What this book covers

First two chapters start with some theoretical explanations about concepts and patterns which are used in the Framework as well as the process of installation of the framework.

The next chapter explain how to build a really simple application. The readers could use to with the framework way of coding, everything in this chapter is hand crafted, so people could actually see how it’s works.

After this there are 4 chapters which explain in details how the Models, Controllers and Views are working in details.

There is one chapter which explain how to build an scaffolded application with the Bake console script. There is an explanation how to create Models, Controllers and Views.

The next chapters explain how to build a real world example with CakePHP called Quickwall (Questions and Answers), which is different from standard blog creation example. In these chapters the authors upgrade and optimize the app and expand it’s functionality, so at the end it could be used in the real world.

The good things

  • Even though it’s really difficult to write for a framework which still is in development, where some coding conventions could be changed, this book cover almost everything which is currently used in CakePHP1.2
  • The book is build with example driven approach which is really helpful for understanding from the developers
  • Each article is separated in sensible 3 sub-sections: introduction, or theoretical part, Time for action and What Just Happened. This approach is really useful, because first, explained what will be done in the current chapter, the second part display the actual code snippets and the third one explain step by step the code presented in the previous part. For me it’s really good approach of writing a technical book
  • Using non trivial building a blog is also a plus, I personally don’t think that anyone will write a new blog platform, just because for fun. The Quickwall application could be used in real world without any problems.

What I didn’t like

  • It’s opposite of the first thing in the previous list – just because the framework in still in motion the book is jist a little bit outdated. There are few code snippets where I found that for example condition word is in the right part, while at this moment it’s moved to the left. The code in the book was:
    'conditions' => array('ModelName.field_name' => 'LIKE value')

    while it should be

    'conditions' => array('ModelName.field_name LIKE' => 'value')
  • There are some things which are missing although they are well known – the behaviors. The same as the authors explained about the components and helpers, they should take a moment and explain the behaviors as well. In fact this is totally missing in this book.
  • Although the validations are well explained in the book, I miss the list of methods provided from the core.
  • There isn’t any information about the internationalization, which for me as developer working with many people and different languages is essential.

The conclusion

I would recommend this book for PHP developers who are new to the framework and they are willing to understand the core functionality of it. With the good approach of introduction-code-explanation, it will be really easy to learn how to use the CakePHP. This book could build a base, but it cannot create from you an advanced CakePHP developer (well I think it’s not the main purpose of it anyway). The learning is ongoing process and people which are willing to learn could start with this book, but after this, they need to continue reading other materials, as well as start with real development tasks, because the practice is the best teacher invented ever.

Here is a sample chapter from the book kindly provided from Pakt Publishing

Reviews by others: