This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

Admin generator foreign key sort Doctrine (Solved)

Questions relating to sfGuardPlugin or other user management plugins

Admin generator foreign key sort Doctrine (Solved)

by rernst » Mon Apr 05, 2010 1:10 am

Hi,

I have seen a variety of posts on the subject but no real solution (?!?) for my case. I am running 1.4 with Doctrine. The Advanced Admin Generator plugin has not been available for a long time.

Does anyone know how to get this to work again?
rernst
Member
 
Posts: 83
Joined: Thu Oct 15, 2009 9:46 pm

Re: Admin generator foreign key sort Doctrine

by rernst » Tue Apr 06, 2010 2:25 am

Bump? I thought Doctrine was the default ORM for 1.4 now, now foreign key sort possible? Can someone at least verify this?

I have looked at the generated code and I guess the method to add the sort would be possible but I do not see how to establish the column header click with the action that manages the sort.
rernst
Member
 
Posts: 83
Joined: Thu Oct 15, 2009 9:46 pm

Re: Admin generator foreign key sort Doctrine

by rernst » Thu Apr 08, 2010 3:19 pm

Bump another time. How do I modify this thing (Doctrine) to achieve this? Solutions are for Propel.
rernst
Member
 
Posts: 83
Joined: Thu Oct 15, 2009 9:46 pm

Re: Admin generator foreign key sort Doctrine

by chris89 » Sun Apr 11, 2010 2:11 pm

Have you found a solution for this problem yet? I'd be very intressed in this because i have the same Problem in my Backend, too.
chris89
Junior Member
 
Posts: 10
Joined: Sat Jan 31, 2009 3:37 pm

Re: Admin generator foreign key sort Doctrine

by Fabrizio » Sun Apr 11, 2010 4:06 pm

I too have the same problem.
Any advice on how you can add sorting to columns with foreign key?
Fabrizio
Junior Member
 
Posts: 5
Joined: Mon Jan 04, 2010 6:24 pm
Location: Italy

Re: Admin generator foreign key sort Doctrine

by rernst » Tue Apr 13, 2010 3:27 am

You new, never did. I have the sincere belief that despite the 'officialdom' of Doctrine for 1.4 there is only a tiny fraction of actual users. Sad to see and even sadder to have invested effort into it. :oops:

I think eventually this will change and Doctrine certainly at this point is the stronger option but Propel is far from dead and as far as true usability is concerned the preferred option.

My two cents.
rernst
Member
 
Posts: 83
Joined: Thu Oct 15, 2009 9:46 pm

Re: Admin generator foreign key sort Doctrine

by rernst » Fri May 07, 2010 12:38 am

Ok, this is how I solved the problem. First of all I overrode AddSortQuery in my actions class like this:

Code: Select all
    protected function addSortQuery($query) {
        if (array(null, null) == ($sort = $this->getSort())) {
            return;
        }
        $s = $sort[0];
        $fields = $this->varHolder->get('configuration')->getFieldsDefault();
        if ($fields != null) {
            $field = $fields[$s];
            if ($field != null) {
                if (isset($field["sortBy"])) {
                    $criterion = $field["sortBy"];
                    if ($criterion != null) {
                        $s = $criterion;
                    }
                }
            }

        }

        $query->addOrderBy($s . ' ' . $sort[1]);
    }

What this essentially does is check whether the configuration file (yml generator) has a 'sortBy' definition. This would look like this:

Code: Select all
        breed:            { label: Primary breed, sortBy : b.description }


The sortBy definition defines the table and field in that table that this foreign key references. This allows flexibility in respect to the fact that any field can be used and there is no need to enforce naming conventions.

Now we still need to make sure that the hyperlink for sorting shows up in the header of the table.

The second part is a bit of a pain because it requires you to override a template in the admin generator. You either copy the existing admin generator template (and thus create your own admin generator) or edit the existing one (I use the sfAdminThemejRollerPlugin so I simply changed that one - recommended plugin but has a few bugs that I fixed first).

The file you are after is _list_th_tabular.php. Towards the top of the file is a line that reads something like this:

Code: Select all
<?php if ($field->isReal()): ?>


This checks whether the field in question is an actual column in the table. All you have to do is add a definition that checks whether 'orderBy' has been defined for this field, so change the line to read:

Code: Select all
<?php if ($field->isReal() || $field->getConfig('sortBy')): ?>


Note that this may look slightly different depending upon which exact admin theme you are using.

If you need to sort by partials you can work similarly. Note that this method also allows you to override what exactly you sort by if you click on 'real' sort by table columns.

Hope this all makes sense.
rernst
Member
 
Posts: 83
Joined: Thu Oct 15, 2009 9:46 pm

Re: Admin generator foreign key sort Doctrine

by bodidd » Tue Jul 20, 2010 7:55 pm

not sure what version post by rernst was for. for 1.4, this worked for me. thanks to rernst because i wouldn't have come up with it on my own.

Code: Select all
  protected function addSortQuery($query)
  {
    $rootAlias = $query->getRootAlias();
    if (array(null, null) == ($sort = $this->getSort()))
    {
      return;
    }
    $s = $sort[0];
    $fields = $this->varHolder->get('configuration')->getFieldsDefault();
    if ($fields != null)
    {
      $field = $fields[$s];
      if ($field != null)
      {
        if (isset($field["sortBy"]))
        {
          $criterion = $field["sortBy"];
          if ($criterion != null)
          {
            $s = $criterion;
          }
        }
      }
    }
    $query->addOrderBy($rootAlias.'.'.$s . ' ' . $sort[1]);
  }
bodidd
Junior Member
 
Posts: 1
Joined: Tue Jul 20, 2010 7:49 pm

Re: Admin generator foreign key sort Doctrine

by Andromeda » Tue Jan 04, 2011 2:33 pm

This doesn't work for me, cause there is a method that checks the sort-parameter...
Code: Select all
  protected function isValidSortColumn($column)
  {
    return Doctrine_Core::getTable('Logging')->hasColumn($column);
  }

Except of overwriting and always return true, anybody an idea?

The other problem is that copying the template _list_th_tabular.php brings the folowing error...
Code: Select all
Notice: Undefined property: sfPartialView::$configuration in ...\apps\frontend\modules\sys_logging\templates\_list_th_tabular.php on line 1

Because $this is of type sfPartialView and not sfDoctrineGenerator
Andromeda
Member
 
Posts: 78
Joined: Thu Jul 03, 2008 6:55 pm
Location: Berlin, Germany

Re: Admin generator foreign key sort Doctrine (Solved)

by BusinessStream » Tue Feb 28, 2012 3:23 pm

rernst wrote:Hi,



I have seen a variety of posts on the subject but no real solution (?!?) for my case. I am running 1.4 with Doctrine. The Advanced Admin Generator plugin has not been available for a long time.



Does anyone know how to get this to work again?


Can some one please answer this question because I am having the same problem?
BusinessStream
Junior Member
 
Posts: 1
Joined: Tue Feb 28, 2012 3:17 pm