to print the data

You can use the pr() or debug() functions to print it out if you want to see what it looks like.

Advertisements

CakePHP conventions

constroller:
Controller class names are plural, CamelCased, and end in Controller.
ex: PeopleController and LatestArticlesController

URL Considerations for Controller Names:
single word controllers map easily to a simple lower case URL path.
ex: ApplesController is accessed from http://example.com/apples

File and Class Name Conventions:
The Controller class KissesAndHugsController would be found in a file named KissesAndHugsController.php
The Component class MyHandyComponent would be found in a file named MyHandyComponent.php
The Model class OptionValue would be found in a file named OptionValue.php
The Behavior class EspeciallyFunkableBehavior would be found in a file named EspeciallyFunkableBehavior.php
The View class SuperSimpleView would be found in a file named SuperSimpleView.php
The Helper class BestEverHelper would be found in a file named BestEverHelper.php

Model and Database Conventions:
Model class names are singular and CamelCased.
ex: Person, Post, User, CountyState, and PostsTags
Table names corresponding to CakePHP models are plural and underscored.
ex: people, posts, users, country_states, and posts_tags
Field names with two or more words are underscored
ex: first_name, last_name, user_name

View Conventions:
The getReady() function of the PeopleController class will look for a view template in /app/View/People/get_ready.ctp

 

_________________________________________________________________________________________

Database

Database tables are plural: “comments” or “user_comments” etc.

I recommend to stick to conventions and underscore + lowercase all table fields: “last_login” (instead of “lastLogin” or even worse “last login”) etc.

Models

Model names are singular and camelCased: “Comment or “UserComment” filename: “comment.php” or “user_comment.php”

Controllers

Model names are plural and camelCased: “Comments or “UserComments” filename: “comments_controller.php” or “user_comments_controller.php”

Controller actions should be underscored! “function admin_import_from_xml(){}”

Views

Views have an own folder for each controller – plural: “comments” or “user_comments”. Inside are the templates for the specific actions- underscored. filenames for example: “index.ctp” or “admin_index.ctp” (with prefix admin) or “admin_order_entries.ctp”

Libs

As long as namespaces are not an issue, they might conflict with existing model classes (they have no “Model” appended!) and other cake core classes or even some vendor files. I recommend to use the controller syntax here: Name – camelcased: “GoogleTranslateLib” filename: “google_translate_lib.php”

This way it won’t interfere with any Google class – or “FileLib” won’t interfere with core “File” class.

Behaviors

Name – camelcased: “GeoPlugin” filename: “geo_plugin.php”

They should not be similar to model names. Otherwise they might interfere with existing models. So use adjectives, verbs or plural names: “Ratable”, “Geocoded”, “Configurations”, …

Components

Name – camelcased: “GoogleTranslateComponent” filename: “google_translate.php”

They could interfere with models (and maybe behaviors). So it might make sense to use plural forms or forms that are not likely to be a model name.

Helpers

Name – camelcased: “GoogleHelper” filename: “google.php”

With the new 1.3 “$this->Helper” style they can now be anything you want. With a little modification of the core, at least. I like to call my helpers “MyHelper” if there are specific to a controller/module and it is not yet a plugin. Example: Conversations controller + Conversation model + MyConversation helper (as it is only needed inside this specific controller). Other helpers like “GoogleMap” might be used in several controllers, for instance.

Plugins

They should not be similar to controller names. Otherwise they might interfere with existing controllers. So use adjectives, verbs or singular names: “Rating”, “Setup”, “Configuration”, …

Constants

No matter if they are class constants or global constants, they are always uppercase and underscored: “MIN_USER_AGE” etc.

Variables

camelBacked: $userComment

The first letter is uppercase if the variable represents an object: $File = new File(); $GeoPlugin = new GeoPluginLib();

Layout/Script

CSS classes/ids: I use camelBacked style here, too: <div id="someId"></div> But thats purely a matter of taste. I like to use it in IDs because I can easily attach “-x” (the record id) and split it afterwards on “-” to get the record id in JS: … id=”someId-2″… comes in handy sometimes

Test Cases

Same as the original file – only with “.test” appended to it: “google_translate_lib.test.php” for instance.

How to create custom MySQL queries in CakePHP?

Location Controller should be:

loadModel(“Location”);
$this->LocationModel->getLocations(); // I will strongly discourage using get()
}
}

Location Model should be:

query(“SELECT * FROM locations;”); // if table name is `locations`
return $this->query(“SELECT * FROM Location;”); // if table name is `Location` since your public name is `Location`
}
}

CodeIgniter Outline

1 Introduction
2 Installing Codeigniter
3 Refer the Codeigniter User Guide Text Preview

SECTION 2: BASIC CONCEPTS
4 Theory behind Codeigniter – MVC
5 Routing Basics

SECTION 3: CODEIGNITER BASICS
6 Controllers
7 Views
8 Models
9 Helpers
10 Libraries
11 Using Static Files

SECTION 4: USING THE DATABASE
12 Introduction to Databases Text
13 Connecting
14 With SQL
15 With Active Records

SECTION 5: MINI PROJECT
16 Introduction
17 Creating the Database
18 Creating the base template
19 Adding a Blog Post
20 Loading all the posts
21 Loading a Single Post
22 Delete Post Functionality
23 Recap + Exercise
24 Source Code with Database SQL 2.19 MB

SECTION 6: SECURITY
25 XSS Prevention
26 SQL Injection Prevention
27 Hiding PHP Errors
28 Form Validation Text
29 CSRF Prevention

SECTION 7: CONFIGURATION
30 Config Files
31 Autoloading
32 Remove index.php
33 Change Application Folder
34 Advanced URL Routing

SECTION 8: EXTEND CODEIGNITER
35 Creating a library
36 Extend a library
37 Adding a helper
38 Extend a helper
39 Extending Core Classes

Explain the differece between mysql connect and pconnect.

mysql_connect() and mysql_pconnect() both are working for database connection but with little difference. In mysql_pconnect(), ‘p’ stands for persistance connection.

When we are using mysql_connect() function, every time it is opening and closing the database connection, depending on the request .

But in case of mysql_pconnect() function,
First, when connecting, the function would try to find a (persistent) connection that’s already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.
Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the connection will remain open for future use (mysql_close() will not close connection established by mysql_pconnect()).

mysql_pconncet() is useful when you have a lot of traffice on your site. At that time for every request it will not open a connection but will take it from the pool. This will increase the efficiency of your site. But for general use mysql_connect() is best.

_______________________________________________________________________________

  1. mysql_pconnect() is deprecated as of PHP 5.5.0, and will be removed in the future because it has several disadvantage.
  2. Major disadvantage is that mysql_close() didn’t support connection created by mysql_pconnect(), connection remains open for future use.
  3. When we establish connection with mysql_pconnect() then it will search previous existing connection with same hostname, username and password.If old active connection found then it will not recreate new connection.
  4. In case of PHP most of the sites hosted with DB and PHP on same machine, so connection time is almost negligible, in that way persistant connection is useless.
  5. For Web-Request there is no need to make persistant connection, because HTTP is sateless protocol.
  6. Connection will automatically close when we refresh page, because it assumes every request as new request.
  7. Persistant connection need too much resource, that’s why persistant connection is Highly Discouraged.
  8. Normally Temp Tables dropped when connection close, but persistant connection do not close so Temp tables are no more temporary.