Showing posts with label developer cakephp. Show all posts
Showing posts with label developer cakephp. Show all posts

Tuesday, September 6, 2011

cakephp check database configuration exist and connection alive valid

Keyword
Check cakephp database configuration exist
Check cakephp database can be connect

In cakephp you need to configure the connection
The configuration file was in app/config/database.php

Inside the file, you can see 2 variable which is 'default' and 'test'

By default is 'default' but if you want to use 'test' in the controller for example, you need to call it like this
$this->Mmodel->useDbConfig = 'test';
 

Problem arise is, can we connect to 'test' without problem?

So, here is the code I made. Been googling it and quite hard to found the solution, thus I'm putting this in my blog. lol...

This code will check
- Does the configuration 'test' exist
- If the configuration exist, does the database connection valid

If these 2 condition is pass, you can only have SQL Syntax Error like selecting table that does not exist

So here is the code (its free)
//Checking is done by connecting with the  database
if (class_exists('DATABASE_CONFIG')){
    $dbConfigName1 = 'test'; //<-- PUT YOUR DATABASE CONFIG VARIABLE NAME HERE

    $dbConfig1 = new DATABASE_CONFIG();
    //pr( $dbConfig1);                    

    if( isset( $dbConfig1->$dbConfigName1)){                       

        //Now check wether the database configuration, its alive!
        //pr( $dbConfig1->$dbConfigName1);
        $dbTemp1 =& ConnectionManager::getDataSource($dbConfigName1);

        if( $dbTemp1->isConnected() == true){

            //Means database exist and you can use it
            echo "DATABASE CONFIG '$dbConfigName1' EXIST AND MANAGE TO CONNECT";
        }
        else{

            //Database configuration exist, but cannot connect to the database
            echo "DATABASE CONFIG '$dbConfigName1' EXIST BUT FAILED TO CONNECT";
        }
    }
    else{

        //The database configuration does not exist
        echo "HAS NO DATABASE CONFIGURATION WITH NAME '$dbConfigName1'";
    }
}
else{

    //The database.php file not even exist. How can this be? IMPOSIBIEBERBEL!
    echo "CANNOT FIND THE 'database.php' CONFIGURATION FILE";
}

Reference
http://bakery.cakephp.org/articles/T0aD/2009/07/08/handle-database-connection-errors
http://debuggable.com/posts/handling-database-connection-errors-in-cakephp:480f4dd5-9570-421a-a04d-43cdcbdd56cb

Sunday, January 30, 2011

Prevent mail from spam or junk by encryption from crawler

If you write a blog and put your email on it. There is a chance for you to get Spam/Junk mail
This can be cause of many reason but I'm focus on crawler method

How this spam works?
Basically this is the procedure
1. They find your email
2. They write email to you

Spam usually include 1000 email or more and of course it is done by using a bot (a system) and not just people manually type in

To find your email
They using web-crawler, spam-crawler or anything-crawler. These crawler will browse slowly through the internet (from blog to blog, website to website until forever). If your blog put an hyperlink of your friend blog (blogroll they called) these crawler will go into it

Thus in, programming these crawler detect 2 things

1st detection
Detect "http://" text. If they found and word, URL or string match. They will retrieve the link and put in on the "Next going to be crawl list"

2nd detection
Since they want to spam to your mail, they simply find any string or word with "@" symbol. Thus your email will be captured in these process

Write email to you (spam)
These is a process of producing mass email and of course they use the list they got from the first step and use a bot (system) to mass mail it


Prevention

There is 2 type of prevention
1. Email filter (thats why you have spam/junk mail categories)
2. Prevent your email from being on the spammer list

1st prevention - Filter your mailbox
This is already common and build in feature for almost all email system like gmail and others. So no need discussing much

2nd prevention - Prevent from listed
This is what I want to discussed about
Usually people will make their email not in easy format or encryption.

Using different email format like (let say your email is ajskdhqjeakjbdia@gmail.com)

Email: ajskdhqjeakjbdia (@gmail)

Email: ajskdhqjeakjbdia at gmail dot com

Or they use encryption like these

ajskdhqjeakjbdia@gmail.com
(It looks normal but it is actually encrpted)

So these encryption can be easily google. But I'm suggesting you the simplest one which taken from these website http://www.web-designz.com/tools/email_encoder.shtml
Note: The simplest method might also means it is easily detectable by the crawler

If you are a programmer and wandering how did the website do the answer is they are using convertToUnicode(...) function

If you view the page source. It wont show and "@" or your email at all because it is encrypted and of course the crawler is a system, they read the page source and not reading like you did (using web browser)

-End-

Tuesday, January 25, 2011

HTML UTF encoding entities


I was doing some project require math symbol. The key was not exist in keyboard
The website that I use as reference is
http://www.fileformat.info/info/unicode/char/b1/index.htm
The link is a plus minus symbol (±). You can search fur further symbol at the top left

