Additional criteria to symfony filter - symfony1

i'm working on a symfony project to manage a database. First i explain how it works:
In the database, all elements are associated to an unique element 'scene'. When a user accesses the application, chooses what scene he wants to see (it saves that in a user parameter). So when listing elements, the application should only list elements associated with the scene selected by the user.
*Note: all elements have an scene attribute in the table definition.
So my problem comes here:
I developed a listing of an element entities using the help of a sfPropelPager class to paginate. Also added some filters to search in the list, and for that i used the filter system provided by symfony (<element>FormFilter.class.php and stuff).
Now i want the list to not show elements from other scenes than the selected by the user.
How can i do to add additional criteria to the criteria given by the filter class?
or How would you solve the problem?
here is my action code:
public function executeUnidadfilter(sfWebRequest $request){
$this->filter = new BaUnidadorganizativaTblFormFilter();
$c = $this->filter->getCriteria();
$this->filter->bind($request->getParameter($this->filter->getName()));
if($this->filter->isValid()){
$this->pager = new sfPropelPager('BaUnidadorganizativaTbl',$this->sfPropelPagerLines);
echo $this->getUser()->getEscenario();
$this->pager->setCriteria($c);
$this->pager->init();
}else{
$this->pager = new sfPropelPager('BaUnidadorganizativaTbl',$this->sfPropelPagerLines);
$this->pager->init();
}
$this->setTemplate('Unidadlist');
}
*Note: 'scene' mentioned below is Escenario in the code
thank you very much for your time

I solved the problem. The trouble was that i assigned the formfilter generated criteria to my criteria var Before the filter was filled. That's why of the error.
The resulting code is that:
public function executeUnidadfilter(sfWebRequest $request){
$this->filter = new BaUnidadorganizativaTblFormFilter();
$this->filter->bind($request->getParameter($this->filter->getName()));
if($this->filter->isValid()){
$this->pager = new sfPropelPager('BaUnidadorganizativaTbl',$this->sfPropelPagerLines);
$esc = $this->getUser()->getEscenario();
$c = new Criteria();
$c = $this->filter->getCriteria();
$c->addAnd('codigo_escenario',$esc);
$this->pager->setCriteria($c);
$this->pager->init();
}else{
$this->pager = new sfPropelPager('BaUnidadorganizativaTbl',$this->sfPropelPagerLines);
$this->pager->setCriteria($this->filter->getCriteria());
$this->pager->init();
}
$this->message=null;
$this->messageType=null;
$this->setTemplate('Unidadlist');
}

Related

AWS DynamoDB session table keeps growing, can't delete expired sessions

