Tag Archives: Symfony

How TO Install GoDaddy Wild Card SSL Certificate

This article presumes that you have already downloaded the wild card certificate after generating the private key and csr request. GoDaddy gives you two files: gd_bundle.crt yourdomain.com.crt Step 1. change httpd.conf file to create a virtualhost for https at port … Continue reading

Posted in Linux, developer | Tagged , , , , , | View Comments

Doctrine: Magic Finders

Doctrine offers some magic finders for your Doctrine models that allow you to find a record by any column that is present in the model. This is helpful for simply finding a user by their username, or finding a group by the name of it. Normally this would require writing a Doctrine_Query instance and storing this somewhere so it can be reused. That is no longer needed for simple situations like that.

The basic pattern for the finder methods are as follows: findBy%s($value) or findOneBy%s($value). The %s can be a column name or a relation alias. If you give a column name you must give the value you are looking for. If you specify a relationship alias, you can either pass an instance of the relation class to find, or give the actual primary key value.

First lets retrieve the UserTable instance to work with:

// test.php
// …
$userTable = Doctrine_Core::getTable(‘User’);
Now we can easily find a User record by its primary key by using the find() method:

// test.php

// …
$user = $userTable->find(1);
Now if you want to find a single user by their username you can use the following magic finder:

// test.php

// …
$user = $userTable->findOneByUsername(‘jonwage’);
You can also easily find records by using the relationships between records. Because User has many Phonenumbers we can find those Phonenumbers by passing the findBy**() method a User instance: Continue reading

Posted in Doctrine, ORM, Symfony, Technology | Tagged , , | View Comments

Symfony Exceptions

In case of error or unauthorized access to, for example, it makes sense to throw an exception. In order to react to the error conditions better, should throw it back on the correct type of the exception and not always … Continue reading

Posted in Symfony | Tagged , | View Comments

Symfony2 WebProfiler

Symfony2 just introduced its WebProfiler, the utility will soon be the favorite of all programmers. Most veterans will recall that five years ago was the first Symfony framework to include a web debug toolbar. This bar displays useful information for debugging applications and provides access to all logs with a single click:

Symfony2 presents its WebProfiler as the great evolution of the web debug toolbar. Whenever you view a page, Symfony2 generates a unique token debug (pictured above, the token is 4c7e59811509c). By clicking on that token, it shows the WebProfiler with all the debug information:

In the menu on the left side you can see the five main sections to the Profiler which now has:

Request: displays information about the request and response (parameters, cookies, headers).
Exception: if the request has caused an exception, it shows the type of exception, the message from the server and the whole execution trace.
Events: Shows the events raised during execution of the application (along with their event listeners) and the events that have been identified but have not come to run.
Logs: Displays the same information to log the original debug toolbar.
Doctrine / Propel: shows the queries to the database and the time taken for each.
Another great features of the Profiler is that it keeps all your information in a database called SQLiteprofiler.db and stored in the directory cache/ in your application. With this database, you’ll be able to consult the entire execution history of your application, which will facilitate the clearance of projects:

If you want to try the Profiler, you need version of the sandbox Symfony2 PR3, which has not yet been published as a downloadable file. Therefore, when you can only download via git:

mkdir sandbox
git clone http://github.com/symfony/symfony-sandbox.git sandbox
cd sandbox /
git checkout PR3
Now you can try accessing http://localhost/sandbox/index_dev.php Continue reading

Posted in Symfony, Technology | Tagged , , , | View Comments

Multiple domains on symfony project

This post is about developing a symfony project that provides multiple applications that are reachable on multiple top level domains. In our case, the symfony applications “UMTS Netzabdeckung” and “Flatrates Vergleich” share the same backend (models, data, etc.) but each site is a standalone application and has a dedicated top level domain.

Create two applications

The first step is to create two applications. Run

symfony generate:app site1
symfony generate:app site2

in your console and change the default actions, routing, etc. Same thing as usual.

Edit .htaccess

The next step is quite simple: Open your .htaccess file in /sf_project_dir/web and insert your second application to the .htaccess file (BEFORE the default RewriteRule)

RewriteCond %{HTTP_HOST} secondhost.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ site2.php [QSA,L]

That’s all!

That’s all to get it working. Each request to your second domain will be redirected to the according application. A drawback is, that your “frontend” application is still reachable on your second domain if you type in the script name directly. Since I’m not an htaccess expert, I just added some php code to my index.php:

if($_SERVER['SERVER_NAME'] != ‘www.flatratesvergleich.de’) {
header(‘Location: http://www.flatratesvergleich.de’ . $_SERVER['REQUEST_URI']);
die;
} Continue reading

Posted in General, Symfony | Tagged , , , , | View Comments

Snippet: Symfony & Propel–Queries with SQL functions

This is a super short snippet that I ALWAYS forget how to do! Often I want to build more complex where clauses with Propel Criteria which use SQL functions such as UCASE, LCASE, LEN and the date functions DAY, MONTH and YEAR. This is possible using Propel & Criteria, but how to do it is not immediately obvious.

The snippet below shows how to select objects from the database which were created in a specific month and year. Using Criteria::CUSTOM, it’s possible to specify a column and a comparison to do with that column. This is quite useful for doing things like building archive lists.

$c->add(MyObjPeer::CREATED_AT, ‘MONTH(‘.MyObjPeer::CREATED_AT.’)=’. $month, Criteria::CUSTOM);
$c->addAnd(MyObjPeer::CREATED_AT, ‘YEAR(‘.MyObjPeer::CREATED_AT.’)=’. $year, Criteria::CUSTOM);
A Note on Snippets: When using frameworks such as Symfony it is often the simplest pieces of code which are the hardest to either find or remember. These snippets are placed here for my own reference and will hopefully be useful to others. If you find them useful or have any suggestions, please let me know. Continue reading

Posted in Symfony | Tagged , , , | View Comments

Symfony Coding Standards

Following coding standards is one of the easiest way for everybody to understand everybody’s code.Here’s the golden rule: Imitate the existing symfony code. Never use tabulations in the code. Indentation is done by steps of 2 spaces: <?php class sfFoo … Continue reading

Posted in Symfony, Technology | Tagged , , , | View Comments

Symfony:Forward vs Redirect

Forward vs Redirect? Symfony Redirect: This simply does a HTTP header(”location: “) Symfony Forward: This is complete custom code that comes with the Symfony framework that forwards you within the Symfony application. Use Symfony Forward if: If the action needs … Continue reading

Posted in General, Symfony | Tagged , | View Comments

Type confusions in Symfony

I post this merely to point out how easy it is to forget to check EVERYTHING when you appear to have a bug. I’ve been trying unsuccessfully for the last hour to save a string to my database, like $myString … Continue reading

Posted in Symfony | Tagged , , , | View Comments

Before And After Action in Symfony

Ever wanted to run some functions or create some variables before every action in a module? Or ever wanted to do something crafty after every action and before the template gets displayed? Even if your answer is no, its good … Continue reading

Posted in Symfony | Tagged , , | View Comments