Using Doctrine with Zend framework

Copy Doctrine.php and Doctrine folder to library folder.

Now add following function to your Bootstrap.php file.

public function _initDoctrine(){
require_once 'Doctrine.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->pushAutoloader(array('Doctrine','autoload'));
$manager = Doctrine_Manager::getInstance();
$options = $this->getOptions(); // zf application config array
$dsn = $options['dsn'];
$conn = Doctrine_Manager::connection($dsn, 'defaultConn');
Doctrine_Core::generateModelsFromDb(APPLICATION_PATH.'/models',
array('defaultConn'),
array(
'generateBaseClasses' => true,
'generateTableClasses'=> true,
'classPrefix'=>'Model_',
'classPrefixFiles'=> false,
'baseClassesDirectory'=> '.'
)
);
}

The above code creates your Doctrine connection, so else where in your code you can use $conn = Doctrine_Manager::connection($dsn, ‘defaultConn’); to get a connections to database.

Also the above code gets DSN setting for you database which looks like dsn = “mysql://username:password@host/database_name” in your application.ini file.

The function Doctrine_Core::generateModelsFromDb with above settings will generate table and record classes for all the tables in your database and would save them to your application/models folder.

As all the classes generated by Doctrine would be prefixed with “Model_” you can would be able to use them anywhere in your Zend framework application because of ZF autoloader. As the above generated class would follow ZF convention for models.

How to fix Warning: is_readable(): open_basedir restriction in effect in Zend Framework => 1.10

If you are getting load of this error after upgrading to Zend framework 1.10 and greater, you can fix it by changing Zend/Loader.php file

Change method isReadable() to following
Also make sure anything in your include path should be in your basedir option too.
Waring!!! use the code at your own risk as I have not done extensive tests.


    /**
     * Returns TRUE if the $filename is readable, or FALSE otherwise.
     * This function uses the PHP include_path, where PHP's is_readable()
     * does not.
     *
     * Note from ZF-2900:
     * If you use custom error handler, please check whether return value
     *  from error_reporting() is zero or not.
     * At mark of fopen() can not suppress warning if the handler is used.
     *
     * @param string   $filename
     * @return boolean
     */
    public static function isReadable($filename)
    {
        if (is_readable($filename)) {
            // Return early if the filename is readable without needing the 
            // include_path
            return true;
        }

        if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN'
            && preg_match('/^[a-z]:/i', $filename)
        ) {
            // If on windows, and path provided is clearly an absolute path, 
            // return false immediately
            return false;
        }

        if(strpos($filename, '/')==0) return false; //if absolute path skip

        foreach (self::explodeIncludePath() as $path) {
            if ($path == '.') {
                if (is_readable($filename)) {
                    return true;
                }
                continue;
            }
            $file = $path . '/' . $filename;
            if (is_readable(realpath($file))) {
                return true;
            }
        }
        return false;
    }

How to make firefox search google.co.uk rather google.com by default

1. First install google toolbar from http://www.google.com/toolbar/ff/index.html

2. Restart firefox

3. right click on Google toolbar, select “Toolbar options”.

4. Click on Search from the top icons, now on this page change “Use Google site” option to google.co.uk

5. Click on Layout from the top options, now select option “Replace firefox search box and hide Google toolbar”.

6. Click save, done.

How to Find & replace recursively in Linux from command line script

Following is the script to find any phrase and replace it with another.

for i in `grep –include=”*\.include” -rl ‘phrase to search’ /var/www/`; do

sed –in-place=bak -e ‘s/phrase to search/phrase to replace with/g’ “$i”

done

The above script searches folder /var/www and all of its sub folders with any file with extension .include for phrase “phrase to search” and will replace all the found phrases with phrase “phrase to replace with

How to auto start Red 5 on Ubuntu or any linux distro

Following script monitors RMTP port, which indicates if Red5 is running or not

if (netstat -na | grep -q :1935) ; then
echo “Red5 running.”
else
bash /usr/local/red5/red5.sh
fi
If RMTP is open for connection nothing is done, else the script tries to start Red5 server by executing the red5.sh startup script.
so save the above script in red5 folder say /usr/local/red5/cron.sh
now we need to add a cron job to run the cron.sh at specified intervals, e.g. we will run it every 2 mins.
edit cron jobs with following command
crontab -e
now add following line to the cron jobs
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,59 * * * * /usr/local/red5/cron.sh & >/dev/null 2>&1
The above setup will monitor red5 every two minute , if server is not running it will start the server.

