0

I have problem with my code. I have an entity:

    namespace CP\API\Entity;

    use CP\Model\Configuration;
    use CP\Model\Content;
    use CP\Model\Language;
    use CP\Model\MenuTranslation;
    use CP\RestBundle\Model\Locator;
    use Doctrine\ORM\Mapping as ORM;
    use Doctrine\Common\Collections\ArrayCollection;


    /**
     * @ORM\HasLifecycleCallbacks()
     * @ORM\Table(name="cp_menu")
     * @ORM\Entity(repositoryClass="CP\API\Repository\MenuRepository")
     */
    class Menu
    {
        /**
         * @var int
         *
         * @ORM\Id()
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;

        /**
         * @var Locator
         *
         * @ORM\ManyToOne(targetEntity="Locator", cascade={"persist"})
         * @ORM\JoinColumn(name="locator_id", referencedColumnName="id", onDelete="SET NULL")
         */
        protected $locator;

        /**
         * Parent Menu
         *
         * @ORM\ManyToOne(targetEntity="Menu", inversedBy="children", cascade={"persist"})
         * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="SET NULL")
         */
        protected $parent;

        /**
         * @ORM\ManyToOne(targetEntity="DataPool",cascade={"persist"})
         * @ORM\JoinColumn(name="datapool_id", referencedColumnName="id", onDelete="SET NULL")
         */
        protected $dataPool;

        /**
         * @ORM\ManyToOne(targetEntity="Product")
         * @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=true)
         */
        protected $product;

        /**
         * @var string
         *
         * @ORM\Column(name="identifier", type="string", nullable=true)
         */
        protected $identifier;

        /**
         * @var array
         *
         * @ORM\Column(name="data", type="text", length=65535)
         */
        protected $data = [];

        /**
         * @var boolean
         *
         * @ORM\Column(name="display", type="boolean")
         */
        protected $display;

        /**
         * @var boolean
         *
         * @ORM\Column(name="display_children", type="boolean")
         */
        protected $displayChildren;

        /**
         * @var string
         *
         * @ORM\Column(name="path_string", type="string", nullable=false)
         */
        protected $pathString;

        /**
         * @var int
         *
         * @ORM\Column(name="priority", type="integer")
         */
        protected $priority;

        /**
         * @var int
         *
         * @ORM\Column(name="status", type="smallint", length=1)
         */
        protected $status;

        /**
         * @var boolean
         *
         * @ORM\Column(name="produce", type="boolean")
         */
        protected $produce = false;

        /**
         * @var int
         *
         * @ORM\Column(name="inheritance_priority", type="integer", nullable=false, options={"default" : 0})
         */
        protected $inheritancePriority;

        /** @var DateTime */
        protected $creation;

        /** @var DateTime */
        protected $modification;

        /**
         * @return int
         */
        public function getId(): int
        {
            return $this->id;
        }

        /**
         * @param int $id
         */
        public function setId(int $id): void
        {
            $this->id = $id;
        }

        /**
         * @return Locator
         */
        public function getLocator(): Locator
        {
            return $this->locator;
        }

        /**
         * @param Locator $locator
         */
        public function setLocator(Locator $locator): void
        {
            $this->locator = $locator;
        }

        /**
         * @return mixed
         */
        public function getParent()
        {
            return $this->parent;
        }

        /**
         * @param mixed $parent
         */
        public function setParent($parent): void
        {
            $this->parent = $parent;
        }

        /**
         * @return mixed
         */
        public function getDataPool()
        {
            return $this->dataPool;
        }

        /**
         * @param mixed $dataPool
         */
        public function setDataPool($dataPool): void
        {
            $this->dataPool = $dataPool;
        }

        /**
         * @return mixed
         */
        public function getProduct()
        {
            return $this->product;
        }

        /**
         * @param mixed $product
         */
        public function setProduct($product): void
        {
            $this->product = $product;
        }

        /**
         * @return string
         */
        public function getIdentifier(): string
        {
            return $this->identifier;
        }

        /**
         * @param string $identifier
         */
        public function setIdentifier(string $identifier): void
        {
            $this->identifier = $identifier;
        }

        /**
         * @return array
         */
        public function getData(): array
        {
            return $this->data;
        }

        /**
         * @param array $data
         */
        public function setData(array $data)
        {
            $this->data = $data;
        }

        /**
         * @return bool
         */
        public function isDisplay(): bool
        {
            return $this->display;
        }

        /**
         * @param bool $display
         */
        public function setDisplay(bool $display): void
        {
            $this->display = $display;
        }

        /**
         * @return bool
         */
        public function isDisplayChildren(): bool
        {
            return $this->displayChildren;
        }

        /**
         * @param bool $displayChildren
         */
        public function setDisplayChildren(bool $displayChildren): void
        {
            $this->displayChildren = $displayChildren;
        }

        /**
         * @return string
         */
        public function getPathString(): string
        {
            return $this->pathString;
        }

        /**
         * @param string $pathString
         */
        public function setPathString(string $pathString): void
        {
            $this->pathString = $pathString;
        }

        /**
         * @return int
         */
        public function getPriority(): int
        {
            return $this->priority;
        }

        /**
         * @param int $priority
         */
        public function setPriority(int $priority): void
        {
            $this->priority = $priority;
        }

        /**
         * @return int
         */
        public function getStatus(): int
        {
            return $this->status;
        }

        /**
         * @param int $status
         */
        public function setStatus(int $status): void
        {
            $this->status = $status;
        }

        /**
         * @return bool
         */
        public function isProduce(): bool
        {
            return $this->produce;
        }

        /**
         * @param bool $produce
         */
        public function setProduce(bool $produce): void
        {
            $this->produce = $produce;
        }

        /**
         * @return int
         */
        public function getInheritancePriority(): int
        {
            return $this->inheritancePriority;
        }

        /**
         * @param int $inheritancePriority
         */
        public function setInheritancePriority(int $inheritancePriority): void
        {
            $this->inheritancePriority = $inheritancePriority;
        }

        /**
         * @return DateTime
         */
        public function getCreation(): DateTime
        {
            return $this->creation;
        }

        /**
         * @param DateTime $creation
         */
        public function setCreation(DateTime $creation): void
        {
            $this->creation = $creation;
        }

        /**
         * @return DateTime
         */
        public function getModification(): DateTime
        {
            return $this->modification;
        }

        /**
         * @param DateTime $modification
         */
        public function setModification(DateTime $modification): void
        {
            $this->modification = $modification;
        }


    }

