Yii - loop an array from findAll - yii

I'm trying to implement for loop to update time.
$files = File::model()->findAll('type="A" AND id='.$id);
function files($files,$limit=null){
$limit = isset($limit) ? $limit : count($files);
for($i=0;$i<=$limit;$i++){
$files[$i]->date = date('Y:m:d H:i:s');
$files[$i]->update();
}
}
This function return error undefined offset 1.
What is the result of findAll method? Is it a multidimensional array of records? And How could I fixed this error?

You are setting $limit to count($files) but using $i<=$limit in your loop. Should be < instead.
for($i=0; $i<$limit; $i++){
Since there is a chance of an external $limit being applied you could use a foreach loop with a conditional break.
foreach($files as $a => $file) {
if($a == $limit) {
break;
}
$file->date = date('Y:m:d H:i:s');
$file->update();
}
Note that findAll() without a limit retrieves all the records from the databases for that table. Perhaps you should consider adding the limit through CDbCriteria

Related

Phalcon: specific column on joined PHQL

The below PHQL generates a complex resultset like it should:
$phql = "SELECT User.*, ProductUser.* "
. "FROM ProductUser "
. "INNER JOIN User "
. "WHERE ProductUser.product_id = 5";
Replacing ProductUser.* with an existing column like ProductUser.id causes an error:
MESSAGE: The index does not exist in the row
FILE: phalcon/mvc/model/row.zep
LINE: 67
This is version 2.0.6. Is this a bug or am I making a mistake somewhere? According to the documentation it should be fine.
It was my mistake (expecting the row to always be an object).
I hope this helps someone because looping complex resultsets is not in the documentation.
$result = $this->modelsManager->createQuery($phql)->execute();
if ($result instanceof \Phalcon\Mvc\Model\Resultset\Complex) {
foreach ($result as $rows) {
foreach ($rows as $row) {
if (is_object($row)) {
$modelData = $row->toArray());
// this is what I needed
} else {
$id = $row;
}
}
}
}
First of all you're missing the ON clause in your query.
Anyways, it's easier and more error prone to use Phalcon's query builder for querying:
<?php
// modelManager is avaiable in the default DI
$queryBuilder = $this->modelsManager->createBuilder();
$queryBuilder->from(["product" => 'Path\To\ProductUser']);
// Inner join params are: the model class path, the condition for the 'ON' clause, and optionally an alias for the table
$queryBuilder->innerJoin('Path\To\User', "product.user_id = user.id", "user");
$queryBuilder->columns([
"product.*",
"user.*"
]);
$queryBuilder->where("product.id = 5");
// You can use this line to debug what was generated
// $generatedQuery = $queryBuilder->getPhql();
// var_dump($generatedQuery);
// Finish the query building
$query = $queryBuilder->getQuery();
// Execute it and get the results
$result = $query->execute();
// Now use var_dump to see how the result was fetched
var_dump($result);
exit;

How can I find out what memcache returns result ? (Yii 1)

I use memcache, cofigurated it in config. All work fine, but i think data always returns from db, not from cache.
In CDBCommand 495 string:
if(($result = $cache->get($cacheKey)) !== false) {
var_dump('test'); die;
Yii::trace('Query result found in cache', 'system.db.CDbCommand');
return $result[0];
}
But $result always is FALSE.
This query I use:
$result = Yii::app()
->db
->cache(100)
->createCommand('SELECT COUNT(*) as count FROM wo_users')
->queryRow();
What is wrong?

Does Laravel Input::hasfile() work on input arrays?