The script whose uid is 33 is not allowed to access /tmp owned by uid 0

If SAFE MODE is on in PHP, the owner ID of the PHP file needs to be same to any folder or file it tries to access. So if you try to use sessions and if your PHP is configured to use /tmp folder for saving session data it can cause error shown in title of this blog post.

To fix it check following directive is set as follows

session.save_path = /tmp

If yes create folder

/var/lib/php5/session

change owner of the above folder to same as that of the web server, so for e.g. you would say

chown www-data:www-data /var/lib/php5/session

now change /etc/php.ini  or /etc/php5/apache2/php.ini

to use following setting

session.save_path = /var/lib/php5/session

now reload web server e.g.

service apache2 reload

or

/etc/init.d/apache reload

This should fix the problem

Problems faced configuring LAMP

1. All the virtual host need to use the private IP address in the virtual host directive rather the elastic IP or it will not work.

i.e.

Good

<VirtualHost 10.202.150.134:80>

Not

<VirtualHost *:80>

<VirtualHost 0.0.0.0:80>

<VirtualHost 184.72.230.132:80>

2. Access denied for user ‘www-data’@’localhost’ shows up if the first attempt to connect to database has failed and application has used mysql_query after that, so php won’t find any active connections so it would try default settings to connect to database, quite hard to fix.

Mysql Replication Settings

http://www.softwareprojects.com/resources/programming/t-how-to-move-copy-a-live-mysql-database-and-what-1257.html

On Mysql Server

1. Turn binary logging on and add server id

CAUTION ! DO NOT USE “log-bin=/var/log/mysql/mysql-bin.log’

[mysqld]
log-bin=mysql-bin
server-id=1

2. Create replication account on the server

GRANT REPLICATION SLAVE ON *.* TO ‘repl’@’slave-ip-address’ IDENTIFIED BY ‘password-here’;

3. Keep the above mysql console open, start new terminal and connect to server via new terminal

cd /var/lib/mysql

now use dir, you should see all the database folders

4. Time to lock all the databases on server.

mysql> SET GLOBAL WAIT_TIMEOUT=600000; SET WAIT_TIMEOUT = 600000; FLUSH TABLES WITH READ LOCK;

now on the new console enter

root@server:/var/lib/mysql#tar -cvf /tmp/mysql-snapshot.tar ./ –exclude mysql &

enter following in mysql console, this will show the position of server in log file, keep this window open and note file and position column

SHOW MASTER STATUS;

now enter following in mysql console

UNLOCK TABLES;

5. Copy the tar file from master to slave server

scp -i permission-file.pem /tmp/mysql-snapshot.tar root@slave-server-ip:/root/

6. Extract the tar file on slave server

cd /var/lib/mysql

mv /root/mysql-snapshot.tar .

tar –extract –file=mysql-snapshot.tar

7. Change server id on slave mysql server, add following to config file /etc/my.cnf or /etc/mysql/my.cnf

[mysqld]

server-id=2

8. Start or restart Slave mysql server with

service mysql restart

9. Login to mysql console and enter

mysql>stop slave;

mysql> CHANGE MASTER TO
MASTER_HOST=’ip-of-master-server’,
MASTER_USER=’repl’,
MASTER_PASSWORD=’replication_password-from-step-2′,
MASTER_LOG_FILE=’recorded_log_file_name-from-step-4′,
MASTER_LOG_POS=recorded_log_position-from-step-4;

mysql>start slave;

If every thing is done correctly the slave should start replicating.

Once master has been set on and slave started on Slave, after restart the slave will auto start.

If something is not right on slave, login to slave mysql console

use following

stop slave;

reset slave;

now from the new bash console, reextract the tar file replacing existing file with following command

root@slave-server:/var/lib/mysql# tar –overwrite –extract –file=mysql-snapshot.tar

now repeat step 9.




Quick Tips

Question: SSH idle connection time out issue?

Answer: On client machine add following

ServerAliveInterval 5

to /etc/ssh/ssh_config

Taken from http://embraceubuntu.com/2006/02/03/keeping-ssh-sessions-alive/

Error in Ubuntu:

perl: warning: Setting locale failed.

perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LANG = “en_GB.UTF-8”
are supported and installed on your system.

