Answer the question
In order to leave comments, you need to log in
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();
}
Answer the question
In order to leave comments, you need to log in
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();
}
findAll()
$allImg
does 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 questionAsk a Question
731 491 924 answers to any question