Learning CakePHP

April 2, 2009

On Being Hard-Headed: Not following naming conventions

With all the features and possibilities cakePHP can offer, one sometimes find it hard to always adhere to cake’s naming convention. Like a table definition in your database should be always plural. The class name of the model for that table will be the singular name of the table and so on and so forth.

But as in some cases, someone (like me!) will not always want to follow this convention. Like for example, if I would want to create a table that shall contain some info’s on the site: amazon.com,  I would like to be firm on naming my database table as amazon and not amazons. 

So for that,  we have to tell cakePHP that the model amazon.php shall use the table amazon and not amazons.

So inside amazon.php:

<?php
  class Amazon extends AppModel {
    var $name = Amazon';
    var $useTable = 'amazon';
  }
?>

Or if you would rather not use a table at all:

<?php
  class Amazon extends AppModel {
    var $name = Amazon';
    var $useTable = false';
  }
?>

April 1, 2009

Tips and Tricks: Allowing Global Definition of Components and Helpers in CakePHP

:D

I am starting to create my layouts and pages. But I encountered this problem (much to my annoyance. How much errors could I take?):

Notice (8): Undefined variable: javascript [APP/views/layouts/default.ctp, line 14]

I remembered encountering this before on my localhost development. So I browsed my local files and see that I created an app_controller.php inside app/controllers/

The controller just have these codes:

<?php

class AppController extends Controller {

  var $helpers = array(‘Html’, ‘Form’, ‘Ajax’, ‘Javascript’, ‘Time”);

  var $components = array(‘Session’); 

  }

?>

which just enables you to allow the use of the following helpers and components all throughout your cakePHP application without redefining these on individual controllers.

so. ciao! :D n

Strict Standards: CakePHP Fresh Install

:) . It’s funny how my enthusiasm to refresh my cakephp knowledge turns to be an ‘ordeal’. I am now facing one error to another. After solving the .htaccess (Namely the 500 Internal Server Error) I am faced with yet another unfamiliar error. A number of errors show on my freshly installed cakephp.

strict-standards-error1

 

My first instinct is that I have to change the “debug” setting mode of cakephp in app/config/core.php. However that is not the case. So I relied again to google and it (keyword search in Google: cakephp Strict Standards: Redefining already defined constructor for class Object in) returned a number of results and the first search result gave me an immediate most direct solution.

According to the blog, MAMP, PHP5, CakePHP, and Strict Standards, this is not a cakephp problem and instead a new PHP5 Error Standard (Oh my! another trouble with the new PHP 5) which can be turned off in two ways:

 

  • Changing your php.ini 
  • Or modifying the .htaccess in your root cakephp installation

The first method requires access to the php.ini (which I do not have) so I went on two solution # 2:

Or, in your .htaccess file in your root cake installation, you can add the following line:

php_value error_reporting 6143

And when I did that, I refreshed my page and saw that the error’s gone. (Yipee!!!)

It is also quite interesting to note that the reason why this error exists (still exists) is to maintain back compatibility with older version of PHP (that is PHP4).

More powers to the Open-Source Community and to the people who continually share their knowledge and experiences for people like me who is just starting to learn (really learn).

Cheers! 

CakePHP: 500 Internal Error

Filed under: CakePHP How-To's, Idiot's Guide — Tags: , , , — ldazo @ 5:49 am

I decided it’s the time I build my own application on cakephp. So I downloaded the latest release from cakephp.org And uploaded them on my server in mosso.
I got a 500 Internal Error.

Looking up the web again. from various help, experiences and FAQs in blogs and other sites I found two articles:

In mosso (cloud sites), according to their knowledgeBase, due to their unique hosting environment, they require a slight addition to the code used for mod_rewrite. That is, an additional  line of code is necessary to make it work.

The RewriteBase should be specified for the root directory – / or to the subdirectory of the application. This is written after stating RewriteEngine on as follows:

RewriteEngine on
RewriteBase /

This should be specified in the .htaccess located at the web/content directory.

It is indeed quite frustrating encountering a problem you do not know the cause (since the page just tells you to contact the root-administrator of your site). Especially when you have already developed a whole cakephp application that is already working on your localhost and later finds out that an error  of this kind is thrown out upon uploading the entire cakeApp to your server.

It’s good thing that a lot of people are sharing their experiences in their blogs or forums so that people who might have experience this kind of problem has a reference.

October 24, 2008

CakePHP Blog:: Database Structure

Filed under: My Blog Application :) — Tags: , , — ldazo @ 8:40 am

I need the following tables for my blog application:

  • categories
  • posts
  • replies
  • users

There after some thinking I have thought I needed only four tables. :)

Of course, the posts -> non explanatory. Posts should be  categorized and are written by users. The exercise only asked for one user so the users table is almost unnecessary.

(more…)

My First CakePHP Blog Application

Filed under: About, My Blog Application :) — Tags: , — ldazo @ 8:24 am

I build this using cakePHP 1.1.19. It was an exercise for a company I applied for.

The task was simple, build a blog application using cakephp. I was given 3 days to work this out.

From thursday night(I think.) to saturday night. So technically, I had two nights and one whole day to do it.

Let us, disregard the fact that it had been almost a year since I last dealt with anything that has something to do with cakePHP framework. But having just this span of time to build something, I do not know if I could do it.

Mainly because I have not really made a whole cakephp app on my own. I do not know how to handle sessions in cake, make a simple validation and other simple things like those. But I have a clear picture of how should my blog application should look like. Because I’ve been blogging for sometime. And I have been using a number of online applications that I think, I know what is expected from this work.

