Call to undefined method stdClass::count() in drupal 7 - drupal

I have an issue with a count function in drupal 7.
Code is :
$sql = "select count(Status) from TB_Aanmeldingen where (Status= 'Ja' or Status='Ja, met kleine') and ID_Wedstrijd = :match";
$args = array(':match' => $match);
$row = db_query($sql, $args)->fetchObject();
$aantal = $row->count(Status);
Error message:
Call to undefined method stdClass::count()
Any help is very much appreciated!

I'm not sure what you're trying to do with:
$aantal = $row->count(Status);
Do a print_r or var_dump of the $row object, and you'll see what the structure is. db_query returns a DatabaseStatementInterface, and you're getting an object from it. But the object doesn't have a function you're trying to call.
Instead, debug, print out, and debug the value of $row, and get the value you're looking for.
Also, look into db_select. It's better to use that then db_query.
https://api.drupal.org/api/drupal/includes!database!database.inc/function/db_select/7

Related

Woocommerce Add to cart function showing error

I am just using the below code. It was working fine but from today is shows error - "TypeError thrown wc_add_number_precision(): Argument #1 ($value) must be of type float, null given, called in /chroot/home/a23cc473/fdd76f6eaa.nxcli.net/html/wp-content/plugins/woocommerce/includes/wc-core-functions.php on line 1955". Here is my code - WC()->cart->add_to_cart( 5 )
Can anyone please advise me why this error is showing
It's related with newest WC core, go and edit wc->includes->wc-core-functions.php row 1918 and replace it with older function:
new (not working) :
function wc_add_number_precision( float $value, bool $round = true ) {
old (working one) :
function wc_add_number_precision( $value, $round = true ) {
It's not best fix, but at least is working.

where clause inside when condition in query builder, saying undefined variable

I have a strange problem, I am making a conditional query with values stored in different variables and passing them to where clause. That where clause simply not accepting values and keep on saying undefined variable in very first condition. To understand it I just cut it to one condition and make it simple to get easy answers.
Error: ErrorException: Undefined variable: col_name in file E:\xampp\htdocs\coder\app\Http\Controllers\a26_gets\a26_gets_ctrl.php on line 97
Code:
$this->tableName = 'mytable';
$col_name = 'first_name';
$value = 'danny';
$tableData = DB::table($this->tableName)
->when($value, function ($query, $value) {
return $query->where($col_name, $value);
})->paginate(1);
print_r($tableData);
Try adding the $col_name to the scope of the conditional query...
$this->tableName = 'mytable';
$col_name = 'first_name';
$value = 'danny';
$tableData = DB::table($this->tableName)
->when($value, function ($query, $value) use ($col_name) {
return $query->where($col_name, $value);
})->paginate(1);
print_r($tableData);

I have successfully enlisted a user in cohort via core_cohort_add_cohort_members but getting a warning

I have successfuly enlisted a user in moodle cohort using core_cohort_add_cohort_members but i am getting a warning in response.Can anybody help me remoce this warning.
Here is the warning i get.
{"warnings":[]}
Here is my code
$fname = 'core_cohort_add_cohort_members';
/// Paramètres
$member = new stdClass();
$member->cohorttype[type]='id';
$member->cohorttype[value]=2;
$member->usertype[type]='id';
$member->usertype[value]=8;
$members = array($member);
$par = array('members' => $members);
$rest_format = 'json';
$Serve_Url = 'localhost/moodle' . '/webservice/rest/server.php'. '?wstoken=' . '72f102312fff135cbb4a301d9c7839ae' .'&wsfunction='. $fname;
require_once('curl.inc');
$Rest_format = ($rest_format == 'json') ? '&moodlewsrestformat=' . $rest_format : '';
$curl = new curl;
//if rest format == 'xml', then we do not add the param for backward compatibility with Moodle < 2.2
$rep = $curl->post($Serve_Url.$Rest_format, $par);
dpm($rep);
The warnings array is empty in your example reply.
If you want to detect this more verbosely, you can always just json_decode() the reply. You should see an empty array there.
So I think it's working just as you wanted.
Turn the keys into strings i.e
$member->cohorttype["type"]='id';
$member->cohorttype["value"]=2;
$member->usertype["type"]='id';
$member->usertype["value"]=8;
Mainly because newer versions of PHP may cause real exceptions because of
unidentified variables(type and value)
That aside as Sebastian Berm stated empty warnings array means no problem

Call to undefined method Slice in Doctrine

I have this function,
public function getWall(){
$q = $this->createQueryBuilder('f');
$q->leftJoin("f.profilo", 'p');
$q->leftJoin("p.utente", 'u');
$q->where('(f.foto_eliminata IS NULL OR f.foto_eliminata != 1)');
$q->andWhere('p.fase_registrazione = :fase');
$q->andWhere('u.locked = :false');
$q->slice(0, 20);
$q->setParameter(':fase', 100);
$q->setParameter('false', false);
$q->orderBy('f.created_at', 'desc');
$dql = $q->getQuery();
$results = $dql->execute();
return $results;
}
but I get this error,
Call to undefined method Doctrine\ORM\QueryBuilder::slice()
Ok, so, u get this error, cause QueryBuilder has no such method. But Collection has. If u want to use slice, a possible variant is:
use Doctrine\Common\Collections;
public function getWall(){
$result = $this->createQueryBuilder('f')
->leftJoin("f.profilo", 'p')
->leftJoin("p.utente", 'u')
->where('(f.foto_eliminata IS NULL OR f.foto_eliminata != 1)')
->andWhere('p.fase_registrazione = :fase')
->andWhere('u.locked = :false')
->setParameter('fase', 100)
->setParameter('false', false)
->orderBy('f.created_at', 'desc')
->getQuery()
->getResult();
// $result typed as array
return new Collections\ArrayCollection($result))->slice(0,20); // convert array to collection, then slice
}
By the way, it is not a good idea to 'limit' result of the query in a such way.
U can use setMaxResults(20), and not to select all objects at all.
About lazy collections (http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/extra-lazy-associations.html): after selecting result objects, u can take some object from result collection: $r = $result[0] after that:
$portfilos = $r->getPortfolio(); // returns for example Collection with some objects;
// its Lazy, without SQL query!
$portfolios->slice(0, 20); // queries first 20 potfolios
To use slice is a rather good idea, if u have lots of objects in some relation.
p.s. sry, mb I didn't recognized your problem, but tried :)
EDITED
fixed errors in code.

Drupal filter is not working properly

I'm not sure how to ask it, so if you need anymore additional information, please ask for it!
Situation
I've got a website in three languages. I got a lot of customer cases online each connected to a sector (depending in which sector they belong). Each sector and reference has it's own unique nid.
In my template.php it's stated like this:
if ('sector' == $vars['node']->type) {
$lang = '/'.$vars['language'].'/';
$key_path = $_SERVER['REQUEST_URI'];
$key_path = substr_count($key_path, $lang) ? substr($key_path, strlen($lang)) : $key_path;
if (strpos($key_path, '?')) $key_path = substr_replace($key_path, '', strpos($key_path, '?'));
if (strpos($key_path, 'sectors-references') === 0) {
$view = views_get_view('references');
if (!empty($view)) {
$view->set_arguments((int)$vars['node']->nid);
$vars['content']['suffix'] = $view->render();
}
}
}
And yet, every sector shows me the same references... What do I have to change to get the correct reference under the right sector?
Usually arguments are passed to set_arguments using an array, if you pass a non-array the argument will probably be ignored which is why you're always getting the same result. Try:
$view->set_arguments(array((int)$vars['node']->nid));

Resources