M
M
Messi2017-09-06 20:05:11
Apache HTTP Server
Messi, 2017-09-06 20:05:11

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

3 answer(s)
P
Pavel, 2019-05-18
@Us59

All outdated. install from ppa https://launchpad.net/~ondrej/+archive/ubuntu/apache2
Without ppa, installation via aptitude should help
sudo aptitude install apache2Or
sudo aptitude -f install apache2

D
Dim Boy, 2019-05-18
@twix007

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

F
Fetur, 2017-09-06
@Fetur

Well, it seems obvious, if the book has a zero id, then save, there is an id - update.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question