Or also here
http://www.danshort.com/HTMLentities/index.php?w=hertz

Sunday, January 23, 2011

CakePHP jquery autocomplete

Im using cakephp 1.2 and want to make autocomplete on them
Have googling for several hours (including tying it) and finally succeed

p/s: Im using try an error method. Im afraid that this 3rd try was success because of additional code I have put during try #1 and #2

1st I try to use this and succeess (except when want to autocomplete from database source)
Using original jquery http://jqueryui.com/demos/autocomplete/ which is version jquery-ui-1.8.8.custom.min.js and jquery-ui-1.8.8.custom.css

2nd was from this blog (this fail)
http://nuts-and-bolts-of-cakephp.com/2008/05/07/jquery-autocomplete-in-cakephp/
http://www.pengoworks.com/workshop/jquery/autocomplete.htm

3rd was from this website
http://www.cakephp.bee.pl/ajax/autoComplete

Thanks for all those programmer who make my life easier

After several investigation (I go crazy with these 2nd try for almost 2 days and turned out it cause of URL?)
I manage to enable the 2nd try. The reason of error is because I didnt put my system name
the website tutorial said "/products/autoComplete" but if your CakePHP 1.2 system name is 'accounting' means its going to become '/accounting/products/autoComplete'

What next? I want to make the view to be able to save more Account name. Thus is use a loop like this
input("Account.$aa.name"); ?>
input("Account.$aa.id"); ?>

Remember, the account name is just a name. At the controller what matters is the Account.$aa.id
Thus, after the autocomplete on the Account Name, the Account Id will be put the Id of the selective Account

To do this you require to modify function findValue(...) because this is where the event for 'Choosing which Account Name from the list of autocomplete'. And also a dummy input and one new function

New dummy input (on the view)
<input id="tempCount" value="" />

New dummy function (on the view at the javascript)
function tempCountert( theTempCountValue)
{
document.getElementById('tempCount').value = theTempCountValue;
}
Then you need to change you loop for the account name

From
input("Account.$aa.name"); ?>

To
input("Account.$aa.name", array( 'onchange'=>'tempCountert("Account'.$aa.'Id")')); ?>

Finally will be on the function findValue(li). Make sure to add this code
var updatedColumner = document.getElementById('tempCount').value;
document.getElementById(updatedColumner).value = sValue;



How my code works?
  1. You type in some value in the 'Account name' and the autocomplete list out the match
  2. When you click one of the match result. The dummy input (tempCount) will change the value to Account Name index. let say Account.0.name will print out 'Account0Id' in dummy input
  3. Function findValue(...) will be executed. And get the value in dummy input. Thus change the value

Thursday, January 20, 2011

Netbeans detect cakephp .ctp file

It is happen I use Netbeans as IDE and CakePHP as web framework
The problem is Netbeans only detect .php, .html and others but not .ctp file

.ctp file is a cakephp file which is similar to .php (dont know why these cakephp guys want to use different file name)


Fow Windows users

And to make the Netbeans to be able to detect it was originally from this blog http://www.davidtan.org/netbeans-enable-syntax-hightlighting-for-cakephp-ctp-view-files/comment-page-1/#comment-18469

I rewrite down the same thing
  1. From the Menu, go to Tools > Option (Then there will be a pop-up window named "Options")
  2. From the Options windows there is several Tabs, click on the "Files" tab
  3. Under the Files tab, there is a "File Extension:". Try to create a new file extension and name it "ctp"
  4. Then "Associated File Type (MIME)" for the ctp file extension, set it to "text/x-php5"
  5. Finally press "OK" button at the bottom


For Ubuntu users

I found the solution from here, pretty much the same but slightly in different place
http://forums.netbeans.org/post-60683.html&highlight=

Write back same things
tools > option > miscellaneous > Files tab

Wednesday, January 5, 2011

CakePHP connect 2, many, multiple database

Tag
CakePHP connect with more than 1 database
CakePHP connect multiple database
CakePHP connect more database
CakePHP connect 2 database
CakePHP connect with 2 database. 1 is CakePHP structured and another 1 is not

The main source is from here
http://blog.4webby.com/posts/view/6/cakephp_models_using_multiple_db_connections

Repeat the same thing
class DATABASE_CONFIG {

var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'your_host',
'login' => 'your_login_1',
'password' => 'your_password_1',
'database' => 'DB_1',
'prefix' => ''
);

var $general_syst = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'your_host',
'login' => 'your_login_2',
'password' => 'your_password_2',
'database' => 'DB_2',
'prefix' => ''
);
}
?>

class User extends AppModel {

var $name = 'User';
var $useDbConfig = 'general_syst';

//your code here
//....
}

class Post extends AppModel {

var $name = 'Post';
var $useDbConfig = 'default';

//your code here
//....
}

