Yii Undefined offset: 0 Using Query builder - yii

It give error with this code:
$checklist = Yii::app()->db->createCommand()
->select('indicators, conditions, values, description')
->from('indicator')
->where([':indicators' => $kl_indicators[$i]])
->limit(1)
->queryAll();
"PHP notice:
Undefined offset: 0"
what is wrong with that code? Help please.
Thanks a lot.

Your params are wrong. It should be:
->where('indicators=:in', [':in'=>$kl_indicators[$i]] )
or:
->where('indicators = "'.$kl_indicators[$i].'"')
See the official API for more details.

Finally found this answer:
$checklist = Yii::app()->db->createCommand()
->select('id,conditions,indicators, values, description') //
->from('indicator')
//->group('indicators')
->order('id ASC')
->where('indicators=:indicators', [':indicators'=>$kl_indicators[$i]])
->queryAll();
Thank you Samuel Liew, i need all rows so i use queryAll().

Related

Yii2 - hasMany with custom query

I am trying to create custom query in the hasMany function. I am not getting any error, but I am not getting any data from the table inside join statement. What's is wrong?
This is the function:
return $this->hasMany(UserKeys::classname(), ['user_id' => 'id'])
->select('licences.licenceName, userKeys.*')
->from('userKeys')
->innerJoin('licences', 'licences.id = userKeys.licence_id');
Try with following syntax. It may help you.
return $this->hasMany(UserKeys::classname(), ['user_id' => 'id'])
->select('licences.licenceName, userKeys.*')
->from(['userKeys' => UserKeys::classname()])
->viaTable('licences', ['licences.id = userKeys.licence_id']);

undefined method `size' for nil:NilClass

Why is my code returning this error?
#articles = Article.order("id DESC").where(:visible => 1)
if #aritcles.size > 15
#articles = Article.order("id DESC").where(:visible => 1).limit(15)
end
returns:
undefined method `size' for nil:NilClass
If i run
#articles = Article.order("id DESC").where(:visible => 1)
#articles.size
It returns an integer...
If that is from your real code, then you mispelled articles to aritcles.
If that is different, please provide the real code.
if #aritcles.size > 15 should be if #articles.size > 15.
But your code is odd, it is not necessary to do that.
Just do the below is enough.
#articles = Article.order("id DESC").where(:visible => 1).limit(15)
Replace #aritcles.size > 15By #articles.seze > 15

Rails 3 where with multiple params

I need to build a dynamic sql queue with 2 incoming params. It is easy when both params are defined.
MyClass.where(:column1 => param[:first], :column2 => params[:second])
but when for example param[:first] = 0 I want to select all(not null) fields for this column(so when both params are = 0 it would be equal to select * from tablename). Tried this syntax:
MyClass.where(:column1 => param[:first], :column2 => !nil)
but it gives me wrong output. Any suggestions how to elegantly resolve this?
You could use the ?: operator inside where:
MyClass.where(params[:first] ? {:column1 => params[:first]} : "column1 IS NOT NULL")
.where(params[:second] ? {:column2 => params[:second]} : "column2 IS NOT NULL")
What about MyClass.where('column1 IS NOT NULL AND column2 IS NOT NULL').where({:column1 => params[:first], :column2 => params[:second]}.delete_if{|k,v| v.nil?})?
I think this may work from what I've found. It appears to work in the rails console and considering active record alows active chaining:
MyClass.where(:column1 => params[:first], :column2 => params[:second]).where("column1 is NOT NULL").where("column2 is NOT NULL")

complex ActiveRecord to MongoID query

could you please help me to convert active record query to mongoid?
where(["access_grants.access_token = ?
AND (access_grants.access_token_expires_at IS NULL
OR access_grants.access_token_expires_at > ?)",
conditions[token_authentication_key], Time.now]).joins(:access_grants).
select("users.*").first
thank all, but i've got recipe how to solve this issue - http://groups.google.com/group/mongoid/browse_thread/thread/7d55c5687479355e
user_id = AccessGrant.where(:access_token => conditions[:token_authentication_key]).any_of({ :access_token_expires_at => nil }, { :access_token_expires_at.gt => Time.now).first.user_id
user = User.find(user_id)

What's wrong with Query?

So locally I have
#fooentries = Entry.where(:status => 'foo').where("created_at >= #{Date.today}")
Which runs fine, but when I deploy to Heroku it seems to be breaking it.
I asked a friend and he told me to do the following but this fails locally:
#fooentries = Entry.where(:status => 'foo').where("created_at >= #{Time.zone.now.beginning_of_day.to_s(:db)}")
Anyone?
EDIT: Doesn't matter, fixed it. This is what the query needs to be-
#fooentries = Entry.where(:status => 'foo').where('entries.created_at >= ?', Time.zone.now.beginning_of_day)
Would you try to escape the query, maybe it depends on the format your query is built
try
#fooentries = Entry.where(:status => 'foo').where("created_at >= ?", Date.today)
As per the edit above, this is what I needed -
#fooentries = Entry.where(:status => 'foo').where('entries.created_at >= ?', Time.zone.now.beginning_of_day)