perl: warning: Setting locale failed.perl: warning: Please check that your locale settings: LANGUAGE = (unset), LC_ALL = (unset), LANG = “en_GB.UTF-8”    are supported and installed on your system.

Answer:

On bash console enter following

locale-gen en_GB.UTF-8

How to install red5 on Fedora 8

Goto java.sun.com download most recent jdk for linux with jdk-rpm.bin

chmod o+x jdk-rpm.bin

./jdk-rpm.bin

java is now installed

test commands

javac -version

java -version

now follow step 2,3,4 and 5 from http://www.sohailriaz.com/how-to-install-red5-server-on-centos-53/

Following content are taken from http://www.sohailriaz.com/how-to-install-red5-server-on-centos-53/ on 6th august 2010

2) Download and Install Ant (Apache Project)

Ant will need to compile RED5 server code. Ant comes in binary form, so just download and install it in /usr/local directory.

cd /usr/src
wget http://mirrors.kahuki.com/apache/ant/binaries/apache-ant-1.8.0-bin.tar.bz2
tar jxvf apache-ant-1.8.0-bin.tar.bz2
mv apache-ant-1.8.0 /usr/local/ant

3) Export Variables for Ant and Java

export ANT_HOME=/usr/local/ant
export JAVA_HOME=/usr/lib/jvm/java
export PATH=$PATH:/usr/local/ant/bin
export CLASSPATH=.:$JAVA_HOME/lib/classes.zip

Also export these variables in /etc/bashrc to become available for every user login or for any terminal opens.

echo ‘export ANT_HOME=/usr/local/ant’ >> /etc/bashrc
echo ‘export JAVA_HOME=/usr/lib/jvm/java’ >> /etc/bashrc
echo ‘export PATH=$PATH:/usr/local/ant/bin’ >> /etc/bashrc
echo ‘export CLASSPATH=.:$JAVA_HOME/lib/classes.zip’ >> /etc/bashrc

4) Download and Install RED5 Server

Here the latest version available for RED5 is 0.7 on site but download from google code using svn as the tarball of 0.7 on site is missing some of the files.

cd /usr/src
svn checkout http://red5.googlecode.com/svn/java/server/trunk/ red5
mv red5 /usr/local/
cd /usr/local/red5
ant prepare
ant dist

you will see a ton of lines, but you should get at last

BUILD SUCCESSFUL

that’s mean its install and now copy the conf directory from dist/ and test the red5 installation.

cp -r dist/conf .
./red5.sh

If it shows Installer service created in the last then everything is fine here, press ctrl+c and move to next step to create init script.

5) Init Script

Now we will create init script for red5 to start, stop and restart easily.

vi /etc/init.d/red5

The init script code also be viewed below. following script on works with fedora or redhats, for ubuntu edit cron with command ‘crontab -e’ and add  ‘@reboot cd /usr/local/red5/ ; ./red5.sh & >/dev/null 2>&1’ and now reboot

#!/bin/sh
# For RedHat and cousins:
# chkconfig: 2345 85 85
# description: Red5 flash streaming server
# processname: red5

PROG=red5
RED5_HOME=/usr/local/red5
DAEMON=$RED5_HOME/$PROG.sh
PIDFILE=/var/run/$PROG.pid

# Source function library
. /etc/rc.d/init.d/functions

[ -r /etc/sysconfig/red5 ] && . /etc/sysconfig/red5

RETVAL=0

case “$1″ in
start)
echo -n $”Starting $PROG: ”
cd $RED5_HOME
$DAEMON >/dev/null 2>/dev/null &
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo $! > $PIDFILE
touch /var/lock/subsys/$PROG

fi
[ $RETVAL -eq 0 ] && success $”$PROG startup” || failure $”$PROG startup”
echo
;;
stop)
echo -n $”Shutting down $PROG: ”
killproc -p $PIDFILE
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$PROG
;;
restart)
$0 stop
$0 start
;;
status)
status $PROG -p $PIDFILE
RETVAL=$?
;;
*)
echo $”Usage: $0 {start|stop|restart|status}”
RETVAL=1
esac

exit $RETVAL

chmod a+x /etc/init.d/red5

Now start the service

/etc/init.d/red5 start

check status

/etc/init.d/red5 status
red5 (pid  XXXXX) is running…

again you can do stop, restart.