Search code examples
phpdoctrine-ormsymfony4entitymanagerpersist

symfony4 error spl_object_hash() expects parameter 1 to be object, string given


An error was appearing when I tried to persist entity:

        $newApplication = new NewApplication(
            $period,
            $ownFunds,
            $orderId,
            $additionalFields,
            $products,
            $rate,
            $latitude,
            $longitude,
            $personalDataSmsCode,
            $shopType,
            $shopUrl,
            $shopAddress,
            $cashierFullName,
            $merchantInn,
            $merchant,
            $shop,
            $customer,
            $cashier
        );

        $this->em->persist($newApplication);

Entity constructor code:

/**
     * @param int                      $period
     * @param int                      $ownFunds
     * @param int                      $orderId
     * @param array | SaleCustomData[] $customData
     * @param array | UnsoldProduct[]  $products
     * @param Rate                     $rate
     * @param float                    $latitude
     * @param float                    $longitude
     * @param int                      $personalDataSmsCode
     * @param ShopType                 $shopType
     * @param string | null            $shopUrl
     * @param string | null            $shopAddress
     * @param string | null            $cashierFullName
     * @param Inn                      $merchantInn
     * @param Merchant                 $merchant
     * @param Shop                     $shop
     * @param Customer                 $customer
     * @param ShopCashier | null       $cashier
     */
    public function __construct(
        int $period,
        int $ownFunds,
        int $orderId,
        array $customData,
        array $products,
        Rate $rate,
        float $latitude,
        float $longitude,
        int $personalDataSmsCode,
        ShopType $shopType,
        ?string $shopUrl,
        ?string $shopAddress,
        string $cashierFullName,
        Inn $merchantInn,
        Merchant $merchant,
        Shop $shop,
        Customer $customer,
        ?ShopCashier $cashier = null
    ) {
        $this->merchantInn = $merchantInn;
        $this->shopType = $shopType;
        $this->period = $period;
        $this->ownFunds = $ownFunds;
        $this->orderId = $orderId;
        $this->customData = new ArrayCollection(array_unique($customData, SORT_REGULAR));
        $this->products = new ArrayCollection(array_unique($products, SORT_REGULAR));
        $this->rate = $rate;
        $this->latitude = $latitude;
        $this->longitude = $longitude;
        $this->personalDataSmsCode = $personalDataSmsCode;
        $this->shopUrl = $shopUrl;
        $this->shopAddress = $shopAddress;
        $this->cashierFullName = $cashierFullName;
        $this->merchant = $merchant;
        $this->shop = $shop;
        $this->cashier = $cashier;
        $this->customer = $customer;
    }

1) First of all to resolve this problem I tried dump NewApplication:

dump($newApplication);
die;

But it didn't help. $newApplication was object - not a string, as an error said.

2) My second version was that the problem was in objects which cascade persist with NewApplication:

     * @param Rate                     $rate
     * @param ShopType                 $shopType
     * @param Inn                      $merchantInn
     * @param Merchant                 $merchant
     * @param Shop                     $shop
     * @param Customer                 $customer
     * @param ShopCashier | null       $cashier

But they were objects too. It wasn't the reason for my error as well.

What else could be the solution?


Solution

  • The problem was in fields containing Collections: $customData and $products. I put there arrays of strings instead of arrays of objects.

    Be careful when working with large entities! Good luck!