Home > Software engineering >  Pass values to an other entity Symfony
Pass values to an other entity Symfony

Time:09-17

What I need to do is to export as as .csv a list of a lot of different values inside different entities. I need each reference created at a certain date with related values.

What I did was first, find all the objects by date and put them in an array.

$parcelRepo = $this->getDoctrine()->getRepository(Parcel::class);

    $dateToday = new \DateTime("now");

    $parcels = $parcelRepo->findBy([
        'date_add' => $dateToday
    ]);

Then, in a foreach loop, I find the values that relates to each parcel entity. This is where I have a problem, I don't understand how to access the values which are not located in the same table as my first parcel entity. I need to not use SQL as well.

Here is the code where I get the "Only variables should be passed by reference" error.

$array = [];


    foreach ($parcels as &$p) {
        $array.array_push(
            $p->getDeliveryOrder()->getProduct()->getAccount()->getCoclico(),
            "1",
            "",
            $p->getDeliveryOrder()->getUser()->getLogin(),
            $p->getDeliveryOrder()->getProduct()->getCode(),
            $p->getInternalReference(),
            $p->getDeliveryOrder()->getProduct()->getCode() "0" $p->getNumber(),
            "121",
            $p->getName(),
            $p->getAddress4(),
            $p->getAddress1(),
            $p->getAddress2(),
            $p->getAddress3(),
            "téléphone", //TODO
            $p->getPostalCode(),
            $p->getCity(),
            \DateTime::createFromFormat('Ymd',$dateToday),
            "",
            "",
            "0"
        );
    }

I think I need to use QueryBuilder but was wondering if there was any other way to do what I need since QueryBuilder is close to SQL (from what I understand).

Also, as I need to export each foreach values in one line, I need a multidimensional array. However I did not look into this issue since I can't even get the values I need.

CodePudding user response:

So the issue comes from

$array.array_push(

this code actually merges $array and array_push() returning value, array_push() first parameter should be the array ( which is sent through reference) you want to push in, and because you are actually sending a value and not a variable, this error appears.

Here is the documentation for array_push https://www.php.net/manual/ro/function.array-push.php so it should actually be

array_push($array, ...);

But as Ricardo left you a comment, this code is not really necessary, you can get the formatted data like this from a query, chaining multiple relation calling like:

$p->getDeliveryOrder()->getProduct()->getAccount()->getCoclico()

is not really desirable, but if you think you can't do that then the fix is just to write array_push correctly.

  • Related