Using optgroup in sfWidgetFormPropelChoice since the values of a column - symfony1

I'm in a project that uses Symfony 1.2 (I know this is quite old, but I can't do anything about this).
I have a sfWidgetFormPropelChoice widget which feed its options from a foreign key than exists say in table A. I would like to group that foreign key since another column in table A, with the optgroup HTML element.
So I have something like that:
$this->widgetSchema['B_has_A_list'] = new sfWidgetFormPropelChoiceMany(array(
'model' => 'TableA',
));
And I would like to have something that easy:
$this->widgetSchema['B_has_A_list'] = new sfWidgetFormPropelChoiceMany(array(
'model' => 'TableA',
'optgroup' => 'ColumnInTableA', //That's not possible. It would group options grouping by ColumnInTableA using the optgroup HTML element
));
Do you know another easy way to achieve this. Or do you know any extended sfWidgetFormPropelChoice that achieve this?
Thank you.

Ok, I extended the symfony widget to achieve this. If anybody is interesed, it's in github:
https://github.com/laMarciana/sfWidgetFormChoiceOptgroup
To use you have to define the same required options than sfWidgetFormPropelChoice plus a optgroup_columnoption with the PhpName of the column you want to use as the optgroups.
Ex:
$this->widgetSchema['field'] = new sfWidgetFormPropelChoiceOptgroup(array(
'model' => 'Table',
'optgroup_column' => 'Column',
));
I added as well a version for Doctrine.

Related

How can I use fromJson to List inside Map?

Usually, I will use this code to make to list.
jsonMap.map((gallery) => new Gallery.fromJson(gallery)).toList();
But I don't know how to use with a fromJson.
This is a picture of my code.
I have solved this problem by using.
List.castFrom(json['galleries']).map((gallery) => new Gallery.fromJson(gallery)).toList();

Grid sorting with Telerik.Web.Mvc.UI Grid

I am using the Telerik.Web.Mvc.UI Grid to show a grid on an Asp.Net MVC website.
All of the columns are supposed to be sortable / filterable, but as you will see from the image below, the only who that actually do have a Sort / Filter in the right place are the File Name column and the Project Requested By column.
These columns are bound by the following code...
c.Bound(o => o.FileName);
c.Bound(o => o.Dto.ProjectDetailsDto.ProjectRequiredBy)
.Template(o => o.Dto.ProjectDetailsDto.ProjectRequiredBy.HasValue ? o.Dto.ProjectDetailsDto.ProjectRequiredBy.Value.ToString(Model.DateTimePattern) : "")
.HeaderHtmlAttributes(Resources.EditorLocalization.ProjectDeadline);
Let's limit the problem, and ask ourselves why the Project Name is not sortable (it isn't). The project name is bound like so :
c.Bound(o => o.Dto.ProjectName)
.Template(o => Html.ActionLink(o.Dto.ProjectName, "Details", "Project", new { projectId = o.Dto.AssetTask.ProjectId }, null))
.HeaderTemplate(Resources.EditorLocalization.ProjectName)
.Sortable(true)
.Filterable(true);
First of all, I considered that it might be that columns that use a template cannot be sorted - only they can, because we have the working case of the Project Required By Column.
Perhaps, I thought, it is the presence of the HeaderTemplate() that is causing us problems, but then why would it? Still, I can't deny, everything I believe should work, but doesn't, has a HeaderTemplate.
So - let's blame that and see what we can do... The reason I am using a HeaderTemplate is because we want to be able to be able to localise the strings used to label the columns (ie the headers). Is there any other approach we might take which would allow us to do that, but also mean we could have sorting on the grid as well?
Thanks in anticpation...
Ok - the problem was indeed down to my use of HeaderTemplate(). If I use the Title() method instead, all is well...

ZF2 Custom query

Hello and good morning to you all,
I am following the qickstart tutorial from Rob Allen in the manual.
I am trying to change some things. One of the things I am trying to do is to get a query like this:
"SELECT max(id) FROM Albums";
I tried things like
$this->select();
$this->columns(array('id' => 'MAX(id)'));
Apparently this is not the way to do it.
I probablly need some expression object or so.
Can anyone tell me how to solve this?
EDIT (forget the above)
This whole code is based on the quickstart in the manual (ZF2)
I have managed to write a query like this:
$select = $this->getSql()->select();
$select->columns(array(new Expression('max(id) as MaxId')));
$rowset = $this->selectWith($select);
$row = $rowset->current();
return $row;
The result of this is an empty object.
But when i change
$select->columns(array(new Expression('max(id) as MaxId')));
to
$select->columns(array(new Expression('max(id) as id')));
then i get back an object with the id as 1. Which is the max(id).
But when I add in my album object in the function exchangeArray one line with maxId, then it returns the maxId field.
BUT, it can't be that i need to do this everytime i just want to do a query like this. Is this really the way it works?
Use Zend\Db\Sql\Expression
So if i see this correctly (which may not be the case) you'd do it like you just did, but wrap the SQL-Expression into new Expression('max(id)). So it should be like the following
use Zend\Db\Sql\Expression;
//...
$this->columns(array(
'maxid' => new Expression('max(id)')
));
If the syntax like this is wrong, please don't curse me, but i would assume that knowing about the Sql\Expression will already help you ;)

