This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » support » symfony 1.3 and 1.4 » Multiple Propel 1.5 aggregate_column behaviors in schema.yml - is it possible?
Multiple Propel 1.5 aggregate_column behaviors in schema.yml - is it possible? [message #102572] Fri, 16 July 2010 10:52 Go to next message
zsoltk  is currently offline zsoltk
Messages: 5
Registered: July 2010
Location: Budapest
Junior Member
Hello,

I'm using Symfony 1.4 + Propel 1.5. I'd like to set multiple aggregate columns on a table.

As in the documentation of Propel 1.5:
http://www.propelorm.org/wiki/Documentation/1.5/Behaviors/ag gregate_column , one can do it only by adding the aggregate_column behavior multiple times.

However (as I dislike writing schema in xml) when my schema.yml gets converted to xml (and then the sql is generated based on it), I end up having only one aggregate_column behavior on the table. I guess it's because they have the same name and the last one overwrites all the previous.

So:

  product:
    _propel_behaviors:
      aggregate_column: { name: nb_images, foreign_table: product_image, expression: "COUNT(id)" }
      aggregate_column: { name: nb_testimonials, foreign_table: product_testimonial, expression: "COUNT(id)" }


ends up

  <table name="product">
    <behavior name="aggregate_column">
      <parameter name="name" value="nb_testimonials" />
      <parameter name="foreign_table" value="product_testimonial" />
      <parameter name="expression" value="COUNT(id)" />
    </behavior>


and the nb_images is lost.

I googled a lot, couldn't even find anyone having this problem. Does anyone have any idea how to solve it?

Thanks a lot in advance.

Re: Multiple Propel 1.5 aggregate_column behaviors in schema.yml - is it possible? [message #102618 is a reply to message #102572 ] Fri, 16 July 2010 16:11 Go to previous messageGo to next message
sescandell  is currently offline sescandell
Messages: 65
Registered: December 2008
Location: France
Member
Hi,

If we think a little on how a YAML file (and the YAML component from Symfony) is working, I think this behavior is normal.
I mean, as you exactly say, during the XML generation, first it will define the first aggregate_column and then overwrite it when parsing the second one.

So thinking on how a YAML file is working, maybe you can try something like that :
 product:
    _propel_behaviors:
      aggregate_column: 
        aggregate_1: { name: nb_images, foreign_table: product_image, expression: "COUNT(id)" }
        aggregate_2: { name: nb_testimonials, foreign_table: product_testimonial, expression: "COUNT(id)" }


So, maybe it will understand that you want two aggregate_column (but I am not sure ... just trying to contribute to your problem).


Good luck
Re: Multiple Propel 1.5 aggregate_column behaviors in schema.yml - is it possible? [message #102631 is a reply to message #102618 ] Fri, 16 July 2010 19:51 Go to previous messageGo to next message
zsoltk  is currently offline zsoltk
Messages: 5
Registered: July 2010
Location: Budapest
Junior Member
Hi,

Thanks for your reply. Thinking along the same logic I've already tried what you wrote. When running the propel:build-sql task, it fails with the following error:

>> propel    Running "sql" phing task
[phingcall] You must define a 'name' parameter for the 'aggregate_column' behavior in the 'product' table

Execution of target "sql" failed for the following reason: [...]\plugins\sfPropel15Plugin\lib\vendor\propel-generator\build-propel.xml:179:22: Execution of the target buildfile failed. Aborting.

    [phing] [...]\plugins\sfPropel15Plugin\lib\vendor\propel-generator\build-propel.xml:179:22: Execution of the target buildfile failed. Aborting.


  Some problems occurred when executing the task:



  build-propel.xml:179:22: Execution of the target buildfile failed. Aborting.


Phing just doesn't like it this way. What now?
Re: Multiple Propel 1.5 aggregate_column behaviors in schema.yml - is it possible? [message #102652 is a reply to message #102631 ] Sat, 17 July 2010 11:50 Go to previous messageGo to next message
zsoltk  is currently offline zsoltk
Messages: 5
Registered: July 2010
Location: Budapest
Junior Member
Can someone tell me whether this is a Symfony or a phing related problem?
Re: Multiple Propel 1.5 aggregate_column behaviors in schema.yml - is it possible? [message #102658 is a reply to message #102572 ] Sat, 17 July 2010 14:56 Go to previous messageGo to next message
sescandell  is currently offline sescandell
Messages: 65
Registered: December 2008
Location: France
Member
Hi,

I found where the problem came from.
It is in the file lib\plugins\sfPropelPlugin\lib\addon\sfPropelDatabaseSchema. class.php (starting on line 385)

I am making a hack that I will post in some minutes Very Happy

[Updated on: Sat, 17 July 2010 15:15]

Re: Multiple Propel 1.5 aggregate_column behaviors in schema.yml - is it possible? [message #102659 is a reply to message #102572 ] Sat, 17 July 2010 15:07 Go to previous messageGo to next message
sescandell  is currently offline sescandell
Messages: 65
Registered: December 2008
Location: France
Member
Hi again !

Here is my hack :
In the file lib\plugins\sfPropelPlugin\lib\addon\sfPropelDatabaseSchema. class.php on line 383 (symfony1.4), replace :
if (isset($table['_propel_behaviors']))
{
          if ($parameters)
          {
            $xml .= "    <behavior name=\"$behavior_name\">\n";
            foreach ($parameters as $param_name => $param_value)
            {
              $xml .= "      <parameter name=\"$param_name\" value=\"{$this->fixXMLBoolean($param_value)}\" />\n";
            }
            $xml .= "    </behavior>\n";
          }
          else
          {
            $xml .= "    <behavior name=\"$behavior_name\" />\n";
          }
        }


by this code :

if (isset($table['_propel_behaviors']))
      {
        foreach ($table['_propel_behaviors'] as $behavior_name => $parameters)
        {
          if (is_array($parameters) && count($parameters))
          {
            foreach($parameters as $behavior_identifier => $params)
            {
              $xml .= "\n  <behavior name=\"$behavior_name\"";
              if (is_array($params) && count($params))
              {
                $xml .= ">\n";
                foreach ($params as $key => $value)
                {
                  $xml .= "    <parameter name=\"$key\" value=\"{$this->fixXMLBoolean($value)}\"/>\n";
                }
                $xml .= "  </behavior>\n";
              }
              else
              {
                $xml .= "/>\n";
              }
            }
          }
          else
          {
            $xml .= "\n  <behavior name=\"$behavior_name\"";
            if (is_array($parameters) && count($parameters))
            {
              $xml .= ">\n";
              foreach ($parameters as $key => $value)
              {
                $xml .= "    <parameter name=\"$key\" value=\"{$this->fixXMLBoolean($value)}\"/>\n";
              }
              $xml .= "  </behavior>\n";
            }
            else
            {
              $xml .= "/>\n";
            }
          }
        }


For the following schema:
propel:
  product_image:
    id: ~

  product_testimonial:
    id: ~

  product:
    id: ~
    _propel_behaviors:
      aggregate_column:
        test1:
          name: nb_images
          foreign_table: product_image
          expression: "COUNT(id)"
        test2:
          name: nb_testimonials
          foreign_table: product_testimonial
          expression: "COUNT(id)"


it will generate the following file:
<?xml version="1.0" encoding="UTF-8"?>
<database name="propel" defaultIdMethod="native" package="lib.model">

  <table name="product_image">
    <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
  </table>

  <table name="product_testimonial">
    <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
  </table>

  <table name="product">

  <behavior name="aggregate_column">
    <parameter name="name" value="nb_images"/>
    <parameter name="foreign_table" value="product_image"/>
    <parameter name="expression" value="COUNT(id)"/>
  </behavior>

  <behavior name="aggregate_column">
    <parameter name="name" value="nb_testimonials"/>
    <parameter name="foreign_table" value="product_testimonial"/>
    <parameter name="expression" value="COUNT(id)"/>
  </behavior>
    <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
  </table>

</database>




Take care ! I didn't tested this hack in all possible situations ...

By the way, in the same file, on line 327, I think we can also replace the code
if (isset($this->database['_propel_behaviors']))
    {
      foreach ($this->database['_propel_behaviors'] as $name => $parameters)
      {
        $xml .= "\n  <behavior name=\"$name\"";
        if (is_array($parameters) && count($parameters))
        {
          $xml .= ">\n";
          foreach ($parameters as $key => $value)
          {
            $xml .= "    <parameter name=\"$key\" value=\"{$this->fixXMLBoolean($value)}\"/>\n";
          }
          $xml .= "  </behavior>\n";
        }
        else
        {
          $xml .= "/>\n";
        }
      }
    }


by
if (isset($this->database['_propel_behaviors']))
    {
      foreach ($this->database['_propel_behaviors'] as $name => $parameters)
      {
        if (is_array($parameters) && count($parameters))
        {
          foreach($parameters as $behavior_identifier => $params)
          {
            $xml .= "\n  <behavior name=\"$name\"";
            if (is_array($params) && count($params))
            {
              $xml .= ">\n";
              foreach ($params as $key => $value)
              {
                $xml .= "    <parameter name=\"$key\" value=\"{$this->fixXMLBoolean($value)}\"/>\n";
              }
              $xml .= "  </behavior>\n";
            }
            else
            {
              $xml .= "/>\n";
            }
          }
        }
        else
        {
          $xml .= "\n  <behavior name=\"$name\"";
          if (is_array($parameters) && count($parameters))
          {
            $xml .= ">\n";
            foreach ($parameters as $key => $value)
            {
              $xml .= "    <parameter name=\"$key\" value=\"{$this->fixXMLBoolean($value)}\"/>\n";
            }
            $xml .= "  </behavior>\n";
          }
          else
          {
            $xml .= "/>\n";
          }
        }
      }
    }


(I didn't try it)

See you !

[Updated on: Sat, 17 July 2010 15:09]

Re: Multiple Propel 1.5 aggregate_column behaviors in schema.yml - is it possible? [message #102660 is a reply to message #102572 ] Sat, 17 July 2010 15:14 Go to previous messageGo to next message
sescandell  is currently offline sescandell
Messages: 65
Registered: December 2008
Location: France
Member
Hi again (and again),

I tested my hack with the following schema:
propel:
  product_image:
    id: ~

  product_testimonial:
    id: ~

  product:
    id: ~
    _propel_behaviors:
      aggregate_column: 
        name: nb_images
        foreign_table: product_image
        expression: "COUNT(id)"


But it's not working anymore. With my code, you have to give a name to the behavior like this :
propel:
  product_image:
    id: ~

  product_testimonial:
    id: ~

  product:
    id: ~
    _propel_behaviors:
      aggregate_column: 
        test1:
          name: nb_images
          foreign_table: product_image
          expression: "COUNT(id)"


I will open a ticket on the development roadmap, and they will find the better way to solve this issue Very Happy

[Updated on: Sat, 17 July 2010 15:14]

Re: Multiple Propel 1.5 aggregate_column behaviors in schema.yml - is it possible? [message #102661 is a reply to message #102660 ] Sat, 17 July 2010 15:19 Go to previous messageGo to next message
zsoltk  is currently offline zsoltk
Messages: 5
Registered: July 2010
Location: Budapest
Junior Member
Thank you so much for your attention! Smile
Re: Multiple Propel 1.5 aggregate_column behaviors in schema.yml - is it possible? [message #102689 is a reply to message #102572 ] Sun, 18 July 2010 04:56 Go to previous message
sescandell  is currently offline sescandell
Messages: 65
Registered: December 2008
Location: France
Member
You are welcome Very Happy


By the way, because we have to define an identifier, I modified the patch:
if (isset($this->database['_propel_behaviors']))
    {
      foreach ($this->database['_propel_behaviors'] as $name => $parameters)
      {
        if (is_array($parameters) && count($parameters))
        {
          foreach($parameters as $behavior_identifier => $params)
          {
            
            if (is_array($params) && count($params))
            {
              $xml .= "\n  <behavior name=\"$name\">\n";
              foreach ($params as $key => $value)
              {
                $xml .= "    <parameter name=\"$key\" value=\"{$this->fixXMLBoolean($value)}\"/>\n";
              }
              $xml .= "  </behavior>\n";
            }
            else
            {
              throw new sfException(sprintf('Incorrect database behavior configuration.'));
            }
          }
        }
        else
        {
          throw new sfException(sprintf('Incorrect database behavior configuration.'));
        }
      }
    }


if (isset($table['_propel_behaviors']))
      {
        foreach ($table['_propel_behaviors'] as $behavior_name => $parameters)
        {
          if (is_array($parameters) && count($parameters))
          {
            foreach($parameters as $behavior_identifier => $params)
            {
              if (is_array($params) && count($params))
              {
                $xml .= "\n  <behavior name=\"$behavior_name\">\n";
                foreach ($params as $key => $value)
                {
                  $xml .= "    <parameter name=\"$key\" value=\"{$this->fixXMLBoolean($value)}\"/>\n";
                }
                $xml .= "  </behavior>\n";
              }
              else
              {
                throw new sfException(sprintf('Incorrect behavior configuration for "%s" table.', $tb_name));
              }
            }
          }
          else
          {
            throw new sfException(sprintf('Incorrect behavior configuration for "%s" table.', $tb_name));
          }
        }
      }
Previous Topic:admin generator language
Next Topic:Use View Filters in Model
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software