First problem encountered with Symfony 2

Just solved my first problem with symfony 2. It was complaining about sqlite3 module or something…
http://symfony-hribo.blogspot.com/2011/01/you-need-to-enable-either-sqlite-or.html
will now restart apache to see how is it going?…

Right from the get go I also had to edit the php.ini @ /etc/php5/cgi
and also just to remember my short_open_tag by default were set to On on that same php.ini file
Symfony2 told me to change that to Off

Later on I also went ahead and installed the APC PHP caching system, easily searching on the ubuntu installer software
and I went ahead also for the intl module for php5 that was requiring…

so far all good until I click and get this:

Fatal error: Class ‘Symfony\Bundle\FrameworkBundle\FrameworkBundle’ not found in /home/cordoval/sites/Symfony/app/AppKernel.php on line 11

All of a sudden I noticed after cat README.txt that I have missed running the vendors.sh scripts and then noticed that I had not installed git! nothing that a sudo apt-get install git could not solve…

then again I ran into a weird thing, I had to run /bin/vendors.sh outside of the folder bin then did that but then got a problem with

sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle’ not found in /home/cordoval/sites/Symfony/app/AppKernel.php on line 18

So now it is a problem with the FrameworkExtraBundle

Scratched everything and did:

git clone git://github.com/symfony/symfony-standard.git .

now running vendors.sh again…

there are more scripts to be run on the README.md and now I found that symlinks is an option…

got it, it was just a singular, meaning no s at the end of symlinks

Ended the day by finishing the quick tutorial.

establishing an sqlite3 db connection via ActiveRecord

ActiveRecord can be used as a stand-alone gem without going through a Rails app. When establishing a connection we must provide a name for the database. Here is an example where I use sqlite3 as a db engine. I want to provide a full path to the database also.

[code]
my_db_dir = ‘~/projects/myapp/db’
filename = File.join(my_db_dir,’development.sqlite3′)
ActiveRecord::Base.establish_connection(:adapter => ‘sqlite3’, :database => filename)
[/code]

On Ubuntu systems it is very likely that we will get an error


No such file or directory

I know the file is there. I know the path is correct. So what is wrong? The problem is the “~” character. ActiveRecord uses Ruby’s File.binread() method to read the file. The binread() method is a wrapper for a function in the c code (io.c) of the Ruby interpreter. When you dig deep into the source code of the Ruby project you will find the file.c file which contains a function for checking file paths for security related issues. This function is called rb_get_path_check().

Basically we should not pass a tilde character to File.binread(). It is something that will confuse you for many hours if you it!!

Here is a demonstration of the problem using interactive Ruby (irb).

[code]
$ irb
ruby-1.9.2-rc2 > File.binread("~/myfile.txt")
errno::ENOENT: No such file or directory – ~/myfile.txt
from (irb):1:in `binread’
from (irb):1
from /home/philip/.rvm/rubies/ruby-1.9.2-rc2/bin/irb:17:in `<main>’
ruby-1.9.2-rc2 > File.binread("/home/myuser/myfile.txt")
=> "This is the content of myfile… "
[/code]

You can see that the “~” character did not work. 🙁

fun with sqlite3

We already have sqlite3 installed. So creating a new database is easy:

[code]
..mypath/db$ sqlite3 test.sqlite3
SQLite version 3.7.2
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>
[/code]

We add some tables:
[code]
sqlite> create table categories (name varchar(10), id smallint);
sqlite> create table products(name varchar(20), id smallint, category_id smallint);
[/code]

List our new tables:
[code]
sqlite> .schema
CREATE TABLE categories (name varchar(10), id smallint);
CREATE TABLE products(name varchar(20), id smallint, category_id smallint);
sqlite>
[/code]

Now insert some data:
[code]
sqlite> insert into categories values (‘catholicism’, 1);
sqlite> insert into categories values (‘baptist’, 2);
sqlite> insert into categories values (‘chino orthodox’, 3);
sqlite> insert into categories values (‘redneck’, 4);
sqlite> insert into products values (‘bible’, 1, 2);
sqlite> insert into products values (‘candle’, 2, 1);
sqlite> insert into products values (‘gun’, 3, 4);
sqlite> insert into products values (‘chopsticks’, 4, 3);
[/code]

List content of tables:
[code]
sqlite> select * from categories;
catholicism|1
baptist|2
chino orthodox|3
redneck|4
sqlite>
[/code]
[code]
sqlite> select * from products;
bible|1|2
candle|2|1
gun|3|4
chopsticks|4|3
sqlite>
[/code]

Okay. In this simple design there is a one-to-many relationship between tables. A category has many products. A category is just a category. It has no foreign keys. The products tables has a foreign key called “category_id.”

Now we can join the data:

[code]
sqlite> select p.name, c.name from products p inner join categories c on p.category_id = c.id;
bible|baptist
candle|catholicism
gun|redneck
chopsticks|chino orthodox
sqlite>
[/code]

Why did I use the “inner join” construct? Honestly, I don’t even think about this. I always think first in terms of inner join. If I need more complicated logic then perhaps I will use an “left join” construct.

[code]
sqlite> select p.name, c.name from products p left join categories c on p.category_id = c.id;
bible|baptist
candle|catholicism
gun|redneck
chopsticks|chino orthodox
[/code]

Hey! The results are the same! Why? Only because of the state of the data.

Let us change the data:
[code]
sqlite> update products set category_id = null where id = 3;
sqlite> select * from products;
bible|1|2
candle|2|1
gun|3|
chopsticks|4|3
sqlite>
[/code]

Now what category does the “gun” item belong to? We don’t know. Maybe it belongs to the Baptists or the Catholics?

Run the query again with both queries:

[code]
sqlite> select p.name, c.name from products p inner join categories c on p.category_id = c.id;
bible|baptist
candle|catholicism
chopsticks|chino orthodox
sqlite>
[/code]
[code]
sqlite> select p.name, c.name from products p left join categories c on p.category_id = c.id;
bible|baptist
candle|catholicism
gun|
chopsticks|chino orthodox
sqlite>
[/code]

Wow. The first query only shows products that have valid categories. The second query allows the invalid categories. So it returns the intersection and layers it on top of the invalid data.

Now we can ask which products need a category:
[code]
sqlite> select p.name, c.name from products p left join categories c on p.category_id = c.id where p.category_id is null;
gun|
sqlite>
[/code]
Ooooh! Someone in the Church Bookstore forgot to update the category of the gun item!