and my method in MenuRepository:

    /**
         * @param Menu $menu
         * @return mixed
         */
        public function addMenu(Menu $menu)
        {
            try {

                $this->getEntityManager()->beginTransaction(); // suspend auto-commit
                $this->getEntityManager()->persist($menu);
                $this->getEntityManager()->flush($menu);
                $this->getEntityManager()->commit();


                $this->getEntityManager()->detach($menu);


                return $menu;
            } catch (\Exception $e) {
                throw new RepositoryException($e->getMessage());
            }

When i have add new record to database, i got error:

In MenuRepository.php line 79:
Notice: Array to string conversion

line 79 is:

    throw new RepositoryException($e->getMessage());

My Command code to add new record:

    $menuData = new Menu();
    $menuData->setStatus(1);
    $menuData->setData([]);
    $dataPool = new DataPoolEntity();
    $dataPool->setIsReadonly(true);
    $dataPool->setName("qwerty");
    $dataPool->setDescription("tester");
    $dataPool->setChildren(null);
    $menuData->setDataPool($dataPool);
    $menuData->setPathString('qwertty');
    $menuData->setPriority(1);
    $menuData->setDisplayChildren(true);
    $menuData->setDisplay(true);
    $menuData->setIdentifier("qwery");
    $menuData->setInheritancePriority(1);

In my var.log i have this message:

[2019-12-21T20:35:49.313897+01:00] console.ERROR: Error thrown while running command "sdk:menu:create -vvvvv". Message: "Notice: Array to string conversion" {"exception":"[object] (CP\Model\Exception\RepositoryException(code: 0): Notice: Array to string conversion at C:\Users\rever\PhpstormProjects\cp-base\vendor\cp\web-core\src\Model\Repository\MenuRepository.php:79)","command":"sdk:menu:create -vvvvv","message":"Notice: Array to string conversion"} []

I don't have idea what is wrong with my code :( For several hours I have been looking for the cause of the error and I can't deal with it, that's why I wrote. I have already tried to add some value rigidly, but it will give the same problem.

0

1 Answer 1

1

your data are defined in your entity like this:

/**
 * @var array
 *
 * @ORM\Column(name="data", type="text", length=65535)
 */
protected $data = [];

but in your @var you specified array, so you can change the column type to array like this:

/**
 *
 * @ORM\Column(name="data", type="array")
 */
protected $data = [];

This will result in a longtext field with comment "(DC2Type:array)" so Doctrine knows how to handle it. It will store a serialized array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.