the ASP.NET_SessionState table grows all the time, already at 18GB, not a sign of ever deleting expired sessions.
we have tried to execute DynamoDBSessionStateStore.DeleteExpiredSessions, but it seems to have no effect.
our system is running fine, sessions are created and end-users are not aware of the issue. however, it doesn't make sense the table keeps growing all the time...
we have triple checked permissions/security, everything seems to be in order. we use SDK version 3.1.0. what else remains to be checked?
With your table being over 18 GB, which is quite large (in this context), it does not surprise me that this isn't working after looking at the code for the DeleteExpiredSessions method on GitHub.
Here is the code:
public static void DeleteExpiredSessions(IAmazonDynamoDB dbClient, string tableName)
{
LogInfo("DeleteExpiredSessions");
Table table = Table.LoadTable(dbClient, tableName, DynamoDBEntryConversion.V1);
ScanFilter filter = new ScanFilter();
filter.AddCondition(ATTRIBUTE_EXPIRES, ScanOperator.LessThan, DateTime.Now);
ScanOperationConfig config = new ScanOperationConfig();
config.AttributesToGet = new List<string> { ATTRIBUTE_SESSION_ID };
config.Select = SelectValues.SpecificAttributes;
config.Filter = filter;
DocumentBatchWrite batchWrite = table.CreateBatchWrite();
Search search = table.Scan(config);
do
{
List<Document> page = search.GetNextSet();
foreach (var document in page)
{
batchWrite.AddItemToDelete(document);
}
} while (!search.IsDone);
batchWrite.Execute();
}
The above algorithm is executed in two parts. First it performs a Search (table scan) using a filter is used to identify all expired records. These are then added to a DocumentBatchWrite request that is executed as the second step.
Since your table is so large the table scan step will take a very, very long time to complete before a single record is deleted. Basically, the above algorithm is useful for lazy garbage collection on small tables, but does not scale well for large tables.
The best I can tell is that the execution of this is never actually getting past the table scan and you may be consuming all of the read throughput of your table.
A possible solution for you would be to run a slightly modified version of the above method on your own. You would want to call the the DocumentBatchWrite inside of the do-while loop so that records will start to be deleted before the table scan is concluded.
That would look like:
public static void DeleteExpiredSessions(IAmazonDynamoDB dbClient, string tableName)
{
LogInfo("DeleteExpiredSessions");
Table table = Table.LoadTable(dbClient, tableName, DynamoDBEntryConversion.V1);
ScanFilter filter = new ScanFilter();
filter.AddCondition(ATTRIBUTE_EXPIRES, ScanOperator.LessThan, DateTime.Now);
ScanOperationConfig config = new ScanOperationConfig();
config.AttributesToGet = new List<string> { ATTRIBUTE_SESSION_ID };
config.Select = SelectValues.SpecificAttributes;
config.Filter = filter;
Search search = table.Scan(config);
do
{
// Perform a batch delete for each page returned
DocumentBatchWrite batchWrite = table.CreateBatchWrite();
List<Document> page = search.GetNextSet();
foreach (var document in page)
{
batchWrite.AddItemToDelete(document);
}
batchWrite.Execute();
} while (!search.IsDone);
}
Note: I have not tested the above code, but it is just a simple modification to the open source code so it should work correctly, but would need to be tested to ensure the pagination works correctly on a table whose records are being deleted as it is being scanned.

How to populate autocopmlete box with Active Directory emails?

I'm looking for the way to get list of the emails and groups of emails from the active directory. The list will be used to populate autocomplete textbox, the same as in Outlook.
Have any of you done something like that in the past using Asp.Net MVC?
I made this in a project some time ago and here are few steps I think you need to take:
Create a directory entry for your domain
Create a directory searcher and initialize it with a filter
Create a search collection searching with previous filter
Iterate your search collection
Get your properties for current iteration
Get your email from property collection
Code example:
DirectoryEntry dir = new DirectoryEntry("LDAP://" + YourDomain, LoginUsername, LoginPassword);
DirectorySearcher search = new DirectorySearcher(dir);
search.Filter = "(&(objectClass=user)(objectCategory=person))";
SearchResultCollection searchResultCollection = search.FindAll();
if (searchResultCollection != null)
{
for (int i = 0; i < searchResultCollection.Count; i++)
{
SearchResult crt= searchResultCollection[i];
PropertyCollection properties= crt.GetDirectoryEntry().Properties;
// get email from properties["email"].Value
}
}
Some useful links: first , second , third

Creating Remarketing list with tracking code already on website

