I am trying to update a row in an entity using:
$linker = $this->getObjectManager()->getRepository('\Schema\Entity\Link')->find('link_id', 7853);
$linker->setSampleTitle($mytitle);
$linker->setSampleDesc($mydesc);
$this->getObjectManager()->merge($linker);
$this->getObjectManager()->flush();
But i get: An open transaction is required for this operation.
Try this:
$this->getObjectManager()->persist($linker);
$this->getObjectManager()->flush();
Also you might want to use findOneBy instead of find.
I just realized that inside the find() method i should not put 'link_id' but the class \Schema\Entity\Link. So getRepository() is not needed here...
Correct is: $linker= $this->getObjectManager()->find('\Schema\Entity\Link', 7853);
$Link = $this->getObjectManager->getRepository('Schema\Entity\Link')->find( 7853 );
and define link_id as id in Entity by #id or use findBy keyword
Related
I am trying to get unique value of a field with the code:
query.set("q","*:*" );
query.setGetFieldStatistics(true);
query.setGetFieldStatistics("popu_s");
QueryResponse rsp = solr.query(query);
FieldStatsInfo stats = rsp.getFieldStatsInfo().get("popu_s");
System.out.println(stats.getCount());
System.out.println(stats.getCountDistinct());
stats.getCount() gives the correct count. However, stats.getCountDistinct() always returns null.
Any idea?
getCountDistinct() in FieldStatsInfo is null because it was not provided when creating the FieldStatsInfo object (what was returned from solr.query(query)).
There is no guarantee that any of the fields in this pojo are populated.
As an alternative try using stats.getCardinality() although again there is no guarantee this is poulated.
Found the solution!
To make getCountDistinct() return a value, one needs to add this
query.addStatsFieldCalcDistinct("popu_s", true);
Normally,
$model->attributes=$_POST['Tblmodel'];
assigns all the attributes value from $_POST['Tblmodel'] to $model.
I want only to get one attributes value from $_POST['Tblmodel'].
For example:
I only want the value of 'id'. I was trying like this $_POST['Tblmodel']-> id and this doesn't work.
How can i get it?
Can you try this,
$_POST['Tblmodel']['id'];
instead of
$_POST['Tblmodel']-> id
use
$model->attributes=$_POST['Tblmodel'];
$model->id;
Or use
$_POST['Tblmodel']['id'];
How do you work with the old values of a record being updated?
For instance in the following code block how would I run a query using the previous winner_id field after I determine that it has indeed changed?
if self.winner_id_changed?
old_value = self.changed_attributes
User.find(old_value)
#do stuff with the old winner....
end
An example output of self.changed_attributes would be:
{"winner_id"=>6}
Do I really have to convert this to a string and parse out the value in order to perform a query on it? old_value[:winner_id] doesn't seem to do anything.
Use where instead of find, and the following inject method on changes to generate the desired hash:
if self.winner_id_changed?
old_value = self.changes.inject({}) { |h, (k,v)| h[k] = v.first }
old_user = User.where(old_value)
#do stuff with the old user....
end
You can also use ActiveRecord dirty methods such as:
self.winner_id_was
to get specific attribute's old value. Full documentation may be found here.
I'm working on getting a list of members based on the fields they selected on the extended profiles fields in buddypress. Here is my code:
<?php
$membership_group = "Orange Membership";
$db_query = "SELECT user_id FROM wp_bp_xprofile_data WHERE field_id = 33 AND value = \"" .$membership_group ."\"";
$match_ids = $wpdb->get_var($db_query);
$get_these_members = 'include=' .$match_ids;
if (bp_has_members($get_these_members, 'per_page optional=9')) {
//Some Codes here
}
?>
The result is returning just the first member it gets from the query instead of a list of members. Please say what I'm doing wrong.
Thanks
I think you should dive into the class BP_Core_User and its method get_users. It supports meta_key and meta_value.
You can also try to make just a search by field value. So pass an argument s to bp_has_members.
And per_page optional=9 is a wrong syntax.
This:
$wpdb->get_var($db_query);
returns a single var !
This is what you want:
$wpdb->get_col($db_query);
Then fix the syntax error mentioned by slaFFik
Is there a way to clear old selects in a .select("table.col1, ...") statement?
Background:
I have a scope that requests accessible items for a given user id (simplified)
scope :accessible, lambda { |user_id|
joins(:users).select("items.*")
.where("items_users.user_id = ?) OR items.created_by = ?", user_id, user_id)
}
Then for example in the index action i only need the item id and title, so i would do this:
#items = Item.accessible(#auth.id).select("polls.id, polls.title")
However, this will select the columns "items., items.id, items.title". I'd like to avoid removing the select from the scope, since then i'd have to add a select("items.") everywhere else. Am I right to assume that there is no way to do this, and i either live with fetching too many fields or have to use multiple scopes?
Fortunately you're wrong :D, you can use the #except method to remove some parts of the query made by the relation, so if you want to remove the SELECT part just do :
#items = Item.accessible(#auth.id).except(:select).select("polls.id, polls.title")
reselect (Rails 6+)
Rails 6 introduced a new method called reselect, which does exactly what you need, it replaces previously set select statement.
So now, your query can be written even shorter:
#items = Item.accessible(#auth.id).reselect("polls.id, polls.title")