Hello I am trying to find out what method/way is used so that I can build an 'AND' query in my code because I suspect that the query I have been using right now uses the 'OR' query see the code below.
I think this is the building the 'OR' query:
ActiveSubject::find()->where(['clientid' => $clientid,
'subjectid' => $subjects['subjectid'],]);
I have searched around the internet and people have suggested using andWhere()
ActiveSubject::find()->select(['clientid', 'subjectid'])
->andWhere(['clientid' => $_user,])
->andWhere(['subjectid' => $subjects['subjectid'],]);
But still suspect it is still using 'OR' because the output is still getting everything rows with clientid even though there is no related subjectid. Can you help me finding out what the proper method of building a 'WHERE' query in yii 2?
EDIT 1:
I was doing this:
<?php if(ActiveSubject::find()->where(['clientid' => $_user,
'subjectid' => $subjects['subjectid'],]))://if true? ?>
<!--do this-->
<?php else: ?>
<!--do this-->
<?php endif; ?>
But alas it is not doing the else: when the query fails.
EDIT 2
My goal was the if function on my end, but I thought the reason it was not working was because of the query that it was not fulfilling the boolean but then it is actually returning an instance of ActiveQuery and I realized I was wrong. So it would make more sense if I make a function that returns a boolean in the ActiveSubject Model.
The first one uses AND and it's the same as the second one. If you are not sure about produced SQL check your debugger output or call something like:
echo ActiveSubject::find()
->where([
'clientid' => $clientid,
'subjectid' => $subjects['subjectid']
])
->createCommand()
->sql;
It seems that you would like to use AND query.
You can try to debug the query by printing it by using following code.
$query = ActiveSubject::find()->where(['clientid' => $clientid,
'subjectid' => $subjects['subjectid']]);
var_dump($query->prepare(Yii::$app->db->queryBuilder)->createCommand()->rawSql);
And also, you can try following code.
ActiveSubject::find()
->andWhere('clientid = :clientid', [':clientid' => $clientid])
->andWhere('subjectid = :subjectid', [':subjectid' => $subjects['subjectid']]);
Related
For sure it's an understanding issue on my part. I'm just trying to create a collection in which the elements can be output.
Example:
So I want to be able to execute:
$collection=collect([
'index1' => 'data1',
'index2' => 'data2',
'index3' => 'data3',
]);
$row=$collection->first();
dd($row->index1);
Or similar.. But I get the error
trying to get property of a non object.
It's an understanding issue about Laravel collections, I've read the Laravel documentation which goes from basic usage, to API reference. I cannot find the information on how to produce this basic static collection.
Can someone help?
$row=$collection->first(); points to the value data1, not an object/array.
To get the expected behavior try the code below, it wraps each in individual arrays.
$collection=collect([
['index1' => 'data1'],
['index2' => 'data2'],
['index3' => 'data3'],
]);
$row=$collection->first();
dd($row->index1);
As Laravel collections implement ArrayAccess you can simply access collection items as you do with an array
$value = $collection['index1'];
or you can use the get method on collections
$value = $collection->get('index1');
Thanks for your answers, I see the $collection['index1']; format works.
It's not exactly what I wanted, let me explain why. I know there's probably a better way of doing it although, for the sake of this particular coding requirement I'd like to know the answer to this.
I'm building a CRUD blade form.
On my blade I'll have 'field1' with initial output of $dbcollection->field1
Now of course if the database returns a null (create required) this output will fail. So what I'm trying to do here is pass blade a NULL-filled collection it understands so at least it doesn't complain about non instance of an object and avoiding #if statements on the blade form to account for differences in coding format.
I believe this is what you are looking for:
Controller:
$collection=collect([
'index1' => 'data1',
'index2' => 'data2',
'index3' => 'data3',
]);
$row = $collection->first();
view:
<input type="text" value="{{ ($row->index1 ? $row->index1 : '') }}" >
.
.
.
.
I've made an API with the Yii2 framework.
But I don't know how to use the OR condition in my statement.
For example:
I want to get all cars with brand BMW or DODGE.
I've tried the following:
$query = Car::getCar($lang)
->where(['or', ['m.brand' => 'BMW'], ['m.brand' => 'DODGE']])
->all();
But this doesn't work.
I only get it to work with one value for m.brand.
So:
$query = Car::getCar($lang)
->where(['m.brand' => 'BMW'])
->all();
Works just fine.
Tried to put in a few other ways, but I don't get this to work.
Does anyone know what I'm doing wrong?
EDIT
The getCar method returns something like:
(new Query())->select(['a.auto_id'])->from('auto_new a')
EDIT 2
Got it to work with:
$query->andWhere(['or', ['m.brand' => 'BMW'], ['m.brand' => 'DODGE']])
You can actually simplify it a lot by using an array with the values you need:
$query = Car::getCar($lang)
->where(['m.brand' => ['BMW', 'DODGE']])
->all();
This will execute with something like WHERE m.brand IN ('BMW', 'DODGE') which returns the result you are looking for.
If I understand you well, you could use something like this:
Model::find()
->orWhere(['brand' => 'brand1'])
->orWhere(['id' => 'brand2'])
->all();
where() can take an array to create sql along the lines of
SELECT * FROM car WHERE brand in ('brand1', 'brand2');
using this construct you can generate an array of brands you wish to return then use the following ActiveQuery.
$brands = ['BMW', 'DODGE'];
$query = Car::find()->where(['brand' => $brands])->all();
I have a very complex setup on my tables and achieving this via any of the find() methods is not an option for me, since I would need to fix relationships between my tables and I don't have the time right now, so I'm looking for a simple fix here.
All I want to achieve is run a query like this:
SELECT MAX( id ) as max FROM MyTable WHERE another_field_id = $another_field_id
Then, I need to assign that single id to a variable for later use.
The way I have it now it returns something like [{{max: 16}}], I'm aware I may be able to do some PHP on this result set to get the single value I need, but I was hoping there was already a way to do this on CakePHP.
Assuming you have a model for your table and your are using CakePHP 2.x, do:
$result = $this->MyTable->field('id', array('1=1'), 'id DESC');
This will return a single value.
see Model::field()
This example is directly from the CakePHP documentation. it seems you can use the find method of a model to get count
$total = $this->Article->find('count');
$pending = $this->Article->find('count', array(
'conditions' => array('Article.status' => 'pending')
));
$authors = $this->Article->User->find('count');
$publishedAuthors = $this->Article->find('count', array(
'fields' => 'DISTINCT Article.user_id',
'conditions' => array('Article.status !=' => 'pending')
));
iam building my web app, and i need to the simplest thing, just update fields inside table with some variables,but its not working he doesnt do anything ,values of the fields remain just as it was.
public function post_eupdate(){
$update = DB::table('app_events')
->where('ev_id', '=', Input::get("id"))
->update(
array(
'ev_op1' => Input::get('op1'),
'ev_op2' => Input::get('op2'),
'ev_coef1' => Input::get('coef1'),
'ev_coef2' => Input::get('coef2'),
'ev_host' => Input::get('host'),
'ev_stime' => Input::get('stime'),
'ev_ns1' => Input::get('ns1'),
'ev_cat' => Input::get('cat'),
'ev_tip' => Input::get('tip'),
'ev_ns2' => Input::get('ns2')
)
);
Your update statements seams good.
My guess would be that the problem is that Input::get("id") isn't a valid id for app_events.ev_id
Try debugging this by replacing ->update with ->get(). If this return null you would know that my assumption is correct. (If not post it in you question.)
As this is the case you have to modify the Input and not this piece of code which makes your question no longer fitting for the problem.
i use Active record for inserting values to DB.
All other type of queries i do with custom query as it's much easier but the active record insert is quite nice.
So i have this code :
$comment = array (
'point_id' => $object->id,
'title' => $object->title,
'comment' => $object->comment,
'author_name' => $object->author_name,
'is_temp' => 0,
'parent_id' => $object->parent_id
);
return $this->db->insert('comments', $comment);
Now i want to be able to set is_temp as a subquery result, which is :
(SELECT allow_user_comments from subjects where id='somevalue')
How would one achive that?
I was hoping to avoid using third party libraries.
Well, i doubt the fact that that's how you're supposed to do it, but ain't CI all about that?
This is how i got it to work (removing is_temp from the $comment array ofcourse):
$this->db->set($comment);
$this->db->set('is_temp',
'(SELECT allow_user_comments from subjects where id='.$subject_id.')',FALSE);
$this->db->insert('comments');
Feel free to use https://github.com/NTICompass/CodeIgniter-Subqueries. I have used it and it works! Hope it would be useful. :-)