Elastica search add a second filter - symfony

I'm starting using Elastica with my Symfony app but I'm stuck to add a filter.
The following code works well, it search by name, slug, agglomeration and then add a geoloc filter. I want to add a "specialty" filter to remove all results which do not correspond to that filter/ have that specialty but I've no idea how to do that.
$nameQuery = new \Elastica_Query_Text();
$nameQuery->setFieldQuery('name', $name);
$slugQuery = new \Elastica_Query_Text();
$slugQuery->setFieldQuery('slug', $name);
$agglomerationQuery = new \Elastica_Query_Text();
$agglomerationQuery->setFieldQuery('agglomeration', $agglomeration);
$boolQuery = new \Elastica_Query_Bool();
$boolQuery->addShould($nameQuery);
$boolQuery->addShould($slugQuery);
$boolQuery->addShould($agglomerationQuery);
//todo add filter by speciality
if($latitude != null) {
$geoFilter = new \Elastica_Filter_GeoDistance('location', $latitude, $longitude, '3km');
$boolQuery = new \Elastica_Query_Filtered($boolQuery, $geoFilter);
}
return $this->find($boolQuery);

Related

Symfony Insert into sub table

As i am new to symfony framework, for my client symfony website doing the maintenance. i have tried to insert a record into a new table, which was actually a sub table of a already existing table.
But i am not able to write the insert query in this. which shows the error only.
Please check and provide the solutions for the below code,
public function executeUpdatednc(sfWebRequest $request)
{
$patient_id = $_REQUEST['pid'];
$q = Doctrine_Query::create()
->update('patient')
->set('isadmindnc', '?', 1)
->where('id = ?', $patient_id)
->execute();
$datetime = date("Y-m-d H:i:s");
##### Here i need to write the sub query to insert into the new table, Please suggest #####
//$rsm = new ResultSetMapping();
//$query = $this->_em->createNativeQuery('INSERT INTO patient_phone SET ph_pid = ?', $patient_id);
//$query->setParameter(1, $items);
//$result = $query->getResult();
}
For me, simple way to resolve this problem (for update):
$patient = Doctrine_Core::getTable('patient')->findOneById($patient_id);
$patient->setIsadmindnc(1);
$patient->save();
For insert:
$patient = new Patient();
$patient->setIsadmindnc(1);
$patient->save();

Drupal - 2 seperate feeds updating the same set of nodes (feeds module)

I'm using the feeds module to pull in a set of publications into a Drupal content type. They are set to run at regular intervals using cron. I have two separate feeds, which should work as follows:
Feed 1 (pure_feed) - pulls in the bulk of the fields
Feed 2 (harvard_format) - accesses a separate url source and updates one field on the content type.
The problem I have is that feed 2 always creates a new set of nodes rather than updating the existing nodes (that were created using feed 1). I have used the debug options at /import and can see that the GUIDs for feed 2 match the GUIDs for feed 1, but it still creates 2 sets of nodes rather than updating the 1 set of nodes.
Here is an excerpt from the feeds_items database table:
As you can see they both have the same GUID but they are mapped to separate nodes. Is there any way to have the second feed map to the same nodes as the first feed?
I knocked something together that allows my second feed to update the nodes from my first feed. Not sure if this is the right way of doing things but it works. Here's what I did in case it helps someone else in future:
Created a custom processor that extends FeedsNodeProcessor
Copied across all of FeedsNodeProcessor's functions to the new class.
Overrided the existingEntityId function as follows (harvard_format is my secondary feed and pure_feed is my primary feed):
protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) {
if($source->id == 'harvard_format') {
$query = db_select('feeds_item')
->fields('feeds_item', array('entity_id'))
->condition('feed_nid', $source->feed_nid)
->condition('entity_type', $this->entityType())
->condition('id', 'pure_feed');
// Iterate through all unique targets and test whether they do already
// exist in the database.
foreach ($this->uniqueTargets($source, $result) as $target => $value) {
switch ($target) {
case 'url':
$entity_id = $query->condition('url', $value)->execute()->fetchField();
break;
case 'guid':
$entity_id = $query->condition('guid', $value)->execute()->fetchField();
break;
}
if (isset($entity_id)) {
// Return with the content id found.
return $entity_id;
}
}
return 0;
}
elseif ($nid = parent::existingEntityId($source, $result)) {
return $nid;
} else {
// Iterate through all unique targets and test whether they do already
// exist in the database.
foreach ($this->uniqueTargets($source, $result) as $target => $value) {
switch ($target) {
case 'nid':
$nid = db_query("SELECT nid FROM {node} WHERE nid = :nid", array(':nid' => $value))->fetchField();
break;
case 'title':
$nid = db_query("SELECT nid FROM {node} WHERE title = :title AND type = :type", array(':title' => $value, ':type' => $this->config['content_type']))->fetchField();
break;
case 'feeds_source':
if ($id = feeds_get_importer_id($this->config['content_type'])) {
$nid = db_query("SELECT fs.feed_nid FROM {node} n JOIN {feeds_source} fs ON n.nid = fs.feed_nid WHERE fs.id = :id AND fs.source = :source", array(':id' => $id, ':source' => $value))->fetchField();
}
break;
}
if ($nid) {
// Return with the first nid found.
return $nid;
}
}
return 0;
}
}
Copied across the the Process and newItemInfo functions from FeedsProcessor.
Overrided newItemInfo as follows:
protected function newItemInfo($entity, $feed_nid, $hash = '') {
$entity->feeds_item = new stdClass();
$entity->feeds_item->entity_id = 0;
$entity->feeds_item->entity_type = $this->entityType();
// Specify the feed id, otherwise pure_feed's entries in the feeds_item table will be changed to harvard_format
$entity->feeds_item->id = ($this->id == "harvard_format") ? "pure_feed" : $this->id;
$entity->feeds_item->feed_nid = $feed_nid;
$entity->feeds_item->imported = REQUEST_TIME;
$entity->feeds_item->hash = $hash;
$entity->feeds_item->url = '';
$entity->feeds_item->guid = '';
}

