Greetings to all!
Can someone explain me how to use Doctrine\DBAL\Types\Type
?
In migration file I created column that have type array
$table->addColumn('filePrev', Type::TARRAY, ['notnull' => false,]);
In DB I succesfuly have column filePrev
with comment (DC2Type:array)
Then, I spent a lot of time to figureout how to save $files = ['file_path1','file_path_2'];
in that column and solution was:
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
use Doctrine\DBAL\Types\Type;
class Task extends Entity implements JsonSerializable {
protected $uid;
protected $title;
protected $description;
protected $filePrev;
protected $publishDate;
public function __construct() {
$this->addType('fileWork', Type::TARRAY);
}
public function jsonSerialize() {
return [
'id' => $this->id,
'uid' => $this->uid,
'title' => $this->title,
'description' => $this->description,
'filePrev' => $this->filePrev,
'create_date' => $this->createDate,
];
}
}
Finaly, half of all magic happened. Column fileWork
have value a:2:{i:0;s:10:"file_path1";i:1;s:11:"file_path_2";}
Next I’m stuck (×_×)
When I making a query:
public function findAll() {
$qb = $this->db->getQueryBuilder();
$qb->select('*')->from($this->getTableName());
return $this->findEntities($qb);
}
I’m geting serialized array:
Object
(
[uid:protected] => oleg
[title:protected] => Some title
[description:protected] =>
[filePrev:protected] => Array
(
[0] => a:2:{i:0;s:10:"file_path1";i:1;s:11:"file_path_2";}
)
[createDate:protected] => 2020-03-08 14:44:13
)
Question of life and deth what I shoud do to get filePrev
unserialized like:
Object
(
[uid:protected] => oleg
[title:protected] => Some title
[description:protected] =>
[filePrev:protected] => Array
(
[0] => "file_path1",
[1] => "file_path2",
)
[createDate:protected] => 2020-03-08 14:44:13
)