Completed
Push — master ( 4099fa...f02648 )
by Nic
12s queued 11s
created

ManyImageRelationTask::yieldProductImages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Dynamic\Products\Task;
4
5
use SilverStripe\Dev\BuildTask;
6
use SilverStripe\ORM\DB;
7
8
/**
9
 * Class ManyImageRelationTask
10
 * @package Dynamic\Products\Task
11
 */
12
class ManyImageRelationTask extends BuildTask
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $title = 'Product Image Relation Update Task';
18
19
    /**
20
     * @var string
21
     */
22
    private static $segment = 'product-image-relation-update-task';
0 ignored issues
show
introduced by
The private property $segment is not used, and could be removed.
Loading history...
23
24
    /**
25
     * @param \SilverStripe\Control\HTTPRequest $request
26
     */
27
    public function run($request)
28
    {
29
        $this->updateRelationTable();
30
    }
31
32
    /**
33
     *
34
     */
35
    protected function updateRelationTable()
36
    {
37
        foreach ($this->yieldProductImages() as $record) {
38
            DB::prepared_query(
39
                'UPDATE `Product_Images` SET `FileID` = ? WHERE `ID` = ?',
40
                [$record['ImageID'], $record['ID']]
41
            );
42
        }
43
    }
44
45
    /**
46
     * @return \Generator
47
     */
48
    protected function yieldProductImages()
49
    {
50
        foreach (DB::query('SELECT * FROM `Product_Images`') as $record) {
51
            yield $record;
52
        }
53
    }
54
}
55