Cakephp has a good ways for handling table relationship model using AppModel association structure. It is easy to create and set relation between two or more tables on your database. Cakephp has setting for one to one, one to many, many to many relationship.
I would like to tell you about setting foreignKey for one to many relationship in cakephp that the master table doesn’t have foreignKey id. On your model, the relationship for one to many that set on child table would look like this
<?php
class CurrencyRate extends AppModel {
var $belongsTo = array('Currency' => array(
'className' => 'Currency',
'foreignKey' => 'currency_id',
));
}
?>
(more…)
Cakephp have great routing capability, i was install wordpress blog on my main website root directory. And then i need to move my blog into new folder, i put it on /app/webroot/blog/ because i build new web application on root directory using cakephp.
I use rewriting rule for my wordpress blog. The rule is http://www.balistupa.com/year/month/slug/. I want to redirect into http://www.balistupa.com/blog/year/month/slug/. I don’t want to loose my blog visitors because the blog posting has good ranking on google.
So i do setting on app/config/routes.php and add this line
Router::connect(‘/:year/:month/:slug’, array(‘controller’ => ‘pages’, ‘action’ => ‘redirect_blog’), array(‘pass’ => array(‘year’, ‘month’, ‘slug’), ‘year’ => ‘[0-9]{4}’, ‘month’ => ‘[0-9]{2}’, ‘slug’ => ‘[A-Za-z0-9-]+’));
(more…)
At this time, a company website normally should have blog to post some business activity or related activity with website themes. WordPress is the best blog framework (CMS) that i ever used. When i was build web application under my own php framework, i wasn’t worried about integrating wordpress into my website. It would has separated setting with main application.
However, now i am using cakephp framework. And if we need to put file or folder, it need to be put inside app/webroot folder. So if i need to install wordpress blog in http://www.balistupa.com/blog, i need to put the blog folder inside /app/webroot/.
The problem is about the url, with standard setting the blog would be redirected to http://www.balistupa.com/app/webroot/blog/ not to http://www.balistupa.com/blog/. This is not look good for visitor and also for seo. Because the folder structure is to deep.
(more…)
Actually there is no Thread.sleep method on javascript that work on Threading. Moreover there is no true threading in javascript. So everybody that want to deal with threading need to emulate it. In this article i would like to show you some method that emulate sleep method for waiting on a process. Each of this method has each own disadvantage, so you need to choose which is suitable for your case.
First method is sleep using javascript loop. This is first method that come to my mind when emulate sleep method on javascript. We wait for the process by checking the time in the loop.
function wait(millisecond) {
var date = new Date();
var current_date = null;
do { current_date = new Date(); }
while(current_date-date < millisecond)
}
// do something
wait(1000); // wait 1000 millisecond
// do something
(more…)
Sometime we need to handle or do something when user closing a browser windows or page. Maybe we want to ask user if they really want to close the page or it was a mistake click. This procedure all can be done using javascript.
There are two condition or event that you can intercept before it really happen, first when user close the browser windows and second is when browser windows will be closed without any way to abort it.
For first condition we can handle it using window.onbeforeunload event.
function closeHandler() {
return “Are you sure you want to close this page?”;
}
window.onbeforeunload = closeHandler;
(more…)
Sometime we need to print the structure data that return by a function. I was work on a task for google map and need to print object properties of Placemarks object on javascript. I need to analyze what kind of structure data return by geocoding function. The code below is a short function to print object properties in javascript
function isObject(obj) {
return obj.constructor == Object;
}
function print_object(object, level){
var tab = “”;
if(level > 0){
for(i = 0; i < level; i++) tab += “\t”;
}
var str = “”;
for(prop in object) {
if(!isObject(object[prop])) str += tab + prop + ” value :” + object[prop] + “\n”;
else str += tab + prop + “\n” + print_object(object[prop], level + 1);
}
return str;
}
and how to use this function is really simple approach
address = addresses.Placemark[0];
alert(print_object(address, 0));
in this scenario, i would like to print first placemark object return by geocoding function. Parameter level is use to make nice print view using tab character.
Before June 2008, there is no clean implementation for GROUP BY. commonly people code with CakePHP will put GROUP BY sintak on conditions, something like this
$this->Product->find(‘all’, array(‘conditions’ => ’1=1 GROUP BY Product.category_id’));
Now CakePhp have a clean sintak for GROUP BY by new additional paramater on find function. We can use ‘group’ to define GROUP BY on cakephp. For example
$this->Product->find(‘all’, array(‘fields’ => array(‘Product.category_id’, ‘COUNT(Product.hotel_id) as total’), ‘group‘ => array(‘Product.category_id’)));
How if we want to have HAVING sintak on query? i ussually use this solution for new cakephp
$this->Product->find(‘all’, array(‘fields’ => array(‘Product.category_id’, ‘COUNT(Product.hotel_id) as total’), ‘group’ => array(‘Product.category_id HAVING COUNT(Product.hotel_id) > 1′)));
Sometime we need to pass data or parameter to another Activity on Android. Only one activity is active at once. An activity open new activity for result and opened activity need parameter to set their interface or another option based on request. So it is important a system can handle sending and retrieve parameter between two Activity.
To open an activity and wait for a result, we can use this sintax
Intent newIntent = new Intent(this.getApplicationContext(), ActivityClass2.class);
startActivityForResult(newIntent, 0);
ActivityClass2 is the class name of activity that we need to create and then open it from current activity. startActivityForResult is a method to run newActivity. Later we can have the result by add this function on current Activity (more…)
By default, belongsTo and hasOne using LEFT JOIN relation between parent and child table or model. We can forcing the relationship to use either INNER JOIN or LEFT JOIN based on our need. Cakephp has add new option when binding model, name type: value is LEFT or INNER.
For example we have parent table Author and hasOne Post, we can set INNER JOIN relation by default on model class or in controller using bindModel. In this example the hasOne relation set on model class
<?php
class Author extends AppModel {
var $name = ‘Author’;
var $hasOne = array(’Post’=>array(’type’=>’INNER’));
}
?>
and running on controller
$this->Author->find(‘all’);
cakephp will generate query for author table.
SELECT `Author`.`id`, `Author`.`name`, `Post`.`id`, `Post`.`post_id`, `Post`.`title` FROM `authors` AS `Author` INNER JOIN `posts` AS `Post` ON (`Post`.`post_id` = `Author`.`id`) WHERE 1 = 1
I want to show you a situation that probably happen to you when build a mysql query. The situation can be vary but basically same condition. For example we have places table to store all beautiful places around the world. And a categories table to classify our places data. The rule is a place can be put on one or some categories. To achieve this function we just add a field into places table that save category id list.
So places table will have id, name, categories fields. and categories table have id, name. We want to save category list into categories field on places table. The data will look like this ’2,3,4,5,6,7,8′. A question will come to your mind about how to grab or find a matching id inside this categories, for example we want to show all place with category id 5.
We cannot use LIKE in this example because it cannot match perfectly on many digit number. So i found a solution that easy to do. The query to find all data inside mysql Array field is
SELECT * FROM places WHERE FIND_IN_SET(5, categories)
Another solution is by adding new table to save category list for place. But my solution look nice, right?