Author : beriba | 09/02/2012

In big projects, where many developers can put their fingers over the code sometimes unpredictable things can happen. It’s obvious. Especially when the project is written in PHP. Many classes, even more methods – big mess. In such projects it is not easy (if even possible) to know every name of every class. Sometimes PHP interpreter will tell you that:

Fatal error: Cannot redeclare class MyClass in /tmp/MyClass.php on line 4

It can be the result of this code:

class myClass {
}

class myClass {
}

And you can say: „Yeah, right. Who can write such stupid code”. I’ll say – you never know. But this is just a sample when you can expect such error. If you know how to trigger an error on purpose, you’re half way to avoid triggering it unexpectedly. Of course it gets harder when duplicated class names are in two different, not related files. The only solution for that is to rename one of the classes. If you don’t know how to find them, I’ll give you a solution in the end of this post.

Almost the same error could get a little bit tricky to resolve in some conditions. When you are using some kind of autoloader (for example Symfony Framework) and you declare classes of the same name in two different files it is very likely that you won’t get above error. Why? It’s because autoloader will load the first class and then replace it with second. When you’ll try to invoke a method on object (which you think is an instance of the first class) you’ll get an error:

Fatal error: Call to undefined method MyClass::doSomething() in 
/tmp/MyClass.php on line 6

But you have your class just in front of your face, the method exists and you think „what the…”. There could be more than one reason of this error. First is explained above. The second – code you’ve written is just in memory of your editor or IDE and was not saved in file. Yes, this could sometimes happen – espacially when you work on remote project. Just open the file in another editor and find out what’s inside. If it’s the first reason we need to check which class is actually loaded. For that you can use static method:

public static function getPathToObject($object, $method = '__construct')
{
    $object = new ReflectionObject($object);
    $method = $object->getMethod($method);
    $declaringClass = $method->getDeclaringClass();
    $filepath = $declaringClass->getFilename();
    return $filepath;
}

It uses reflection to find path to loaded object. If the given path is different from the file you’re looking at then you just need to rename one of the classes. If the paths are the same then… you can ask me in the comments and I’ll try to help 🙂

Author : beriba | 08/27/2012

Sometimes building query using Criteria can be very painful. And that’s a fact. There is also another thing – Propel is not as powerful as plain SQL. Sometimes you need to make very complex query to get some data from your database. If you’re in that point, consider using a view. Of course if your database supports them (PostgreSQL and MySQL does).

When view is not what you are looking for or you want to strictly write SQL query and execute it via Propel, here’s how to do this:

$sql = "SELECT * FROM table;"; //your SQL query
$conn = Propel::getConnection();
$st = $conn->prepare($sql);
$st->execute();
$results = $st->fetchAll(PDO::FETCH_ASSOC);
//$results - associative array of results
//each element is as row
//every element is an array where keys are names of columns 
//and values are just values of these columns
Author : beriba | 08/20/2012

To track newly added lines to file in Linux you can use command

tail -f filename.txt

It will show you every line added at the end of the filename.txt, exactly when it will be added to this file. It is very useful to make some new lines (after executing the command) to separate existing content from the one that will show up later. But don’t worry. Those new lines won’t be added to file.

Author : beriba | 08/17/2012

Sometimes when you’re building Criteria object you want to know what exactly Propel will produce. To be sure about the query or to debug it. You can do it as simple as that:

$params = array();
$sql = BasePeer::createSelectSql($criteria, $params);
var_dump($sql); //SQL query
var_dump($params); //array of params passed to SQL