$this->Post->bla_bla_bla : data will be retrieved/inserted/updated from DB_1
$this->User->bla_bla_bla : data will be retrieved/inserted/updated from DB_2


Here is some additional
1. I can connect even without make it persistance.

pr($this->Student->useDbConfig);
$this->Student->useDbConfig = 'general_syst';
$ret = $this->Student->query("SELECT * FROM tbl_lecturer");


Note that tbl_lecturer is another non cakePHP database. It used MySQL database and query does take the data nicely

If you want to check the database connection. Refer here http://asipi.blogspot.com/2011/09/cakephp-check-database-configuration.html

Thursday, September 30, 2010

PHP ' become &#039;

in PHP What happen when I output the string into text file is
The character ' become &#039;

Copy this &amp;#039; to create this &#039;

&#039; is a html character special code that represent ' symbol
Means it will display correctly as ' if you see it in web browser

Simply solving the problem by html_entity_decode( "&#039;", ENT_QUOTES)

Note that html_entity_decode( "&#039;") will not work
I found this solution in some page (didnt remember the site URL, sorry >.<)

So what is this html_entity_decode(...) function?
It is use to convert special html character into the normal character
Detail was in http://php.net/manual/en/function.html-entity-decode.php

The inverse of it was htmlentities(...)
Detail was in http://www.php.net/manual/en/function.htmlentities.php

Tuesday, September 28, 2010

Software Architecting Success Factors and Pitfalls

My boss give to my email so sharing it with myself

Software Architecting Success Factors and Pitfalls

The top critical success factors for the architecting effort that we have identified are:

The architecting effort must:

* address a strategic business objective of your key sponsor
* have a good lead architect with well-defined role and style
* have a lead architect and architecture team who are able to "sell" (lead); conversely, the organization must be willing to "buy into" (follow)
* contribute immediate value to developers (utilizers of the architecture)

The architecture is more likely to be successful if:

* there are architecture advocates at all levels of the organization
* architecture is woven into the culture
* there is customer involvement/pressure/demand

Critical Success Factors

* Interpersonal and team communication and ownership
* Leadership
* Vision
* Teamwork
* Availability of talent/resources
* Must have strong management sponsorship
* Market/business understanding
* Good match between technology and business strategy
* Customer focus
* Clear specifications including dependencies
* Simple architecture
* Deployed in phases/incrementally
* Architecture is understandable by all
* Solve at least the current problem
* Validation of requirements during each step of the process
* Project management

The architect must have the following skills:

* good domain knowledge
* good communicator/listener
* good persuader
* good project management skills

The architect must

* have a clear and compelling vision
* champion the cause
* provide constructive feedback

Pitfalls

* Poor leadership
* Thinking at too low a level
* Poor communication inside/outside the architecture team
* Not enough "selling"
* Lack of resources/talent
* Poorly designed roles and responsibilities
* Bad design/idea
* Lack of extensibility
* Doesn't solve the project team's problems
* Lack of control/authority
* Requirements unclear, not well-defined, not signed off, changing
* Architecture team loses touch with the product team's problems
* Product team believes "we can solve it better ourselves"
* Development management not penalized for "stalling"
* Politics

Tuesday, August 10, 2010

Web Graph

I surfing the net and come across with this http://www.rgraph.net
It provide you with a graph creation for website development. Pretty nice compared with JPGraph
Its a free for non business (only 1 time payment for business)

Tuesday, May 18, 2010

CakePHP iframe

I very new with iframe so i just job it down to myself

lets says I have a controller name customers, inside it has 3 function which is index, add and edit.

Inside the index.ctp, i put an iframe into it

like this
<a href="customers/edit/1" target="test">HREF</a>

<iframe name="test" width="500" height="200" frameborder="1" src="customers/add"></iframe>


Tho cool part is, it will load the customer/add into the iframe and when I click the hyper link, the customers/edit/1 will load

Thursday, May 13, 2010

CakePHP utf8 special character chinese character

i involve in some database with a chinese character.
It turns out my find('all') give me "????" for the chinese character

After some google, this is because database encoding. Its need to be utf-8
So this is the link thats help me http://nik.chankov.net/2007/10/01/cakephp-and-character-set-in-the-database/

Conclusion is, inside the database.php put the 'encoding'
class DATABASE_CONFIG {

var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'project_name',
'prefix' => '',
'encoding' => 'utf8'
);
}


What will happen actually is before any SQL query is executed.
It will execute this code first, I think.
"SET NAMES 'utf8'"


Just to remind something. If there is a case you table Collation/charset is not utf-8 means you will not be able to use Chinese character. So here is the tweak
ALTER TABLE 'tblcustomers' COLLATE utf8_general_ci;


And still, my website did not display the chinese character.
So I end it with
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf8"$gt;

and the problem is solve

Yeah!