And so I accepted the challenge and spent the next hours reviewing cakephp basics, building my database and then baking… Ooopss.. I have forgotten the basics like apache mod_rewrite and how to actually use the bake script in Console.

Before I had difficulty with this. I have forgotten that changing configuration files in your server means you have to restart xampp again..

This and all that. I was able to finish the application on time. So I was happy. This exercise made me want to study cakephp more.

script to generate cakephp app -> changed

Filed under: Idiot's Guide — Tags: , — ldazo @ 6:53 am

I have jumped from cakePHP 1.1.17 to cakePHP 1.2.0 and there have been a lot of changes particularly the bake generator.

Then, i use the bake.php file located at the /cake/scripts/bake.php

(Since I am using Windows XP)

On the console i cd to C:/ then assuming my cakePHP app is contained under cake_app folder:


C:\>cd xampp/htdocs/cake_app


C:\xampp\htdocs\cake_app> C:\xampp\php\php.exe cake\scripts\bake.php

and the bake script runs.

Now the generator is run through cake.php inside the /cake/console folder.

If you do things without reading first, you’d be damned.

I had been..

About this Blog

Filed under: About — Tags: , , — ldazo @ 1:57 am

I know, i know. CakePHP has its own ‘nice’ framework to build a blog. So why am i using wordpress instead?

It’s free.

I’m poor, and I have to make use of the free-available resources. Soon maybe, i’ll put my own site using my own domain to build a cakephp application, but hey.. right now, i need a place to get attention, that is to blog what i know, what i learned and hopefully someone will learn from it to and most of all someone would tell me maybe what i know is wrong. I just need an online journal not because i want to brag or something. I just feel that I need one.

I already have a wordpress account.

It is frustrating for me to keep several accounts. Since i already have blogs in wordpress, it’s just another one-step registering another blog.

Fetching Json Data inside Views

Filed under: Building CakePHP with ExtJS, Idiot's Guide — ldazo @ 1:34 am

I’ve been reading hydra’s and djzone’s blog as a reference for starting a cakephp+extjs application. Their tutorials about building the grid using json are almost the same. Following their tutorial step-by-step, I failed to obtained the desired result. That is – a grid showing my data.

I think the root cause of my problem is when calling the url containing my json data, extjs is not able to recognize it as a json data. (Am i going in circles? I, myself, don’t even know how to put this problem in words.)

Coz, when i try to change the url in the data store into a page outside my cakephp app, the data inside the grid shows. But, what the heck, i have to be able to dynamically change the json data based on the content of the users table and having a static json array won’t do. I need, of course, cakephp for this.

So, I’ve been thinking on the points where I did wrong.

Retracing my steps, i think that I have followed hydra’s and djzone’s instructions. I almost wanted to restart my cakephp app from scratch like going back to the versions of cakephp and extjs they used just to see where did I go wrong.

This is when i paid attention to the response received by my page.

And I think I got where my mistakes were.

  1. I should have made sure that the page holding my json data contains nothing except the json array. Nothing except it.
  2. This means i have to make a blank layout that only contains the following code:  <?=$content_for_layout;?>
  3. and i have to assign this layout inside the controller function that will call my json data.
  4. This ensures that I get a page with no excess tags.
  5. Next, I have to configure my /app/config/core.php and set debug to 0.
  6. This step, i think is what made me go around circles. Since ive set this to 2 because i am a lousy coder so, i need something to tell me what line where i misplaced a single semicolon.

With these, i was able to make my grid work.

This article does not tell you how to build your extjs grid inside your cakephp app, because many good people have been contributing to the community for this purpose. I think, I wrote this just in case, someone like me has been following the blogs of many people and yet still don’t know where they had gone wrong, and maybe they have the same situation as mine.

Kanpai!

October 21, 2008

My First Cake Experience

Filed under: About — ldazo @ 8:40 am

Ironically, i do not have any knowledge in PHP when i ‘had‘ to do my first cakePHP project. Had because i was not supposedly a part of that project. But because of some problems, i was involved with it.

The instructions were simple and the tasks involved were easy. I was told that:

CakePHP is easy. You just have to understand Engish.

Oddly enough, the instructions were right. Using CakePHP’s generator we are able to generate the necessary forms we needed along with the ‘controllers’ (action receivers).

All we have to do is fix the VIEW Files with some CSS and were set to go.

Because cake’s generator gives crude views, we have to edit each html files to suit our needs. See how ignorant i was in the world of programming… It was only later that i learned how to really use CSS to the fullest.

They were enough but not quite and so with no one to turn to, I took a step forward and study the framework in question.

For someone like me who is new to the world of web programming, studying a framework built on a language i don’t even know is not easy. In fact, I feel like a student in a Calculus course who does not even know how to derive equations much more, do integrations.

My teachers were the bakery and the cakephp manuals. It was, at first, like reading a reference book written in german. I couldn’t understand the codes but i do understand the logic. Knowing what is needed, i searched for answers and obtained them. It took me a week to be able to comprehend the essence of the framework and be able to use it in the project.

Fortunately we get by. I was happy being able to do something helpful in the end. It was because of this project that i learned the basics of PHP and the framework itself.

I was not satisfied. Of course, I am the type of person who is always eager to know more, to learn more, to do more.

And so every so often (if time permits me) I do some small coding and stuff and study cakePHP. For some reason,  I am hooked with it.

Up to now, i am still a nobody when it comes to cakephp.. But I am quite determined to really dwell into the subject.

:D

Blog at WordPress.com.