$ entity-> getId():错误:数组

时间:2019-04-16 10:20:17

标签: php arrays symfony

我的功能有问题。 我有一个User实体,该实体具有一个包arrayCollection(在许多方面)。 目标是获取其拥有的软件包的ID数组。

为此,我使用两个函数:

/**
 * {@inheritdoc}
 * 
 * @return Collection
 */
public function getPackages()
{
    return $this->packages->toArray();
}




public function getPackagesId()
{
    $lesPackages = $this->getPackages();
    $tabId = array();
    foreach ($lesPackages as $unPackage)
    {
        $id = $unPackage->getId();
        array_push($tabId,$id);
    }

    return $tabId;
}

问题是我收到此错误:

  

错误:在数组上调用成员函数getId()

但是,我不明白怎么了,语法很好吗?

我的实体:

<?php

namespace Site\PagesBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Site\PagesBundle\Security\Traits\traitUser;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * UserCas
 *
 * @ORM\Table(name="user_cas")
 * @ORM\Entity(repositoryClass="Site\PagesBundle\Repository\UserCasRepository")
 */
class UserCas
{

    use traitUser;


    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var int
     *
     * @ORM\Column(name="nbTelechargementsAuto", type="integer", nullable=true)
     */
    private $nbTelechargementsAuto;

    /**
     * @var bool
     *
     * @ORM\Column(name="enabled", type="boolean")
     */
    private $enabled;


    /**
     * @ORM\Column(name="mail", type="string")
     */
    private $mail;

    /**  
     * @var \Doctrine\Common\Collections\Collection
     * @ORM\ManyToMany(targetEntity="Paquet")  
     * @ORM\JoinTable(name="paquetsDDLUserCas") 
     * @ORM\JoinColumn(nullable=false)
     */  
    private $packages;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->packages = new ArrayCollection();
        $this->setEnabled(true);

    }




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


    /**
     * @return string
     */
    public function getMail()
    {
        return $this->mail;
    }

    public function setMail($mail)
    {
        $this->mail = $mail;
    }


    /**
     * Set enabled
     *
     * @param boolean $enabled
     *
     * @return UserCas
     */
    public function setEnabled($enabled)
    {
        $this->enabled = $enabled;

        return $this;
    }

    public function isEnabled()
    {
        return $this->enabled;
    }

}

所以:我的包裹属性:

/**  
 * @var \Doctrine\Common\Collections\Collection
 * @ORM\ManyToMany(targetEntity="Paquet")  
 * @ORM\JoinTable(name="paquetsDDLUserCas") 
 * @ORM\JoinColumn(nullable=false)
 */  
private $packages;

/**
 * Constructor
 */
public function __construct()
{
    $this->packages = new ArrayCollection();
    $this->setEnabled(true);

}

我对某些功能使用特质:

<?php

namespace Site\PagesBundle\Security\Traits;

trait traitUser
{
    public function decDDL()
    {
        $this->setNbTelechargementsAuto($this->getNbTelechargementsAuto() - 1);
    }

    public function verifDDL()
    {
        if($this->getNbTelechargementsAuto() == 0)
        {
            $this->setEnabled(false);
        }
    }


    /**
     * Désactivation du compte
     */
    public function desactiverCompte()
    {
        $this->setEnabled(false);
        $this->setCreatedAt();

        return $this;
    }

    /**
     * Set nbTelechargementsAuto
     *
     * @param integer $nbTelechargementsAuto
     *
     */
    public function setNbTelechargementsAuto($nbTelechargementsAuto)
    {
        $this->nbTelechargementsAuto = $nbTelechargementsAuto;

        return $this;
    }

    /**
     * Get nbTelechargementsAuto
     *
     * @return int
     */
    public function getNbTelechargementsAuto()
    {
        return $this->nbTelechargementsAuto;
    }

    public function addPackage($package)
    {
        $this->packages[] = $package;

    }

    /**
     * {@inheritdoc}
     * 
     * @return Collection
     */
    public function getPackages()
    {
        return $this->packages->toArray();
    }

    public function getPackagesId()
    {
        $lesPackages = $this->getPackages();
        $tabId = array();
        foreach ($lesPackages as $unPackage)
        {
            dump($unPackage);
            $id = $unPackage->getId();
            array_push($tabId,$id);
        }

        return $tabId;
    }

    /**
     * {@inheritdoc}
     */
    public function setPackages(array $packages)
    {
        $this->packages = array();

        foreach ($packages as $package) {
            $this->addPackage($package);
        }

        $this->decDDL();
        $this->verifDDL();

        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function removePackage($package)
    {
        if (false !== $key = array_search(strtoupper($package), $this->packages, true)) {
            unset($this->packages[$key]);
            $this->packages = array_values($this->packages);
        }

        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function hasPackage($package)
    {
        return in_array(strtoupper($package), $this->getPackages(), true);
    }
}

还有转储:

traitUser.php  line 77
Paquet {#577 ▼
  -id: 27
  -typeUser: PersistentCollection {#371 ▶}
  -titre: "testtyu"
  -urlPaquet: "ck3.PNG"
  -paquetFile: File {#231 ▶}
  -updatedAt: DateTimeImmutable @1555411482 {#659 ▶}
  -urlNotice: "commande composer.txt"
  -noticeFile: File {#687 ▶}
  -commentaire: "atgz"
}
traitUser.php  line 77
Paquet {#389 ▼
  -id: 28
  -typeUser: PersistentCollection {#381 ▶}
  -titre: "retest"
  -urlPaquet: "baubiche.PNG"
  -paquetFile: File {#685 ▶}
  -updatedAt: DateTimeImmutable @1555411482 {#226 ▶}
  -urlNotice: "ck3.PNG"
  -noticeFile: File {#686 ▶}
  -commentaire: "rthrh"
}
DefaultController.php  line 222
array:2 [▼
  0 => array:9 [▶]
  1 => array:9 [▶]
]
traitUser.php  line 77
array:9 [▼
  "typeUser" => array:2 [▶]
  "updatedAt" => "2019-04-16T12:44:42+02:00"
  "paquetFile" => "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAwEAAAIPCAYAAAArYtLRAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAKAZSURBVHhe7f35dx3Hle+Jnn ▶"
  "id" => 27
  "titre" => "testtyu"
  "urlPaquet" => "ck3.PNG"
  "noticeFile" => "data:text/html,export%20COMPOSER_MEMORY_LIMIT%3D-1%3B%20composer%20update%0D%0A%0D%0Aphp%20-d%20memory_limit%3D-1%20%2Fusr%2Flocal%2Fbin%2Fcomposer%20update%0D% ▶"
  "urlNotice" => "commande composer.txt"
  "commentaire" => "atgz"
]

1 个答案:

答案 0 :(得分:0)

$this->getPackages();返回2 Paquet对象和一个数组(我不知道为什么)。您可以检查该元素是否为Paquet对象或跳转到下一个元素。但这是解决方法,而不是解决方案。


public function getPackagesId()
{
    $lesPackages = $this->getPackages();
    $tabId = array();
    foreach ($lesPackages as $unPackage)
    {
        if (!$unPackage instanceof Paquet)
            continue;

        $id = $unPackage->getId();
        array_push($tabId,$id);
    }

    return $tabId;
}