Symfony 1.4 embedRelation where clause

Is it possible to set a where clause on embedRelation?
$this->embedRelation('treatedStones');
I need to get the treatedStones where stone_free = 0
I think you can't do this with embedRelation, but you can do manually as it's done in sfFormDoctrine.
$subForm = new sfForm();
// create a custom query e.g. in `TreatedStoneTable::queryFree($relatedId)` and
// a method to the class of `$this->getObject()` to retrieve free stones
// e.g. `Class::getFreeTreatedStones()` to call the query
foreach ($this->getObject()->getFreeTreatedStones() as $index => $childObject)
{
$form = new TreatedStoneForm($childObject);
$subForm->embedForm($index, $form);
$subForm->getWidgetSchema()->setLabel($index, (string) $childObject);
}
$this->embedForm('treated_stones', $subForm);
You can find further details about how forms work in the docs.

How to change the sorting of a view using hook_views_pre_view()?

I've got the following:
function view_sorter_views_pre_view(&$view) { // don't need $items
if ($view->name == 'MOST_RECENT') {
$insert = array();
$insert[order] = 'DESC'; //SORT ORDER
$insert[id] = 'title';
$insert[table] = 'node';
$insert[field] = 'title';
$insert[override] = array();
$insert[override][button] = 'Override';
$insert[relationship] = 'none';
unset ($view->display['default']->display_options['sorts']['title']);
$view->display['default']->display_options['sorts']['title'] = $insert;
}
}
Basically, I'm just changing the sort order... but this does not appear on the view when opening it. Any idea why?
I believe what you want is
/**
* Implementation of hook_views_pre_view().
*/
function view_sorter_views_pre_view(&$view) {
if ($view->name == 'MOST_RECENT') {
$view->display['default']->handler->options['sorts']['title']['order'] = 'DESC';
}
}
Views uses the handler object to build the query instead of the display_options. The display_options contain all the options for every display type that the view contains (eg. default, page_1, block_1, etc...). The 'handler' object holds the options that will be used to actually build the current display.
Note: I simplified the code to only change the sort order. The rest of your code should work, just change the last two lines to
unset($view->display['default']->handler->options['sorts']['title']);
$view->display['default']->handler->options['sorts']['title'] = $insert;

batch create users?

I have an excel list of 600 users with name, email, and role - that I need to add to the drupal site I'm building.
There are 2 roles distributed among the users.
As an added complication, the site is using the Content Profile module, so it would be a great help if for each new user account created, a corresponding profile node was also auto-created.
Any ideas how to batch-create the new users?
How about the user_import module?
I had the same thing, and created a module for this.
Basically, it reads the user and what role to get from a file; in my case it was a CSV file with emailadres, name, role and stuff needed for the content profile.
Let's say you want user x#mail.com and fill out automatically his content profile data Name, Sirname and City or something.
In your module:
read the line from the file
create a new user
create a new node, (stdClass object, give it the correct type ('profile_data' or whatever your content profile type is) and fill the rest of yout node and save.
A sample:
<?php
//create a form with a button to read the CSV file
function bulk_users_submit() {
$users = 0;
$handle = fopen(drupal_get_path('module', 'bulk_users') .'/'.DATAFILE, "r");
if (!$handle) {
return $users;
}
while (($data = fgetcsv($handle)) !== FALSE) {
//this is similar to what the users.module does
if (bulk_users_create_user($data)) {
$users++;
bulk_users_create_profile($data);
}
}
fclose($handle);
return $users;
}
function bulk_users_create_profile($user, $data) {
$node = new stdClass();
$node->title = t('First and Last Name');
$node->body = "";
$node->type = 'first_and_last_name';
$node->created = time();
$node->changed = $node->created;
$node->status = 1;
$node->promote = 0;
$node->sticky = 0;
$node->format = 0;
$node->uid = $data['uid'];
$node->language = 'en';
$node->field_firstname[0]['value'] = $data['firstname'];
$node->field_lastname[0]['value'] = $data['lastname'];
node_save($node);
}
?>
not tested, but the idea is clear i hope.

Resources