Multiple DB update query using Laravel 5 - mysqli

I'm new in Laravel and I'm trying to update mysql table's multiple rows using one query. I need to update for example 100 rows like this:
$q = \DB::table('exmaple')
->where('exampleID', $array)
->update(array('Viewed' => 1));
$array is array with ID-s.
I have try also to implode array to string, then execute query, but with no result. Can anyone help?

Does this work for you:
$q = \DB::table('example')
->whereIn('exampleID', $array)
->update(array('Viewed' => 1));
whereIn() accepts an array as a second parameter so all IDs in this array will be affected by the query.

Related

get most actual row with zend's fetchRow

I'm very new to zend (1.12)..so please excuse my very basic question:
I want to fetch only one row from a database. Thatfore I want to use the fetchRow(..) function like this
$row = $db->fetchRow($db->select()->where("col1 = '".val1."' AND col2='".val2."'"));
The problem is, that there may be many rows that fit to the where-clause and I only want to get the one with the highest id. How can I do this?
The fetchRow() method returs only one row. If you want to choose row with the highest ID meeting other conditions, invoke it like this:
$select = $db->select()
->where('col1 = ?', $val1)
->where('col2 = ?', $val2)
->order('id DESC');
$row = $db->fetchRow($select);
Also, remember to pass values to SQL query in the way as in above code (to avoid SQL injection attack risk).

Laravel 3 Eloquent How to select column as

I'm trying to figure out how to give a column an alias using Eloquent.
So, in other words, how do I execute the following mysql query using Eloquent?
SELECT occupation AS test FROM users WHERE occupation = 'PIMP';
Thx in adv!
Eloquent returns a regular Fluent query. So you can try something like this (assuming your model name is 'User'):
$user = User::where_occupation('pimp')->get(array('occupation as test'));
This is how I have been able to do this in Laravel 5 using select() and passing the col name and the alias to that method (here I'm also using groupby() to restrict it to "DISTINCT" return values then using toarray() to return any array instead of a Collection Object:
$results = asset::select('model_code__c AS option')->whereRAW("model_code__c <> '' AND status = 'A'")->groupby('model_code__c')->get()->toarray();

MongoDB try to access from one table to another table

I am getting the DocumentID from Table1.
so I want to look into each table1 entries and go to the table2 and get the corresponding values.
$cursor = $table1->find();
foreach ($cursor as $obj) {
$ID1= $obj['DocumentID'];
$criteria = array('DocumentID'=>$ID1);
$fields = array('DocumentID', 'DocumentDetails');
$cursor1 = $Documentid2details->find($criteria, $fields);
echo $cursor1->count() . ' document(s) found. <br/>';
foreach ($cursor1 as $obj1) {
echo 'the feed details are ' . $obj1['DocumentIDDetails'] . '<br/>';
echo '<br/>';
}
===
I have one table in which I have DocumentID and DocumentDetails.
another table with DocumentID and name.
I want to get the DocumentID from one table and keep that in the other table to get the document details.
I am having issue with the getting value from other table.
can you please let me know what I am missing
You cannot get data from 2 collections using 1 query with MongoDB. The premise is to denormalize, and if you cannot, perform the 2nd query and aggregate (join) the results in your programming layer.
Options:
denormalize
multiple queries
using embedded documents

how do you insert multiple rows in a loop using doctrine 2

i want to insert multiple rows in a loop using doctrine 2..
i usually insert 1 record using this:
$Entity->setData($posted);
$this->_doctrine->persist($Entity);
$this->_doctrine->flush();
Simply persist all your objects and then call flush() after the loop.
$entityDataArray = array(); // let's assume this is an array containing data for each entity
foreach ($entityDataArray AS $entityData) {
$entity = new \Entity();
$entity->setData($entityData);
$this->_doctrine->persist($entity);
}
$this->_doctrine->flush();
If you're inserting a large number of objects you will want to batch insert (see http://www.doctrine-project.org/docs/orm/2.0/en/reference/batch-processing.html)
Inside your loop, you should be able to simply:
$entity1->setData($data1);
$this->_doctrine->persist($entity1);
$entity2->setData($data2);
$this->_doctrine->persist($entity2);
$this->_doctrine->flush();

Zend Framework - applying order by on a nested query

This might be a very simple thing. Check out the normal sql query below
(select * from shopping order by shopping_id desc limit 5) order by RAND()
This query runs successfully in mysql - not sure if this is the right way of doing it - but it works. It gets the last 5 ids from shopping table and randomly orders them everytime
I want to achieve this in Zend. I'm not sure how to execute the first part and then apply the RAND clause to the results - what I have below does not do that.
$select = $this->select()
->from(array('sh'=>'shopping'))
->order('shopping_id desc')
->limit(5)
->order('RAND()');
Why not take a slightly different approach which will acheive the same results. If you drop the subselect and the order by RAND() you can get the rows very quickly from the database, then when you are working with the rows, you could always randomize them.
$select = $this->select()
->from(array('sh'=>'shopping'))
->order('shopping_id desc')
->limit(5)
$rows = $this->fetchAll($select);
// take it from a rowset object, convert to an array:
$rowArray = array();
foreach ($rows as $row) $rowArray[] = $row;
shuffle($rowArray);
The Zend_Db_Expr class lets you do that. You create a new instance of the Zend_Db_Expr class and using its constructor you pass in the expression as a string: "RANDOM()".
$select = $this->select()
->from(array('sh'=>'shopping'))
->order('shopping_id desc')
->limit(5)
->order(new Zend_Db_Expr('RANDOM()'));