Answer the question
In order to leave comments, you need to log in
Save data array in Yii2?
There is a model Book (AciveRecord), in this model there is a relationship hasMany(books_id => id) with the table BookFiles(AciveRecord).
There is one form that adds a new book to the Book table and files to the BookFiles table at the same time.
Tell me, please, how can I implement saving a book and an array of files into tables at the same time as checking if such a file already exists, then update.
Now I'm trying to do this, but it does not save:
class BookForm extends Model
{
public $title;
public $book;
private $model;
private $bookFiles;
/**
* @param Book $model
*/
public function setModel(Book $model)
{
$this->model = $model;
$this->title = $model->title;
$this->bookFiles = $model->getBookFiles()->All() ?: new BookFile();
}
public function save()
{
if($this->validate()) {
$this->model->title = $this->title;
if (is_array($this->book)) {
$this->bookFiles->name = $this->book['name'];
$this->bookFiles->path = $this->book['path'];
$this->bookFiles->base_url = $this->book['base_url'];
} else {
throw new NotFoundHttpException();
}
if($this->model->save(false) && $this->bookFiles->save(false)) {
return true;
}
}
return false;
}
}
Answer the question
In order to leave comments, you need to log in
All outdated. install from ppa https://launchpad.net/~ondrej/+archive/ubuntu/apache2
Without ppa, installation via aptitude should help sudo aptitude install apache2
Orsudo aptitude -f install apache2
I solved the problem like this:
sudo apt-get purge ^apache*
sudo apt-get autoremove
sudo apt-get clean
sudo rm -rf /etc/apache2
sudo apt-get update
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question