Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top