I am trying to use the Remarketing feature for the first time. I have got the Remarketing code from Adwords and placed it on the website.
Looking through the examples; i have garnered the below flow.
With #2 i can associate one of my Userlist with a predefined Adgroup to be shown during Remarketing. My question is how do i link the tracker ID that i have received (looks like var google_conversion_id = 9925XXXXX) with the code below? where is this linking done? UserListConversionTypes and BasicUserList both have ID's; am i supposed to set any of those or this is done automatically?
Any pointers/help will be appreciated.
Please also let me know if you fine any issue with the code below.
set up remarketing using the AdWords API in two steps:
Create a remarketing list.
Create a CriterionUserList to tie your list to an AdGroup.
1.Create a remarketing list
Creating a remarketing list involves the creation of two separate entities: the RemarketingList itself and its associated UserListConversionTypes also known as remarketing tags.
The following code shows how to create a remarketing list.
AdWordsServices adWordsServices, AdWordsSession session) throws Exception {
// Get the UserListService.
AdwordsUserListServiceInterface userListService =
adWordsServices.get(session, AdwordsUserListServiceInterface.class);
// Get the ConversionTrackerService.
ConversionTrackerServiceInterface conversionTrackerService =
adWordsServices.get(session, ConversionTrackerServiceInterface.class);
UserListConversionType conversionType = new UserListConversionType();
conversionType.setName("Mars cruise customers #" + System.currentTimeMillis());
// Create remarketing user list.
RemarketingUserList userList = new RemarketingUserList();
userList.setName("Mars cruise customers #" + System.currentTimeMillis());
userList.setDescription("A list of mars cruise customers in the last year");
userList.setMembershipLifeSpan(365L);
userList.setConversionTypes(new UserListConversionType[] {conversionType});
// Create operations.
UserListOperation operation = new UserListOperation();
operation.setOperand(userList);
operation.setOperator(Operator.ADD);
UserListOperation[] operations = new UserListOperation[] {operation};
// Add user list.
userList = userListService.mutate(operations).getValue()[0];
2.Tie a remarketing list to an AdGroup
A new type of criteria object called CriterionUserList is now part of v201008. Through this type of criteria you are able to tie a UserList to an AdGroup. As with other types of
criteria, this type is also managed through the AdGroupCriterionService. The following code shows you how to create a CriterionUserList and tie it to an existing AdGroup.
// Create user list criterion.
CriterionUserList userListCriterion = new CriterionUserList();
userListCriterion.setUserListId(userListId);
// Create biddable ad group criterion.
BiddableAdGroupCriterion biddableCriterion = new BiddableAdGroupCriterion();
biddableCriterion.setAdGroupId(adGroupId);
biddableCriterion.setCriterion(userListCriterion);
// Create operation.
AdGroupCriterionOperation operation = new AdGroupCriterionOperation();
operation.setOperand(biddableCriterion);
operation.setOperator(Operator.ADD);
AdGroupCriterionOperation[] operations = new AdGroupCriterionOperation[] {operation};
// Add keywords.
AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(operations);
Thanks,
-Devraj
Those two examples from Google pretty much cover how it's set up
The linking is done behind the scenes by Google - they match the ID in YOUR remarketing tag (which is usually specific to one AdWords account) with your remarketing lists (which are again at account level).
You can have many remarketing lists with varying criteria (visited certain URLs on your site, converted, etc. & combinations thereof) and link one of those lists to your campaign or adgroup.
I'd maybe suggest doing this through the AdWords web user interface first time so you understand the process you are automating with the API
You can get all existing conversion trackers as below.
ConversionTracker theConversionTracker = null;
Selector conversionTrackerSelector = new Selector();
conversionTrackerSelector.fields = new string[] { "Id", "Name" };
ConversionTrackerPage conversionTrackerPage = new ConversionTrackerPage();
try
{
ConversionTrackerService conversionTrackerService = (ConversionTrackerService)adWordsUser.GetService(AdWordsService.v201502.ConversionTrackerService);
conversionTrackerPage = conversionTrackerService.get(conversionTrackerSelector);
if (conversionTrackerPage != null && conversionTrackerPage.entries != null && conversionTrackerPage.entries.Length > 0)
{
//iterate over conversionTrackerPage.entries and write down the Id of the convertion tracker you need
}
}
catch (Exception ex)
{
}
Then you can use the Id when creating the conversion type
UserListConversionType conversionType = new UserListConversionType();
conversionType.name = "My conversion type name";
conversionType.id = 19XXXXXXXXL; // The Id you wrote down.

How do I query complex data and return the data in the correct objects in Zend Framework 2?

I'm just starting out with ZF2 and I've run into a stumbling block and I cant find any useful advice on the internet.
Setting up retrieval of data from a single table and injecting it directly into a specific model is easy, for example, pulling data from a single row from 'school' table and injecting to a 'school' model.
However, I have some slightly more complex data and can't figure out how to return the data in the form of the correct model. For example, pulling multiple addresses from a school address table with a join on the school table.
I've got the following method in my AddressTable object...
public function fetchAllSchoolAddresses($school_id)
{
$stmt = $this->adapter->createStatement();
$stmt->prepare("CALL get_school_addresses(3)");
$stmt->getResource()->bindParam(3, $school_id, \PDO::PARAM_INT, 3);
$resultSet = $stmt->execute();
$addresses = new \ArrayObject();
if(!empty($resultSet)){
foreach ($resultSet as $result) {
$addresses->append($result);
}
}
return $addresses;
}
This quite nicely returns an array of addresses data but I want these results to be returned as Address objects. I'm not sure how to do this?
ZF2 comes with some standard Hydrators, which you can extend / modify if you wish.
http://framework.zend.com/manual/2.0/en/modules/zend.stdlib.hydrator.html
You could create a Hydrator for your School Object, and a Hydrator for your Address object.
The hydrators will build the object for you given the array data from the database for example
For example, you would Hydrate your School Object, and then find all addresses (like above) and use another hydrator to hydrate those. You would then add those to the School object to get your object graph as needed
$school->addAddress($address); // etc
Have a look here to see an example of using Hydrators and Hydrating ResultSets:
http://blog.evan.pro/zf2-tablegateway-hydration
http://framework.zend.com/manual/2.0/en/modules/zend.db.result-set.html
for exampple you could do something like this:
// How ever you want to get your database result do it here..
// this is where you get all addresses for your School
$stmt = $driver->createStatement($sql);
$stmt->prepare($parameters);
$result = $stmt->execute();
$resultSet = new HydratingResultSet(new ReflectionHydrator, new SchoolAddress);
$resultSet->initialize($result);
foreach ($resultSet as $address) {
$school->addAddress($address);
echo $address->getCity() . ' ' . $user->getPostcode() . PHP_EOL;
}
You would have a resultset (collection) of Addresses to add to your School.
That's code is just a very rough example hacked from the code in the docs to give an idea of what you do
How about this?
if(!empty($resultSet)){
foreach ($resultSet as $result) {
$address = new Address();
$address->exchangeArray($result);
$addresses->append($address);
}
}

Creating HtmlSelectOneMenu dynamically - assigning SelectItem list

I have a JSF application where I need to create almost all of the UIComponents dynamically based on certain parameters to the page. The components are created and added to an HtmlPanelGrid. I've been successfully creating HtmlLabel, HtmlInputText, UISelectBoolean, and HtmlCommandButton. Now I need to create an HtmlSelectOneMenu and add it, and I'm having a hard time finding examples that show how to attach the list of items to select.
The selection list is this, where I've made cfaItems a property of my backing bean:
SelectItem[] cfaItems = {
new SelectItem(1, "1"),
new SelectItem(2, "2"),
new SelectItem(3, "3"),
new SelectItem(4, "4"),
new SelectItem(5, "5")
};
The creation of the HtmlSelectOneMenu:
HtmlSelectOneMenu cfaMenu = (HtmlSelectOneMenu)
getApplication().createComponent(HtmlSelectOneMenu.COMPONENT_TYPE);
cfaMenu.setId("cfaMenu");
grid.getChildren().add(cfaMenu);
As best as I can figure it out, I need to create a ValueExpression that would bind the cfaItems list to the cfaMenu but not finding any examples is a problem. I think that I need to do something like this
String menuBinding =
"#{" + beanName + ".cfaItems}";
ValueExpression menuVE = getApplication().getExpressionFactory().
createValueExpression(FacesContext.getCurrentInstance().
getELContext(), menuBinding, String.class);
cfaMenu.setValueExpression("value", menuVE);
But I don't think that's correct. Any suggestions?
You need to create an UISelectItems instance with the given select item array as value and then add it as child of the menu, exactly as you'd do with <f:selectItems> in the view side.
UISelectItems selectItems = new UISelectItems();
selectItems.setValue(cfaItems);
cfaMenu.getChildren().add(selectItems);

Resources