I'm working on a Laravel project that uses a form with multiple file inputs. If I submit the form with the first input empty and all other inputs with a file, then hasFile returns false. It will only return true if the first input contains a file.
if(Input::hasfile('file'))
{
// do something
}
This is the input array via Input::file('file). The small image input is empty, but the large is not. I'd like it to look at the whole array and if there any files present, then proceed with the "do something".
Array
(
[small] =>
[large] => Symfony\Component\HttpFoundation\File\UploadedFile Object
(
[test:Symfony\Component\HttpFoundation\File\UploadedFile:private] =>
[originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image_name.jpg
[mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
[size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 44333
[error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
[pathName:SplFileInfo:private] => /Applications/MAMP/tmp/php/phpHILgX2
[fileName:SplFileInfo:private] => phpHILgX2
)
)
Is this expected behavior? Or, should it be looking at the entire array?
You can check by using the array key for example like below :-
HTML Input type File Element :
<input type="file" name="your_file_name[]" />
Laravel 5 : $request->hasFile('your_file_name.'.$key)
Laravel 4.2 : Input::hasFile('your_file_name.'.$key)
Taken from source:
/**
* Determine if the uploaded data contains a file.
*
* #param string $key
* #return bool
*/
public function hasFile($key)
{
if (is_array($file = $this->file($key))) $file = head($file);
return $file instanceof \SplFileInfo;
}
It seems that it only checks the first one from the array, head returns the first item from the array.
Since I can't comment, seems I'll have to post.
Ronak Shah's answer really should be marked the correct one here, and when I figured out why, it instantly had me saying "Sonnofa--" after 30-40 minutes trying to figure this... "mess" out.
Turns out to use hasFile() on an input array, you need to use dot notation.
So (using my own example) instead of
$request->hasFile("img[29][file]")
it needs to be
$request->hasFile("img.29.file")
That's certainly an eye-opener, given that PHP and dot notation don't really go together. Input arrays really are problem children.
here is a snippet that may help
if(Input::hasFile('myfile')){
$file = Input::file('myfile');
// multiple files submitted
if(is_array($file))
{
foreach($file as $part) {
$filename = $part->getClientOriginalName();
$part->move($destinationPath, $filename);
}
}
else //single file
{
$filename = $file->getClientOriginalName();
$uploadSuccess = Input::file('myfile')->move($destinationPath, $filename);
}
} else {
echo 'Error: no file submitted.';
}
Taken from
http://forumsarchive.laravel.io/viewtopic.php?id=13291
At the time of writing (Laravel 8) the Request class now supports arrays for the hasFile method, as from the source code:
/**
* Determine if the request contains the given file.
*
* #param string $name
* #param string|null $value
* #param string|null $filename
* #return bool
*/
public function hasFile($name, $value = null, $filename = null)
{
if (! $this->isMultipart()) {
return false;
}
return collect($this->data)->reject(function ($file) use ($name, $value, $filename) {
return $file['name'] != $name ||
($value && $file['contents'] != $value) ||
($filename && $file['filename'] != $filename);
})->count() > 0;
}

Multiple between statements in sql builder

I am creating a fairly large query using laravel 4 sql builder. it roughly looks like, get data from table
where(yada yada)and where age between(yada, yada)or between(yada, yada) and where(yad yada).
But what I want is this.
where(yada yada)and where (age between(yada, yada)or between(yada, yada)) and where(yad yada).
some code:
$query = Subject::grabBasic();// start writing query, which results to grab.
$agelength = sizeof($fromAgeValue);
$count = 0;
$ageflag = 0;
foreach( array_combine($fromAgeValue, $toAgeValue) as $from=>$to ){
$count+=1;
if($ageflag==0){
if(($from!=null)&&($to!=null)){
$query = Subject::addfromAgeToAge($query, $from, $to);
$ageflag=1;
}
}else if(($from!=null)&&($to!=null)){
$query = Subject::orfromAgeToAge($query, $from, $to);
}
}
... keep adding to $query
Subject code:
static function addfromAgeToAge($query, $fromAge, $toAge){
return $query->whereBetween('Age', array($fromAge, $toAge));
}
static function orfromAgeToAge($query, $fromAge, $toAge){
return $query->orWhereBetween('Age', array($fromAge, $toAge));
}
So, is there a way to write the addToAge builder function to take in the arrays of fromage and toage values and create a statement, which when added to the rest of the query, will give the desired results. e-g. the sql query will have parentheses in the correct place.
I would also like to do this without raw queries, I suspect there is a way to do this I just haven't been able to find it.
Thanks,
I buckled and used raw queries :(
Here's what it looks like.
$agecounter = 0;// tells me how many from age to age pairs were filed out coorrectly.
foreach( array_combine($fromAgeValue, $toAgeValue) as $from=>$to ){
if(($from!=null)&&($to!=null)){
$agecounter+=1;
}
}
$agelength = sizeof($fromAgeValue);
$ageflag = 0;
foreach( array_combine($fromAgeValue, $toAgeValue) as $from=>$to ){
if($ageflag==0){
if(($from!=null)&&($to!=null)){
$query = Subject::addfromAgeToAge($query, $from, $to, $agecounter); // ages put in for loop when multiple are added.
$ageflag=1; // add or's
$agecounter -=1;
}
}else if(($from!=null)&&($to!=null)){
$query = Subject::orfromAgeToAge($query, $from, $to, $agecounter);
$agecounter -=1;
}
}
and in my Model:
static function addfromAgeToAge($query, $from, $to, $count){
if($count>1){
return $query->whereRaw('( Age between ? and ?', array($from, $to));
}
return $query-> whereBetween('Age', array($from, $to));
}
static function orfromAgeToAge($query, $from, $to, $count){
if($count==1){
return $query->orWhereRaw(' Age between ? and ?)', array($from, $to));
}
return $query->orWhereRaw(' Age between ? and ?', array($from, $to));//('Age', array($fromAge, $toAge));
}
Hope this helps someone else put ()'s in a query builder.

Call to a member function num_rows() on a non-object [duplicate]

This question already has answers here:
Call to a member function on a non-object [duplicate]
(8 answers)
Closed 8 years ago.
I need to get the number of rows of a query (so I can paginate results).
As I'm learning codeigniter (and OO php) I wanted to try and chain a ->num_rows() to the query, but it doesn't work:
//this works:
$data['count'] = count($this->events->findEvents($data['date'], $data['keyword']));
//the following doesn't work and generates
// Fatal Error: Call to a member function num_rows() on a non-object
$data['count2'] = $this->events->findEvents($data['date'], $data['keyword'])->num_rows();
the model returns an array of objects, and I think this is the reason why I can't use a method on it.
function findEvents($date, $keyword, $limit = NULL, $offset = NULL)
{
$data = array();
$this->db->select('events.*, venues.*, events.venue AS venue_id');
$this->db->join('venues', 'events.venue = venues.id');
if ($date)
{
$this->db->where('date', $date);
}
if ($keyword)
{
$this->db->like('events.description', $keyword);
$this->db->or_like('venues.description', $keyword);
$this->db->or_like('band', $keyword);
$this->db->or_like('venues.venue', $keyword);
$this->db->or_like('genre', $keyword);
}
$this->db->order_by('date', 'DESC');
$this->db->order_by('events.priority', 'DESC');
$this->db->limit($limit, $offset); //for pagination purposes
$Q = $this->db->get('events');
if ($Q->num_rows() > 0)
{
foreach ($Q->result() as $row)
{
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
Is there anything that i can do to be able to use it? EG, instead of $data[] = $row; I should use another (OO) syntax?
You function findEvents is returning $data which you declared to be an array at the start. This is not an object and does not allow you to access functions using the member access syntax.
To count the number of values in an array see count
Also, from what I understand you not only want the query results returned as an array, but you want to be able to access methods on the result $Q = $this->db->get('events'); However this is not possible as this is a local variable and it is not being returned out. The function here has a result type of array and is not an object and thus has no access to anything, but the array. One solution to this is to return an associative array:
return array("results" => $data, "count" => $Q->num_rows());
Then use array syntax to access the count or the results. Another option is return a new object that has result and count fields and use accessor methods to get to those.