S
S
Sunnat Abdukhalilov2018-03-19 19:49:17
Yii
Sunnat Abdukhalilov, 2018-03-19 19:49:17

Yii2-images error when adding getPrimaryKey() what is the problem?

In general, I recently work in yii2. But there were no special problems before I installed yii2-images.
The problem is adding pictures, my project is an online store.
When added, this is what happens:

PHP Fatal Error – yii\base\ErrorException
Call to a member function getPrimaryKey() on a non-object

/* @var $img Image */
        $img->setMain(true);
        $img->urlAlias = $this->getAliasString() . '-' . $counter;
        $img->save();
 
 
        $images = $this->owner->getImages();
        foreach ($images as $allImg) {
 
            if ($allImg->getPrimaryKey() == $img->getPrimaryKey()) {
                continue;
            } else {
                $counter++;
            }
 
            $allImg->setMain(false);
            $allImg->urlAlias = $this->getAliasString() . '-' . $counter;
            $allImg->save();
        }

And pictures are not added. Only after reloading the page does this miracle happen.
tried 2 versions yii2.0.14.2 and yii2.0.7.
I've been suffering for two days now. please tell me what is the problem. or click on a normal extension with good functionality.
The project is almost completed, it remains to implement only the addition of pictures.
Help guys!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sunnat Abdukhalilov, 2018-03-23
@f_society

in general, I just commented out the check getPrimaryKey()
, it turned out like this:

public function setMainImage($img)
    {
        if ($this->owner->primaryKey != $img->itemId) {
            throw new \Exception('Image must belong to this model');
        }
        $counter = 1;
        /* @var $img Image */
        $img->setMain(true);
        $img->urlAlias = $this->getAliasString() . '-' . $counter;
        $img->save();


        $images = $this->owner->getImages();
        foreach ($images as $allImg) {

//            if ($allImg->getPrimaryKey() == $img->getPrimaryKey()) {
//                continue;
//            } else {
//                $counter++;
//            }

            continue;
            $allImg->setMain(false);
            $allImg->urlAlias = $this->getAliasString() . '-' . $counter;
            $allImg->save();
        }

        $this->owner->clearImagesCache();
    }

Pictures are added as expected
, but with the output of pictures, it's still a problem.
for now I will use findAll()
to pull pictures from the database

M
Maxim Timofeev, 2018-03-20
@webinar

$allImgdoes not contain an object. Check that you have var_dump ($images);apparently null and errors in php.ini are disabled, so you have not yet been overwhelmed with them. Always, always, even if you really don’t want to, under any circumstances, when development is in progress, php.ini should be error_reporting = E_ALL
In addition, what would be convenient - turn on the debug panel, which is in yii2, as a module out of the box:
www.yiiframework.com /doc-2.0/ext-debug-index.html
There are answers to 99% of the questions why it doesn't work.
Another option, replace
with
Since there can be a connection instead of an array of objects

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question