Tag Archives: doctrine1.2

Force MySQL timezone on RDS in a Symfony1 project

If you need to force a specific MySQL timezone for your symfony1 project you should edit ProjectConfiguration.class.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class ProjectConfiguration extends sfProjectConfiguration
{
    ...
    public function configureDoctrineConnection(Doctrine_Connection $connection)
    {
        $mysql_timezone = sfConfig::get('app_mysql_timezone', 'SYSTEM');
        try
        {
            $connection->exec('SET time_zone = "' . $mysql_timezone . '"');
        } catch (Exception $e)
        {
            /* I'm here because if an invalid timezone
             * is given I'm silently (and cowardly),
             * ignoring it and using MySQL's default
             * timezone instead */

        }
    }
    ...
}
up and down arrow

Symfony 1.4 embed ordered Doctrine relations

Imagine a scenario where a “Product has N ordered Photos”:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Product:
  columns
:
    name
:              { type: string(128), notnull: true }
 
Photo
:
  columns
:
    url
:         { type: string(255) }
    product_id
:  { type: integer }
    position
:    { type: integer }
  relations
:
    Product
:
      local
:           product_id
      foreign
:         id
      foreignAlias
:    Photos
      onDelete
:        CASCADE

In the ProductForm you can embed the related Photos with:

1
2
3
4
5
6
7
class ProductForm extends BaseProductForm
{
    public function configure()
    {
        $this->embedRelation('Photos')
    }
}

Problem: The related Photos will be displayed ordered by their id

Solution: Specify the relation between Product and Photos on the schema.yml file and provide a orderBy attribute:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Product:
  columns
:
    name
:              { type: string(128), notnull: true }
  relations
:
    Photos
:
      type
:     many
      class
:    Photo
      local
:    id
      foreign
:  product_id
      orderBy
:  position ASC   # this is the magic part!
      onDelete
: CASCADE
 
Photo
:
  columns
:
    url
:         { type: string(255) }
    product_id
:  { type: integer }
    position
:    { type: integer }
  relations
:
    Product
:
      local
:           product_id
      foreign
:         id
      foreignAlias
:    Photos
      onDelete
:        CASCADE

Example adapted from: http://devblog.lexik.fr/symfony/tips-symfony-1-31-4-orderby-des-relations-1026

Symfony1.4 and Doctrine1.2 setting and getting child elements in execution time

Question

I’m using Symfony1.4 and Doctrine1.2 to create children objects and print them in execution time. The problem is that I always get the children objects from the first query.

I have the following schema:

1
2
3
4
5
6
7
8
Parent:
  id
  name

Child:
  id
  parent_id
  name

My operations:

  1. Initially I have one Parent (with id=1), and no Child
  2. I grab a Parent (id=1) and store that object on $parent
  3. List it’s Child objects
  4. Result: none OK: as expected
  5. Create a new Child and set it’s parent to 1
  6. List $parent‘s Child objects
  7. Result: none OOPS: I expected the new Child from 5.!

Code:

1
2
3
4
5
6
$parent = Doctrine_Query::create()->from('Parent p')->where('p.id = ?', 1)->limit(1)->fetchOne(); /* from 2. */
print_r($parent->getChild()->toArray()); /* from 3. */
$child = new Child();
$child->setParentId($parent->getId());
$child->save(); /* from 4. */
print_r($parent->getChild()->toArray()); /* from 6. */

Note, from Doctrine’s comments:

1
2
3
4
5
6
7
8
9
10
11
/**
* refresh
* refresh internal data from the database
*
* @param bool $deep                        If true, fetch also current relations. Caution: this deletes
*                                          any aggregated values you may have queried beforee
*
* @throws Doctrine_Record_Exception        When the refresh operation fails (when the database row
*                                          this record represents does not exist anymore)
* @return boolean
*/

I’ve already tried re-grabbing the $parent before the last line, but the result is the same.

Own Answer

I found a work-around for this issue:

1
2
3
4
5
6
7
$parent = Doctrine_Query::create()->from('Parent p')->where('p.id = ?', 1)->limit(1)->fetchOne();
print_r($parent->getChild()->toArray());
$child = new Child();
$child->setParentId($parent->getId());
$child->save();
$parent->refresh(true); /* see note! */
print_r($parent->getChild()->toArray());

http://stackoverflow.com/questions/18859509/symfony1-4-and-doctrine1-2-setting-and-getting-child-elements-in-execution-time