简体   繁体   English

注意:Symfony 中数组到字符串的转换

[英]Notice: Array to string conversion in Symfony

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 :和我在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:在 MenuRepository.php 第 79 行:
Notice: Array to string conversion注意:数组到字符串的转换

line 79 is:第 79 行是:

    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:在我的 var.log 我有这条消息:

[2019-12-21T20:35:49.313897+01:00] console.ERROR: Error thrown while running command "sdk:menu:create -vvvvv". [2019-12-21T20:35:49.313897+01:00] console.ERROR: 运行命令“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"} []消息:“注意:数组到字符串转换”{“异常”:“[对象](CP\\Model\\Exception\\RepositoryException(代码:0):注意:数组到字符串转换在 C:\\Users\\rever\\PhpstormProjects\\cp -base\\vendor\\cp\\web-core\\src\\Model\\Repository\\MenuRepository.php:79)","command":"sdk:menu:create -vvvvv","message":"注意:数组到字符串的转换"} []

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.我不知道我的代码有什么问题:(几个小时以来我一直在寻找错误的原因,但我无法处理它,这就是我写的原因。我已经尝试过严格地添加一些值,但它会给出同样的问题。

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:但是在您的@var您指定了array ,因此您可以将列类型更改为array如下所示:

/**
 *
 * @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.这将产生带有注释“(DC2Type:array)”的长文本字段,因此 Doctrine 知道如何处理它。 It will store a serialized array.它将存储一个序列化的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM