The dummy mistake while coding

CakePHP, Development, Frameworks, General, PHP July 4th, 2008

No body is insured from stupid mistakes while writing code. I am not insured, but subscribed for such things. :)

Here is the story:
Yesterday, while I was working for a small project I lost 1 hour to detect the stupidest mistake I ever made. Here I will explain what happened and how dummy I fee after that.

I am writing an application where users insert records into the database, and after that on every show of the record, there is a counter field which need to add up the number in it. Simple and easy situation.

While I was working I noticed that only one record /from 5 in the database/ got updates on his counter fields. All other had 0, while I was petty much sure that I’ve seen all these records on the screen. Well, I forced showing the 0 rows but the result was the same. Then I start investigating what could be the problem, but in the database everything was just fine. I deleted all records and started inserting texts /because there are some text fields important in this story/ one by one. Strangely, but again some of the records got update, some not. Finally /by accident/ I noticed, that counter changes when there is a space before or in the end of the value of the text I entered.
Read the rest of this entry »

Using more than one database connection in CakePHP

CakePHP, Development, Frameworks, Labs, PHP April 10th, 2008

Probably is pretty obvious for most of the advanced users, but when I started my experience with CakePHP I read a lot of configuring a database, but not multiple ones. There are articles how to use database for development and for production without changing anything but rewriting a function in DATABASE_CONFIG class.

These days we need to choose way of splitting the functionality and we have to decide: Shall we use different databases or shall we put a prefix for every table. So far we choose to use prefix, but I dig into this and I realized that every model could be attached to different database.

Here is the example how to do this:
Read the rest of this entry »

Security Issue in CakePHP

CakePHP, Development, Frameworks, General, PHP March 29th, 2008

I notice this recently when we start creating the security component of a project. The best way to explain the issue is to give an example:

Imagine that in your application you create an action “edit profile” where each user can change his personal details for the account. If course there will be fields for changing user’s real name, password, email etc., but the username field should be readonly.

The direct approach to create such page is to reuse “edit” action of the user’s controller, but instead of getting the ID from the url, it need to be fetched from the session’s auth variable. The second thing which you have to do is to remove “username” field from the view and you are done. :) Well, fast and easy, but not very secured…
Read the rest of this entry »

htmlSQL class - quite nice way of parsing html

General December 11th, 2007

Today I found a class written in PHP which implement the idea of using SQL syntax while parsing HTML documents - htmlSQL. The idea of the author - Jonas David John - is quite simple. If you want to parse a HTML document, let’s way you want to parse all divs with class “row”,the syntax will look like normal SQL:

SELECT * FROM div WHERE $class == "row"

Looking familiar isn’t it? There is also function connect() /which define the html source/ and fetch_array() which contain the results after the query.

There are few examples included in the library package and here is the simplest one:
Read the rest of this entry »

CakePHP and Layout - secret of data passing through

CakePHP, Development, Frameworks, PHP October 10th, 2007

In my current project I need to deal with the Layout more than adding some header, static menu and footer in it. I need to have dynamic menu loaded from the database plus additional blocks which will contain some data related to the main content. For example YouTube.com /and many other sites/ has some blocks which appear all over the site like related movies block from the right depending from the tags related to the main movie. There are a lot examples.

Well, searching in the CakePHP Manual, CakePHP Google Group or Bakery doesn’t satisfy my understanding how to solve the case. Apart from $anything_for_layout: Making HTML from the View available to the layout article I didnt find anything on this topic. And this one sounds like a hack rather than a complex solution.
Read the rest of this entry »

CakePHP and Character Set in the Database

CakePHP, Development, Frameworks, General, PHP October 1st, 2007

Today I found something in the CakePHP which saves me a lot of effort when I need to migrate the database as well as searching for non-English characters from the Database. What is the story:
I had 2-3 projects build with Cake which were for non-English environments. That means the localization of the projects as well as storing proper data into DB with proper encoding. The current project is the same.

In these projects I saw that the data was not stored properly in the MySQL. From the Cake project interface everything looks ok, but when I open phpMyAdmin - the chars looked like strange symbols /not ??? neither ӗ&୶/. So far I ignored this because as far it is working from CakePHP interface for me everything was ok.
Read the rest of this entry »

Extended Autocomplete Helper

Ajax, CakePHP, Development, Frameworks, General, PHP September 3rd, 2007

When I started with CakePHP, my first project includes also Autocomplete functionality for some fields. Of course I start using default Ajax Helper provided from the library. The main problem was that in most of the cases I needed the ID of the string which I searched. The best example is the Country list which is very easy to be accessed with Autocomplete, but instead to messing up with strings as usual developer I wanted to have the ID of the specified country. I used some kind of hack by adding callback function onComplete, which checks the selection in the Autocomplete field, and by this selection set hidden ID field, but as you can imagine this is not the best choice, first of all because it’s a hack - you rely on a sting which already sound as a hack /imagine if you are searching in a list there are some duplicated entries - examples are too many/ and second if you rely on callback this mean that you need another second or two for the second responce. Anyway, by giving this examples just wanted to convince you once again that Autocomplete helper with Key and Value related are really necessary in the projects /or at least that’s what I am thinking./

Well so far the default Autocomplete helper doesn’t provide such functionality. That’s why I created this helper which solves this problem partly. Why partly? You will see the answer of this in Strict Autocomplete with Scriptaculous (Part II)
Read the rest of this entry »

CakePHP - my new best friend.

CakePHP, Development, Frameworks, General, PHP August 28th, 2007

From the beginning of the year I decided to move from my home-grown PHP Framework to CakePHP. Here I will explain how I moved and which were the reasons for this. I will mention only the important things which drive me to this decision.

So here are the most critical issues while you using Home-grown or Community Frameworks:
Read the rest of this entry »

Komodo - My editor in Ubuntu

Development, Editors, Ubuntu May 4th, 2007

since I start using Ubuntu from version 6.06 I had problems finding the proper editor fitting my needs and having similar features like UltraEdit. So I tried all provided by default from standart Ubuntu installation: Ajunta, BlueFish, Screem, but no one match my requirements and I start using gEdit. not bad for simple operations, fast, but macros are applied only to file type: for example if you want to add HTML macro printing directly basic html structure of the document to a *.php file - you cant. It’s only for HTML files. Also suporting function sintax is not the first thing, which you can run on gEdit, but this was the reality - this editor suited me best.

Well a year ago I saw the Komodo Edit and I love it! it looking 99% the same as UltraEdit and also support Macros, there are Syntax Check etc.
Language integration is suported very well and there are hints about functions and their attributes.

What I miss is Code Inteligence API for CakePHP, because I am using that Flamework for building my latest projects. Let’s hope somebody will make cix file for it.

Probably in other posts I will mention about my projects. For now - that’s it