Rails 3 - How to use autocomplete with a few db fields as sources?

Hey guys!
Details
I'm using the rails3-jquery-autocomplete gem (https://github.com/crowdint/rails3-jquery-autocomplete) for my search feature.
To realize the search, I'm using meta-search gem.
Problem
My request is to realize a search on a radio model which contains few columns. Meta-search is able to do that but autocomplete gem seems to be currently limited to autocomplete from only one field at a time. I would like that the autocomplete will take as sources every field of the radio model.
Moreover, there are a few foreign keys in the radio model and I would like to get their corresponding values.
Questions
Did you have such a problem or do you know how to face with it?
I looked around over all the questions in stackoveflow, and tried to use the commits of that guy who implemented himself that feature -> Issue #15 on the gem github page
I would need maybe another gem or way to implement this autocomplete, so if you have any idea, don't hesitate to let me know.
Thx
There are several options, how to do things like that.
One of them is to get some js autocomplete script, that can use external xml/json data source.
I like (and use) this one - http://www.codeproject.com/KB/scripting/AutoSuggestControl.aspx
Using it, what you need to do is to implement some action for some controller, that will query all data sources you want with "LIKE"-style query (getting all words that start with or contain a parameter given) and return it all in way like this:
format.xml { response.headers["Content-Type"] = "application/xml; charset=utf-8";
render :inline => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+
"<listdata>#{#tags.join('|')}</listdata>" };
#tags here is an array of all keywords you got with your query (or series of queries).
Your template will have a code like this:
<script>
var obj = new autosuggest("someid", "", "http://mysite.org/tags.xml?term=");
</script>
Where "someid" is an "id" attribute of your search input, "/suggest.xml" is a path to your controller/action and "term" is your parameter for that action.
I hope this could help you.
Finally, I used the fork of some guy who implemented interesting stuff. But that wasn't working properly so I forked his fork and I modified to be able to use it.
If you need help to autocomplete from few fields and tables on rails 3, ruby 1.8.7 then you can give a try to my fork of the rails3-jquery-autocomplete gem => https://github.com/dbachet/rails3-jquery-autocomplete
There is no support for mongoid, and options were not tested.
Thx

How to "order by" a sfWidgetFormDoctrineChoice in the Admin Generator

I'm using Symfony 1.4 and Doctrine.
Let's say I have 2 classes : a Brand and a Product.
When I create a new product in the Admin Generator based admin, I'd like to choose a brand from a dropdown list.
The Admin Generator is doing that for me, automatically creating a sfWidgetFormDoctrineChoice.
The problem is that the brands are ordered by id. I'd like them to be ordered by their "label" field.
In order to do that I did the following in my ProductForm class:
$this->widgetSchema['brand_id']->addOption('order_by','label');
But I get the following error:
Syntax error or access violation: 1064
You have an error in your SQL syntax;
check the manual that corresponds to
your MySQL server version for the
right syntax to use near 'a' at line
1. Failing Query: "SELECT b.id AS b__id, b.external_id AS
b__external_id, b.label AS b__label,
b.created_at AS b__created_at,
b.updated_at AS b__updated_at FROM
brand b ORDER BY l a"
The order by statement is really weird. I don't understand why it seems to cut the name of the order by statement.
Edit: Apparently the 'order_by' option is expecting an array as a second parameter. What values does it expect?
I didn't try benlumley's solution since he answered right when I found my solution. It seems more tedious than what I ended doing.
I took a look at the source code to figure out how all this was working.
It turns out the "order_by" option needs an array specifying the field on which one wants to order the results and either 'asc' or 'desc':
$this->widgetSchema['product_id']->addOption('order_by',array('label','asc'));
It works like a charm.
You should take a look here:
http://trac.symfony-project.org/wiki/HowtoSortAdminGeneratorListByForeignTableName
Its based off of an old version of symfony, so suspect the plugin it links won't work. But I think the method should still be sound - crux of it is that you have to add method to the action to intercept and amend the default handling of sorting by this specific field:
For doctrine, you need to define/override addSortQuery, for propel, addSortCriteria.
Recommend you take a look in the cache folder to see what the auto-generated classes look like to get the hang of how it works.

Resources