Kohana 3: Getting the insert_id after an INSERT query

Since the database library in Kohana 3 is so much different than in Kohana 2, to get the last insert id of a row, use the following code instead:


$insertTable = DB::query(Database::INSERT,
		"INSERT INTO table (x, y, z) " .
		"VALUES(@x, @y, @z)"
);
$insertTable->param('@x', $x);
$insertTable->param('@y', $y);
$insertTable->param('@z', $z);
$insertTableResult = $insertTable->execute();

$insert_id = $insertTableResult[0];

I’m not too sure what the other ways of doing it is, but this is the only way I found after looking at the code.

No thanks to kohana 3’s non-existent documentation.


Addendum: As Sicabol suggested, the code above can be simplified by using list() instead of manually creating and using a separate array. That is:


$insertTable = DB::query(Database::INSERT,
		"INSERT INTO table (x, y, z) " .
		"VALUES(@x, @y, @z)"
);
$insertTable->param('@x', $x);
$insertTable->param('@y', $y);
$insertTable->param('@z', $z);
list($insert_id, $total_rows_affected) = $insertTable->execute();

Kohana 3: Unable to find a route to match the URI

If you get Unable to find a route to match the URI: /some/uri/path/args on Kohana 3 when attempting to request a action with more than one paramter, you will need to update the code in /application/bootstrap.php:


Route::set('default', '(<controller>(/<action>(/<id>(/<overflow>))))', array('overflow' => '.*?'))
	->defaults(array(
		'controller' => 'home',
		'action'     => 'index',
	));

I don’t get why this wasn’t added by default, along with all the controller, helper, model, views directories in /application similar to Kohana 2. Compounded with the poor documentation, Kohana 3 really is starting to tick me off. The time saved by Kohana is wasted on these things.

Transparent Outter Border Effect

I attempted to make a transparent outter border effect for a design as displayed in this post.

To make this effect in HTML/CSS, I tried to make a outter wrapper that wrapped all the contents of the website with a thick border and opacity set to 8%. Unfortunately, opacities set in parent objects will pass to its children as well; meaning all contents under this wrapper also will have an opacity of 8%. There is no way of getting around this.

The solution to this problem is to make the outter wrapper have an image background consisting of the 8% transparency as needed. Thus, there is no need to adjust the opacity in CSS directly.

Java exec() results in no output in linux

For some reason, executing something like ping in java generates no output, but something like uptime does.

I’m guessing the reason for this is because ping has the sticky bit set. It seems like any programs that has the sticky bit will fail to run and generate any output in java.
Edit: Actually, this doesn’t seem to be true. mount has the sticky bit set but it runs fine.
Edit: Running the java program from a shell script causes java to generate output as it normally should….
Edit: The problem solved itself… Try to remove all environment variables by running unset `env | awk -F= '/^\w/ {print $1}' | xargs` and see what happens by running

Executing Programs in Java

The program arguments will be executed through this ‘shell’. You can make a loop that will make the following snippet a real shell.

import java.io.*;

public class SimpleShell {
	public static void main(String[] argv) {
	String line;
	StringBuilder output = new StringBuilder();

	try {
		Process p = Runtime.getRuntime().exec(argv);
		BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
		while ((line = input.readLine()) != null) {
			output.append(line).append("\n");
		}

		System.out.println(output);
	} catch (Exception e) {
		e.printStackTrace();
	}

	}
}

SVN: “Attempted to lock an already locked dir”

If you work with Eclipse and SVN, you’ll probably come across an error similar to: Attempted to lock an already locked dir

You can sometimes resolve this by cleaning up the workplace directly in eclipse by right clicking on your project -> team -> cleanup.

However, if that fails, you will need to delete all the lock files in your workplace.

If you’re under linux, run find ./ -name lock -exec rm {} \; in the workplace directory. If you’re on windows, I’m guessing you can do a search for all lock files and deleting it from the search results. Once that’s done, try to commit again, and hopefully it will work.

If eclipse still refuses to stop spewing error messages after all that, it’s best you start over your workplace. That is, back up your workplace, delete it, re-checkout the code, copy the backed up code into the workplace again, refresh the workplace in eclipse, and attempt to recommit again.

If even that fails, then it’s time to gg.

Installing and Creating a new SVN Repository

Note: This is the second time I’m writing this entry since RZ somehow managed to lose my first post. So these steps are from memory and might not be 100% correct.

If you want to install subversion, you will need to have apache. Easiest way to install this is via yum (or whatever package manager you need to use).
yum install subversion mod_dav_svn httpd

Once complete, you should have a directory in /etc/httpd/conf.d/subversion.conf.

We will create the repos in /home/subversion. I’ll also make a user ‘subversion’ who can manage these files without root. As root, run the following commands:

  1. useradd subversion
  2. cd /home/subversion

To create a new repo:

  1. Create the directory for the new repo. eg: 'repo'
  2. run 'svnadmin create repo'

To create the initial import of the trunk:

  1. mkdir /tmp/trunk
  2. svn import /tmp/trunk file:///home/subversion/repo/trunk -m "Initial Import"

You may want to add users to access this repo. Suppose you want to add user:

  1. cd /home/subversion/repo/conf
  2. htpasswd -cm svn-auth first_user
  3. htpasswd -m svn-auth second_user


Note: do not use the -c argument after adding the first user, otherwise it will overwrite the existing password file.

You will then need to add an entry to /etc/httpd/conf.d/subversion.conf with the following:


        DAV svn
        SVNPath /home/subversion/repo

        AuthType Basic
        AuthName "Subversion Repo for MyRepoName"
        AuthUserFile /home/subversion/repo/conf/svn-auth
        Require valid-user

Ensure the files in /home/subversion/ are owned by apache, but still is group owned by subversion.

You should now be able to access your new repo by going to /svn/repo after using the username/password combination given to htpasswd above.

Generating Self-Signed SSL Certs

If you want to have SSL enabled on your webserver, you may want to generate self signed certificates.

  1. cd ~
  2. mkdir ssl
  3. openssl genrsa -des3 -out server.key 4096
  4. openssl req -new -key server.key -out server.csr
  5. openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
  6. openssl rsa -in server.key -out server.key.insecure
  7. mv server.key server.key.secure
  8. mv server.key.insecure server.key

Step 3: Generating the key, use any pass phrase.
Step 4: Creating the certificate signing request
Step 5: Signing the certificate signing request
Step 6: Making a key that has no pass phrase

Note: Ensure that all keys are not readable to others. chmod 700 the ssl directory and chmod 600 all the keys.


To install the key on a litespeed web server

  1. create a new listener, call it for example, ‘secure’
  2. go to the ssl tab and specify the following values:
    1. Private Key File: $SERVER_ROOT/ssl/server.key
    2. Certificate File: $SERVER_ROOT/ssl/server.crt
    3. Chained Certificate: no
    4. CA Certificate Path: $SERVER_ROOT/ssl
    5. CA Certificate File: Not Set
  3. Copy the entire ssl directory generated above to the litespeed server root
  4. Chmod the entire ssl directory to 700, and chmod the keys to 600
  5. Chown/Chgroup the entire ssl directory to the lsadm user