Fixing error in toString method Doctrine Record
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 20 May 2012 19:40:01 +0000 (21:40 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 20 May 2012 19:40:01 +0000 (21:40 +0200)
I was not retrieving the right language id value in the
Symfony Doctrine Record object.

lib/model/doctrine/Ad.class.php
lib/model/doctrine/Company.class.php
lib/model/doctrine/GeneralCategory.class.php

index 0b20ca5..8a9351e 100644 (file)
@@ -34,7 +34,7 @@ class Ad extends BaseAd
 
     //Otherwise return with the default language
     $languageCode = sfConfig::get('app_default_language');
-    $languageId = LanguageTable::getInstance()->findByCode($languageCode);
+    $languageId = LanguageTable::getInstance()->findOneByCode($languageCode)->getId();
     foreach ($adDescriptions as $adDescription)
     {
         if ($adDescription->getLanguageId() == $languageId)
index 21bf5a9..b4a1b27 100644 (file)
@@ -7,9 +7,45 @@
  * 
  * @package    mobiads
  * @subpackage model
- * @author     Your name here
- * @version    SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $
+ * @author     Gustavo Martin Morcuende
+ * @version
  */
 class Company extends BaseCompany
 {
+
+ /**
+  * Returns the string representation of this object.
+  *
+  * @return string
+  */
+  public function __toString()
+  {
+    $languageId = sfContext::getInstance()->getUser()->getGuardUser()->getLanguage()->getId();
+
+    //Check if there is description with the user's language
+    $companyDescriptions = CompanyDescriptionTable::getInstance()->findByCompanyId($this->getId());
+    foreach ($companyDescriptions as $companyDescription)
+    {
+        if ($companyDescription->getLanguageId() == $languageId)
+        {
+           //We found it!!!
+           return (string) $companyDescription->getCompanyName();
+        }
+    }
+
+    //Otherwise return with the default language
+    $languageCode = sfConfig::get('app_default_language');
+    $languageId = LanguageTable::getInstance()->findOneByCode($languageCode)->getId();
+    foreach ($companyDescriptions as $companyDescription)
+    {
+        if ($companyDescription->getLanguageId() == $languageId)
+        {
+           //We found the default name description!!!
+           return (string) $companyDescription->getCompanyName();
+        }
+    }
+
+    //Finally, if nothing was found, return nice error message.
+    return (string) "Company without default language";
+  }
 }
index a96f051..5419fb6 100644 (file)
@@ -34,7 +34,7 @@ class GeneralCategory extends BaseGeneralCategory
 
     //Otherwise return with the default language
     $languageCode = sfConfig::get('app_default_language');
-    $languageId = LanguageTable::getInstance()->findByCode($languageCode);
+    $languageId = LanguageTable::getInstance()->findOneByCode($languageCode)->getId();
     foreach ($generalCategoryDescriptions as $generalCategoryDescription)
     {
         if ($generalCategoryDescription->getLanguageId() == $languageId)